1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub fn parse<D, R>(value: D) -> Result<R, String>
where
    D: serde::Serialize,
    for<'r> R: serde::Deserialize<'r>,
{
    match serde_json::to_value(&value) {
        Ok(v) => {
            let document: R = match serde_json::from_value(v) {
                Ok(data) => data,
                Err(e) => return Err(e.to_string()),
            };
            Ok(document)
        }
        Err(e) => return Err(e.to_string()),
    }
}

#[cfg(test)]
mod tests {
    // Note this useful idiom: importing names from outer (for mod tests) scope.
    use super::*;

    #[test]
    fn test_parse() -> Result<(), String> {
        #[derive(Debug, Deserialize, Serialize, PartialEq)]
        pub struct Entry {
            a: u64,
            b: u64,
            c: String,
        }

        #[derive(Debug, Deserialize, Serialize, PartialEq)]
        pub struct Document {
            a: u64,
            b: u64,
            c: String,
        }
        let entry = Entry {
            a: 24,
            b: 42,
            c: "nice".to_string(),
        };

        assert_eq!(
            parse::<Entry, Document>(entry)?,
            Document {
                a: 24,
                b: 42,
                c: "nice".to_string(),
            }
        );
        Ok(())
    }
}