Announcing serde-query 0.2.0

138 points __pandaman64__

Hi! I have released version 0.2.0 of serde-query! This proc-macro library lets you write queries against any serde-compatible data format: https://github.com/pandaman64/serde-query/

The new version comes with a complete rewrite of the code generation. This rewrite allowed me to implement array queries and improve the error messages significantly! Any questions and comments are welcome!

Example

Array queries

#[derive(DeserializeQuery)]
struct Data {
    #[query(".commits.[].author")]
    authors: Vec<String>,
    #[query(".count")]
    count: usize,
}

let document = serde_json::json!({
    "commits": [
        { "author":    "Kou", "hash": 0x0202 },
        { "author": "Kasumi", "hash": 0x1013 },
        { "author": "Masaru", "hash": 0x0809 },
    ],
    "count": 3,
}).to_string();

// You can use `Query<T>` as a `Deserialize` type for any `Deserializer`
// and convert the result to the desired type using `From`/`Into`.
let data: Data = serde_json::from_str::<Query<Data>>(&document)?.into();

assert_eq!(data.authors, vec!["Kou", "Kasumi", "Masaru"]);
assert_eq!(data.count, 3);

Error messages

#[derive(Debug, Deserialize)]
struct Data {
    // missing field
    #[query(".author.name")]
    author_name: String,
    // typo
    #[query(".commit.commiter.name")]
    committer_name: String,
    // type error
    #[query(".author.id")]
    id: String,
}

let error = serde_json::from_str::<Data>(INPUT).unwrap_err();
assert_eq!(
    error.to_string(),
    r#"
Queries failed for fields: 'author_name', 'committer_name', 'id'
1. Query for field 'author_name' failed at '.author': missing field 'name'
2. Query for field 'committer_name' failed at '.commit': missing field 'commiter'
3. Query for field 'id' failed at '.author.id': invalid type: integer `5635139`, expected a string at line 34 column 17
"#
    .trim_start()
);