geosuggest-core

Crates.iogeosuggest-core
lib.rsgeosuggest-core
version0.6.4
sourcesrc
created_at2023-08-18 18:45:41.011892
updated_at2024-09-20 11:19:04.83135
descriptionSuggest by name or find nearest by coordinates cities
homepage
repositoryhttps://github.com/estin/geosuggest.git
max_upload_size
id948098
size135,970
Evgeniy Tatarkin (estin)

documentation

https://docs.rs/geosuggest-core/

README

geosuggest-core

Library to suggest and to find nearest by coordinates cities

Live demo with sources

HTTP service

Examples

Usage example

use tokio;
use anyhow::Result;

use geosuggest_core::{Engine, storage::{self, IndexStorage}};
use geosuggest_utils::{IndexUpdater, IndexUpdaterSettings};

#[tokio::main]
async fn main() -> Result<()> {
    println!("Build index...");
    let engine = load_engine().await?;

    println!(
        "Suggest result: {:#?}",
        engine.suggest::<&str>("Beverley", 1, None, Some(&["us"]))
    );
    println!(
        "Reverse result: {:#?}",
        engine.reverse::<&str>((11.138298, 57.510973), 1, None, None)
    );

    Ok(())
}

async fn load_engine() -> Result<Engine> {
    let index_file = std::path::Path::new("/tmp/geosuggest-index.bincode");

    let updater = IndexUpdater::new(IndexUpdaterSettings {
        names: None, // no multilang support
        ..Default::default()
    })?;

    let storage = storage::bincode::Storage::new();

    Ok(if index_file.exists() {
        // load existed index
        let metadata = storage
            .read_metadata(index_file)
            .map_err(|e| anyhow::anyhow!("On load index metadata from {index_file:?}: {e}"))?;

        match metadata {
            Some(m) if updater.has_updates(&m).await? => {
                let engine = updater.build().await?;
                storage
                    .dump_to(index_file, &engine)
                    .map_err(|e| anyhow::anyhow!("Failed dump to {index_file:?}: {e}"))?;
                engine
            }
            _ => storage
                .load_from(index_file)
                .map_err(|e| anyhow::anyhow!("On load index from {index_file:?}: {e}"))?,
        }
    } else {
        // initial
        let engine = updater.build().await?;
        storage
            .dump_to(index_file, &engine)
            .map_err(|e| anyhow::anyhow!("Failed dump to {index_file:?}: {e}"))?;
        engine
    })

}
Commit count: 132

cargo fmt