Extracting data with saphyr

⚓ Rust    📅 2025-08-14    👤 surdeus    👁️ 2      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Extracting data with saphyr

G'day!

I spent the afternoon hacking on this. Given the following YAML:

settings:
    endpoint: "http://localhost/api/search/instant"
    limit: 50
    site-language: "en"
    restrict: "all"
    selected-languages: [ "lzh", "en", "pgd", "kho", "pli", "pra", "san", "xct", "xto", "uig" ]
    match-partial: false

I've managed to get the endpoint out and into my Settings struct:

impl TryFrom<&Yaml<'_>> for Settings {
    type Error = anyhow::Error;

    fn try_from(yaml: &Yaml) -> Result<Self> {
        if let Yaml::Mapping(settings) = &yaml["settings"] {
            let endpoint_key = &Yaml::Value(Scalar::String(Cow::from("endpoint")));

            let endpoint = settings
                .get(endpoint_key)
                .context("Missing endpoint")?
                .as_str()
                .context("Endpoint not a string")?
                .to_string();

            Ok(Settings {
                endpoint,
                limit: 50,
                site_language: "en".to_string(),
                restrict: "all".to_string(),
                selected_languages: vec!["en".to_string()],
                match_partial: false,
            })
        } else {
            Err(anyhow!("Settings is not a mapping"))
        }
    }
}

That endpoint_key looks a bit funky. Am I on the right track?

1 post - 1 participant

Read full topic

🏷️ Rust_feed