pub fn read_metadata(conn: &rusqlite::Connection) -> Result<Metadata, Box<dyn std::error::Error>> {
    let mut select_metadata = conn.prepare_cached("SELECT name, value FROM metadata")?;
    let mut rows = select_metadata.query([])?;

    let mut metadata = Metadata::default();

    let mut zoom_range = (None, None);
    let mut format_str = String::new();
    let mut mvt_metadata_json = String::new();

    while let Some(row) = rows.next()? {
        let name: String = row.get(0)?;
        let value: String = row.get(1)?;

        match name.as_str() {
            "name" => metadata.name = value,
            "format" => format_str = value,
            "bounds" => {
                let split: Vec<&str> = value.split(",").collect();
                if split.len() == 4 {
                    let bounds = (
                        split[0].parse::<f64>(),
                        split[1].parse::<f64>(),
                        split[2].parse::<f64>(),
                        split[3].parse::<f64>(),
                    );
                    if let (Ok(left), Ok(bottom), Ok(right), Ok(top)) = bounds {
                        let tl_br = (GeoCoord::from_degrees(left, top), GeoCoord::from_degrees(right, bottom));
                        if let (Ok(tl), Ok(br)) = tl_br {
                            if let Ok(bbox) = GeoRect::new(tl, br) {
                                metadata.bounds = Some(bbox);
                            }
                        }
                    }
                }
            }
            "center" => {
                let split: Vec<&str> = value.split(",").collect();
                if split.len() == 3 {
                    let center = (
                        split[0].parse::<f64>(),
                        split[1].parse::<f64>(),
                        split[2].parse::<u32>(),
                    );
                    if let (Ok(lon), Ok(lat), Ok(zoom_level)) = center {
                        if let Ok(coord) = GeoCoord::from_degrees(lon, lat) {
                            metadata.center = Some((coord, zoom_level));
                        }
                    }
                }
            }
            "minzoom" => {
                if let Ok(minzoom) = value.parse::<u32>() {
                    zoom_range.0 = Some(minzoom);
                }
            }
            "maxzoom" => {
                if let Ok(maxzoom) = value.parse::<u32>() {
                    zoom_range.1 = Some(maxzoom);
                }
            }
            "attribution" => metadata.attribution = Some(value),
            "description" => metadata.description = Some(value),
            "type" => {
                if let Ok(r#type) = Type::try_from(value.as_str()) {
                    metadata.r#type = Some(r#type);
                }
            }
            "version" => {
                if let Ok(version) = value.parse::<u32>() {
                    metadata.version = Some(version);
                }
            }
            "json" => mvt_metadata_json = value,
            unknown_key => {
                metadata.custom.insert(unknown_key.to_owned(), value);
            }
        }
    }

    // TODO: error on empty format_str

    metadata.format = match format_str.as_str() {
        "pbf" => {
            let mvt_metadata = serde_json::from_str::<MvtMetadata>(&mvt_metadata_json)?;
            FileFormat::Pbf(mvt_metadata)
        }
        "jpg" => FileFormat::Jpg,
        "png" => FileFormat::Png,
        "webp" => FileFormat::Webp,
        ietf_type => FileFormat::Other(ietf_type.to_owned()),
    };

    if let (Some(minzoom), Some(maxzoom)) = zoom_range {
        metadata.zoom_range = Some(minzoom..=maxzoom);
    }

    Ok(metadata)
}
/// Reads metadata from the given database.
Reads metadata from the given database.
Reads metadata from the given database .
pub fn read_metadata(conn: &rusqlite::Connection) -> Result<Metadata, Box<dyn std::error::Error>> {
    let mut select_metadata = conn.prepare_cached("SELECT name, value FROM metadata")?;
    let mut rows = select_metadata.query([])?;
    let mut metadata = Metadata::default();
    let mut zoom_range = (None, None);
    let mut format_str = String::new();
    let mut mvt_metadata_json = String::new();
    while let Some(row) = rows.next()? {
        let name: String = row.get(0)?;
        let value: String = row.get(1)?;
        match name.as_str() {
            "name" => metadata.name = value,
            "format" => format_str = value,
            "bounds" => {
                let split: Vec<&str> = value.split(",").collect();
                if split.len() == 4 {
                    let bounds = (
                        split[0].parse::<f64>(),
                        split[1].parse::<f64>(),
                        split[2].parse::<f64>(),
                        split[3].parse::<f64>(),
                    );
                    if let (Ok(left), Ok(bottom), Ok(right), Ok(top)) = bounds {
                        let tl_br = (GeoCoord::from_degrees(left, top), GeoCoord::from_degrees(right, bottom));
                        if let (Ok(tl), Ok(br)) = tl_br {
                            if let Ok(bbox) = GeoRect::new(tl, br) {
                                metadata.bounds = Some(bbox);
                            }
                        }
                    }
                }
            }
            "center" => {
                let split: Vec<&str> = value.split(",").collect();
                if split.len() == 3 {
                    let center = (
                        split[0].parse::<f64>(),
                        split[1].parse::<f64>(),
                        split[2].parse::<u32>(),
                    );
                    if let (Ok(lon), Ok(lat), Ok(zoom_level)) = center {
                        if let Ok(coord) = GeoCoord::from_degrees(lon, lat) {
                            metadata.center = Some((coord, zoom_level));
                        }
                    }
                }
            }
            "minzoom" => {
                if let Ok(minzoom) = value.parse::<u32>() {
                    zoom_range.0 = Some(minzoom);
                }
            }
            "maxzoom" => {
                if let Ok(maxzoom) = value.parse::<u32>() {
                    zoom_range.1 = Some(maxzoom);
                }
            }
            "attribution" => metadata.attribution = Some(value),
            "description" => metadata.description = Some(value),
            "type" => {
                if let Ok(r#type) = Type::try_from(value.as_str()) {
                    metadata.r#type = Some(r#type);
                }
            }
            "version" => {
                if let Ok(version) = value.parse::<u32>() {
                    metadata.version = Some(version);
                }
            }
            "json" => mvt_metadata_json = value,
            unknown_key => {
                metadata.custom.insert(unknown_key.to_owned(), value);
            }
        }
    }
    // TODO: error on empty format_str
    metadata.format = match format_str.as_str() {
        "pbf" => {
            let mvt_metadata = serde_json::from_str::<MvtMetadata>(&mvt_metadata_json)?;
            FileFormat::Pbf(mvt_metadata)
        }
        "jpg" => FileFormat::Jpg,
        "png" => FileFormat::Png,
        "webp" => FileFormat::Webp,
        ietf_type => FileFormat::Other(ietf_type.to_owned()),
    };
    if let (Some(minzoom), Some(maxzoom)) = zoom_range {
        metadata.zoom_range = Some(minzoom..=maxzoom);
    }
    Ok(metadata)
}
pub fn read_metadata ( conn : & rusqlite :: Connection ) -> Result < Metadata , Box < dyn std :: error :: Error > > { let mut select_metadata = conn . prepare_cached ( " " ) ? ; let mut rows = select_metadata . query ( [ ] ) ? ; let mut metadata = Metadata :: default ( ) ; let mut zoom_range = ( None , None ) ; let mut format_str = String :: new ( ) ; let mut mvt_metadata_json = String :: new ( ) ; while let Some ( row ) = rows . next ( ) ? { let name : String = row . get ( 0 ) ? ; let value : String = row . get ( 1 ) ? ; match name . as_str ( ) { " " => metadata . name = value , " " => format_str = value , " " => { let split : Vec < & str > = value . split ( " " ) . collect ( ) ; if split . len ( ) == 4 { let bounds = ( split [ 0 ] . parse :: < f64 > ( ) , split [ 1 ] . parse :: < f64 > ( ) , split [ 2 ] . parse :: < f64 > ( ) , split [ 3 ] . parse :: < f64 > ( ) , ) ; if let ( Ok ( left ) , Ok ( bottom ) , Ok ( right ) , Ok ( top ) ) = bounds { let tl_br = ( GeoCoord :: from_degrees ( left , top ) , GeoCoord :: from_degrees ( right , bottom ) ) ; if let ( Ok ( tl ) , Ok ( br ) ) = tl_br { if let Ok ( bbox ) = GeoRect :: new ( tl , br ) { metadata . bounds = Some ( bbox ) ; } } } } } " " => { let split : Vec < & str > = value . split ( " " ) . collect ( ) ; if split . len ( ) == 3 { let center = ( split [ 0 ] . parse :: < f64 > ( ) , split [ 1 ] . parse :: < f64 > ( ) , split [ 2 ] . parse :: < u32 > ( ) , ) ; if let ( Ok ( lon ) , Ok ( lat ) , Ok ( zoom_level ) ) = center { if let Ok ( coord ) = GeoCoord :: from_degrees ( lon , lat ) { metadata . center = Some ( ( coord , zoom_level ) ) ; } } } } " " => { if let Ok ( minzoom ) = value . parse :: < u32 > ( ) { zoom_range . 0 = Some ( minzoom ) ; } } " " => { if let Ok ( maxzoom ) = value . parse :: < u32 > ( ) { zoom_range . 1 = Some ( maxzoom ) ; } } " " => metadata . attribution = Some ( value ) , " " => metadata . description = Some ( value ) , " " => { if let Ok ( r#type ) = Type :: try_from ( value . as_str ( ) ) { metadata . r#type = Some ( r#type ) ; } } " " => { if let Ok ( version ) = value . parse :: < u32 > ( ) { metadata . version = Some ( version ) ; } } " " => mvt_metadata_json = value , unknown_key => { metadata . custom . insert ( unknown_key . to_owned ( ) , value ) ; } } } // TODO: error on empty format_str metadata . format = match format_str . as_str ( ) { " " => { let mvt_metadata = serde_json :: from_str :: < MvtMetadata > ( & mvt_metadata_json ) ? ; FileFormat :: Pbf ( mvt_metadata ) } " " => FileFormat :: Jpg , " " => FileFormat :: Png , " " => FileFormat :: Webp , ietf_type => FileFormat :: Other ( ietf_type . to_owned ( ) ) , } ; if let ( Some ( minzoom ) , Some ( maxzoom ) ) = zoom_range { metadata . zoom_range = Some ( minzoom ..= maxzoom ) ; } Ok ( metadata ) }
Reads metadata from the given database.
Reads metadata from the given database .
conn
&rusqlite::Connection