Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to deserialize a key=value list of strings in Rust

I have multiple structs that need to be deserialized from a String that is a bunch of lines with key value pairs which represent the struct's attributes.

Example

field1=something
field2=556
field3=true
field4=10.0.0.1

The types for each field will always be the same, but are not always present. The order can also change.

struct Data {
    field1: Option<String>,
    field2: Option<u32>,
    field3: Option<bool>,
    field4: Option<std::net::Ipv4Addr>
}

What is the best way to do this? Should I be using the serde crate?

I know that I can do it manually like this (see https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e16244e50492aa218217cb44d5f27cfe)

But how do I genericise this for multiple structs?

use std::net::Ipv4Addr;
use std::str::FromStr;

#[derive(Debug)]
struct Data {
    field1: Option<String>,
    field2: Option<u32>,
    field3: Option<bool>,
    field4: Option<Ipv4Addr>,
}

fn main() {
    let mut s = "field1=something
field2=556
field3=true
field4=10.0.0.1"
        .to_string();

    let mut field1 = None;
    let mut field2 = None;
    let mut field3 = None;
    let mut field4 = None;

    let lines: Vec<_> = s.split("\n").collect();

    for line in lines {
        let pair: Vec<_> = line.splitn(2, "=").collect();
        let key = pair[0];
        let value = pair[1];

        match key {
            "field1" => {
                field1 = Some(value.to_owned());
            }
            "field2" => {
                field2 = Some(u32::from_str(value).unwrap());
            }
            "field3" => {
                field3 = match value {
                    "true" => Some(true),
                    "false" => Some(false),
                    _ => None
                };
            }
            "field4" => {
                field4 = Some(Ipv4Addr::from_str(value).unwrap());
            }
            _ => {}
        }
    }

    println!(
        "{:?}",
        Data {
            field1,
            field2,
            field3,
            field4
        }
    );
}
like image 687
Ross MacArthur Avatar asked Oct 15 '25 19:10

Ross MacArthur


1 Answers

A way to generalize for multiple target structs may be to use serde.

In case of a custom format a deserializer have to be implemented, but instead of such implementation it is worth considering a community supported data format when the custom format is a subset of or it is similar to the standard.

It seems that your format is a subset of TOML format: if this is the case use toml.

#[macro_use]
extern crate serde_derive;

#[derive(Serialize, Deserialize, Debug)]
struct Data {
    field1: Option<String>,
    field2: Option<u32>,
    field3: Option<bool>,
    field4: Option<std::net::Ipv4Addr>
}

fn main() {
    let serialized = r#"
field1="something"
field2=556
field3=true
field4="10.0.0.1"
"#;

    let deserialized: Data = toml::from_str(&serialized).unwrap();
    println!("{:?}", deserialized);

}

If your format is not exactly "standard" compatible look for a way to transform the encoded data before deserializing: for example if field1 and field4 are not quoted strings a regex based pattern substitution may works:

#[macro_use]
extern crate serde_derive;

use std::borrow::Cow;
use regex::{Captures, Regex};

#[derive(Serialize, Deserialize, Debug)]
struct Data {
    field1: Option<String>,
    field2: Option<u32>,
    field3: Option<bool>,
    field4: Option<std::net::Ipv4Addr>,
}

fn reformat_string(before: &str) -> Cow<str> {
    let matcher : Regex = Regex::new(
            r"(?P<f>field1|field4)=(?P<val>[\w.]+)"
            ).unwrap();

    matcher.replace_all(before, |cap: &Captures| {
        let mut buff = String::new();
        if &cap[1] == "field1" || &cap[1] == "field4" {
            cap.expand("$f='$val'", &mut buff);
        }
        Cow::Owned(buff)
    })
}


fn main() {
    let serialized = r#"
    field1=something
    field2=556
    field3=true
    field4=10.0.0.1
    "#;


    let transformed = reformat_string(serialized);
    let deserialized: Data = toml::from_str(&transformed).unwrap();
    println!("{:?}", deserialized);
}
like image 191
attdona Avatar answered Oct 18 '25 11:10

attdona



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!