summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: f40604e8286a2e4b5058b7dfeb7ef3d1a725ada4 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use anyhow::{anyhow, Context};
use serde_json::Map;
use toml_edit::{DocumentMut, Table};

/// Apply updates from a JSON file to a TOML file while preserving comments in the latter.
#[derive(argh::FromArgs)]
struct Args {
    #[argh(positional)]
    toml_file: String,

    #[argh(positional)]
    json_file: String,

    /// overwrites the given TOML file rather than printing to stdout
    #[argh(switch)]
    write: bool,
}

fn main() -> anyhow::Result<()> {
    let args: Args = argh::from_env();

    let toml = std::fs::read_to_string(&args.toml_file)?;
    let json = std::fs::read_to_string(&args.json_file)?;

    let mut doc: DocumentMut = toml.parse()?;
    let patch: Map<String, serde_json::Value> =
        serde_json::from_str(&json).context("failed to parse JSON")?;
    apply(&patch, doc.as_table_mut())?;
    if args.write {
        std::fs::write(args.toml_file, doc.to_string())?;
    } else {
        print!("{doc}");
    }
    Ok(())
}

fn apply(patch: &Map<String, serde_json::Value>, table: &mut Table) -> anyhow::Result<()> {
    for (key, val) in patch {
        match val {
            serde_json::Value::Object(map) => {
                if !table.contains_key(key) || !matches!(table[key], toml_edit::Item::Table(_)) {
                    table.insert(key, toml_edit::table());
                }
                apply(map, table[key].as_table_mut().unwrap())?;
            }
            other => match json_value_to_toml_value(other)? {
                None => {
                    table.remove(key);
                }
                Some(new_value) => {
                    if let Some((_, value)) = table.get_key_value_mut(key) {
                        *value = new_value.into();
                    } else {
                        table.insert(key, new_value.into());
                    }
                }
            },
        }
    }
    Ok(())
}

fn json_value_to_toml_value(value: &serde_json::Value) -> anyhow::Result<Option<toml_edit::Value>> {
    Ok(match value {
        serde_json::Value::Null => None,
        serde_json::Value::Bool(bool) => Some((*bool).into()),
        serde_json::Value::Number(number) => {
            if number.is_u64() {
                Some(i64::try_from(number.as_u64().unwrap()).unwrap().into())
            } else if number.is_i64() {
                Some(number.as_i64().unwrap().into())
            } else if number.is_f64() {
                Some(number.as_f64().unwrap().into())
            } else {
                unreachable!();
            }
        }
        serde_json::Value::String(str) => Some(str.into()),
        serde_json::Value::Array(vec) => {
            let mut new = toml_edit::Array::new();
            for value in vec {
                match json_value_to_toml_value(value)? {
                    None => return Err(anyhow!("arrays must not contain null")),
                    Some(value) => new.push(value),
                }
            }
            Some(new.into())
        }
        serde_json::Value::Object(_) => unreachable!(),
    })
}

#[cfg(test)]
mod tests {
    use indoc::indoc;
    use serde_json::json;
    use toml_edit::DocumentMut;

    fn apply(new_value: serde_json::Value, toml: &str) -> anyhow::Result<String> {
        let mut doc = toml.parse::<DocumentMut>().unwrap();
        super::apply(new_value.as_object().unwrap(), doc.as_table_mut())?;
        Ok(doc.to_string())
    }

    #[test]
    fn patch_object() {
        assert_eq!(
            apply(
                json!({"x": {"y": {"foo": true}}}),
                indoc! {r#"
                [x.y]
                # This is x.y.z.
                z = 3
                # End of x.y section.
            "#}
            )
            .unwrap(),
            indoc! {r#"
                [x.y]
                # This is x.y.z.
                z = 3
                foo = true
                # End of x.y section.
        "#}
        );
    }

    #[test]
    fn null() {
        assert_eq!(apply(json!({"x": null}), "x = 0").unwrap(), "");
    }

    #[test]
    fn object() {
        assert_eq!(
            apply(json!({"x": {"y": {"z": true}}}), "x = 0").unwrap(),
            indoc! {r#"
            [x]

            [x.y]
            z = true
        "#}
        );
    }

    #[test]
    fn array_may_not_contain_null() {
        assert_eq!(
            apply(json!({"x": [null]}), "x = 0")
                .unwrap_err()
                .to_string(),
            "arrays must not contain null"
        );
    }

    macro_rules! test_apply {
        ($name:ident, $json_value:expr, $toml_value:expr) => {
            #[test]
            fn $name() {
                assert_eq!(apply(json!({"x": $json_value}), "x = 0").unwrap(), format!("x = {}\n", $toml_value));

                // FUTURE: move these assertions to separate functions once https://github.com/rust-lang/rust/issues/124225 is stable
                assert_eq!(
                    apply(json!({"x": $json_value}), "# before\nx = 0").unwrap(),
                    format!("# before\nx = {}\n", $toml_value)
                );

                assert_eq!(
                    apply(json!({"x": $json_value}), "x = 0\n# after").unwrap(),
                    format!("x = {}\n# after", $toml_value)
                );
            }
        };
    }

    test_apply!(bool, true, "true");
    test_apply!(u64, 1, "1");
    test_apply!(i64, -1, "-1");
    test_apply!(f64, 1.0, "1.0");
    test_apply!(str, "foo", "\"foo\"");
    test_apply!(array, ["foo", "bar"], "[\"foo\", \"bar\"]");
}