rspyts-cli 0.2.0

The rspyts code generator: emits pydantic models, TypeScript types, and JSON Schema from a bridged Rust crate.
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Conservative compatibility comparison for two complete manifests.
//!
//! The policy deliberately has only one safe additive unit: a whole new
//! top-level declaration. Once a declaration exists, any non-documentation
//! change to it is breaking because generated consumers may depend on its
//! exact closed wire shape.

use anyhow::{Context, Result};
use rspyts_core::ir::{Manifest, TypeDecl};
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    Breaking,
    Additive,
    Informational,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Breaking => "BREAKING",
            Self::Additive => "ADDITIVE",
            Self::Informational => "INFORMATIONAL",
        })
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Change {
    pub severity: Severity,
    pub path: String,
    pub message: String,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Report {
    pub changes: Vec<Change>,
}

impl Report {
    pub fn has_breaking(&self) -> bool {
        self.changes
            .iter()
            .any(|change| change.severity == Severity::Breaking)
    }

    pub fn has_changes(&self) -> bool {
        !self.changes.is_empty()
    }

    pub fn count(&self, severity: Severity) -> usize {
        self.changes
            .iter()
            .filter(|change| change.severity == severity)
            .count()
    }

    pub fn render(&self) -> String {
        if self.changes.is_empty() {
            return "no manifest changes\n".to_string();
        }
        let mut output = String::new();
        for change in &self.changes {
            output.push_str(&format!(
                "{} {}: {}\n",
                change.severity, change.path, change.message
            ));
        }
        output.push_str(&format!(
            "summary: {} breaking, {} additive, {} informational\n",
            self.count(Severity::Breaking),
            self.count(Severity::Additive),
            self.count(Severity::Informational)
        ));
        output
    }
}

pub fn read_manifest(path: &Path) -> Result<Manifest> {
    let bytes = std::fs::read(path)
        .with_context(|| format!("cannot read manifest `{}`", path.display()))?;
    serde_json::from_slice(&bytes)
        .with_context(|| format!("`{}` is not a valid rspyts manifest", path.display()))
}

pub fn compare(old: &Manifest, new: &Manifest) -> Report {
    let mut report = Report::default();

    // ABI is deliberately first even when many declarations also changed.
    if old.abi != new.abi {
        report.changes.push(Change {
            severity: Severity::Breaking,
            path: "abi".into(),
            message: format!("changed from `{}` to `{}`", old.abi, new.abi),
        });
    }
    if old.crate_name != new.crate_name {
        report.changes.push(Change {
            severity: Severity::Breaking,
            path: "crateName".into(),
            message: format!("changed from `{}` to `{}`", old.crate_name, new.crate_name),
        });
    }
    if old.crate_version != new.crate_version {
        report.changes.push(Change {
            severity: Severity::Informational,
            path: "crateVersion".into(),
            message: format!(
                "changed from `{}` to `{}`",
                old.crate_version, new.crate_version
            ),
        });
    }

    compare_declarations(&mut report, "type", &old.types, &new.types, TypeDecl::name);
    compare_declarations(
        &mut report,
        "constant",
        &old.constants,
        &new.constants,
        |decl| &decl.name,
    );
    compare_declarations(
        &mut report,
        "function",
        &old.functions,
        &new.functions,
        |decl| &decl.name,
    );
    compare_declarations(&mut report, "class", &old.classes, &new.classes, |decl| {
        &decl.name
    });

    report
}

fn compare_declarations<T, F>(report: &mut Report, kind: &str, old: &[T], new: &[T], name: F)
where
    T: Serialize,
    F: Fn(&T) -> &str,
{
    let old_by_name: BTreeMap<&str, &T> = old.iter().map(|decl| (name(decl), decl)).collect();
    let new_by_name: BTreeMap<&str, &T> = new.iter().map(|decl| (name(decl), decl)).collect();

    for (decl_name, old_decl) in &old_by_name {
        let path = format!("{kind}.{decl_name}");
        let Some(new_decl) = new_by_name.get(decl_name) else {
            report.changes.push(Change {
                severity: Severity::Breaking,
                path,
                message: "removed".into(),
            });
            continue;
        };

        let old_value = serde_json::to_value(old_decl).expect("manifest declarations serialize");
        let new_value = serde_json::to_value(new_decl).expect("manifest declarations serialize");
        if old_value == new_value {
            continue;
        }
        if without_docs(old_value) == without_docs(new_value) {
            report.changes.push(Change {
                severity: Severity::Informational,
                path,
                message: "documentation changed".into(),
            });
        } else {
            report.changes.push(Change {
                severity: Severity::Breaking,
                path,
                message: "declaration changed".into(),
            });
        }
    }

    for decl_name in new_by_name.keys() {
        if !old_by_name.contains_key(decl_name) {
            report.changes.push(Change {
                severity: Severity::Additive,
                path: format!("{kind}.{decl_name}"),
                message: "added".into(),
            });
        }
    }
}

fn without_docs(mut value: Value) -> Value {
    remove_docs(&mut value);
    value
}

fn remove_docs(value: &mut Value) {
    match value {
        Value::Object(object) => {
            object.remove("docs");
            for child in object.values_mut() {
                remove_docs(child);
            }
        }
        Value::Array(array) => {
            for child in array {
                remove_docs(child);
            }
        }
        _ => {}
    }
}

pub fn exit_code(report: &Report, fail_on_any: bool) -> u8 {
    u8::from(report.has_breaking() || (fail_on_any && report.has_changes()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn manifest_value() -> Value {
        json!({
            "abi": "2.0",
            "crateName": "demo",
            "crateVersion": "1.0.0",
            "types": [
                {"kind": "struct", "name": "Item", "docs": "item docs", "origin": "demo", "fields": [
                    {"name": "value", "wireName": "value", "docs": "field docs", "ty": {"kind": "u32"}, "optional": false}
                ]},
                {"kind": "enum", "name": "Choice", "docs": "", "origin": "demo", "tag": "type", "variants": [
                    {"name": "One", "wireName": "one", "docs": "", "fields": []}
                ]},
                {"kind": "errorEnum", "name": "Failure", "docs": "", "origin": "demo", "variants": [
                    {"name": "Bad", "wireCode": "bad", "docs": "", "fields": []}
                ]}
            ],
            "constants": [
                {"name": "LIMIT", "docs": "", "origin": "demo", "ty": {"kind": "u32"}, "value": 3}
            ],
            "functions": [
                {"name": "run", "docs": "", "params": [
                    {"name": "item", "wireName": "item", "ty": {"kind": "ref", "name": "Item"}}
                ], "ret": {"kind": "unit"}, "err": "Failure", "targets": ["python", "typescript"]}
            ],
            "classes": [
                {"name": "Session", "docs": "", "constructor": {"docs": "", "params": [], "err": null},
                 "methods": [{"name": "close", "docs": "", "mutable": true, "params": [], "ret": {"kind": "unit"}, "err": null, "targets": ["python", "typescript"]}],
                 "statics": [{"name": "open", "docs": "", "params": [], "ret": {"kind": "unit"}, "err": null, "returnsSelf": true, "targets": ["python", "typescript"]}]}
            ]
        })
    }

    fn manifest(value: Value) -> Manifest {
        serde_json::from_value(value).unwrap()
    }

    fn severity_after(pointer: &str, replacement: Value) -> Severity {
        let old = manifest_value();
        let mut new = old.clone();
        *new.pointer_mut(pointer).unwrap() = replacement;
        compare(&manifest(old), &manifest(new)).changes[0].severity
    }

    #[test]
    fn identical_manifests_have_stable_empty_output() {
        let manifest = manifest(manifest_value());
        let report = compare(&manifest, &manifest);
        assert_eq!(report.render(), "no manifest changes\n");
        assert!(!report.has_breaking());
        assert!(!report.has_changes());
    }

    #[test]
    fn every_inside_declaration_rule_is_breaking() {
        let cases = [
            ("/types/0/fields/0/wireName", json!("wire_value")),
            ("/types/0/fields/0/ty/kind", json!("i32")),
            ("/types/0/fields/0/optional", json!(true)),
            ("/types/1/tag", json!("kind")),
            ("/types/1/variants/0/wireName", json!("first")),
            ("/types/2/variants/0/wireCode", json!("veryBad")),
            ("/constants/0/value", json!(4)),
            ("/functions/0/params/0/wireName", json!("input")),
            ("/functions/0/ret/kind", json!("bool")),
            ("/classes/0/methods/0/mutable", json!(false)),
            ("/classes/0/statics/0/returnsSelf", json!(false)),
        ];
        for (pointer, replacement) in cases {
            assert_eq!(
                severity_after(pointer, replacement),
                Severity::Breaking,
                "{pointer}"
            );
        }
    }

    #[test]
    fn added_or_removed_nested_items_are_breaking() {
        for pointer in [
            "/types/0/fields",
            "/types/1/variants",
            "/functions/0/params",
            "/classes/0/methods",
            "/classes/0/statics",
        ] {
            let old = manifest_value();
            let mut new = old.clone();
            new.pointer_mut(pointer)
                .unwrap()
                .as_array_mut()
                .unwrap()
                .clear();
            assert!(
                compare(&manifest(old), &manifest(new)).has_breaking(),
                "{pointer}"
            );
        }
    }

    #[test]
    fn only_whole_new_top_level_declarations_are_additive() {
        let old = manifest_value();
        let mut new = old.clone();
        new["types"].as_array_mut().unwrap().push(json!({
            "kind": "stringEnum", "name": "Mode", "docs": "", "origin": "demo", "variants": []
        }));
        new["constants"].as_array_mut().unwrap().push(json!({
            "name": "ENABLED", "docs": "", "origin": "demo", "ty": {"kind": "bool"}, "value": true
        }));
        new["functions"].as_array_mut().unwrap().push(json!({
            "name": "ping", "docs": "", "params": [], "ret": {"kind": "unit"}, "err": null, "targets": ["python", "typescript"]
        }));
        new["classes"].as_array_mut().unwrap().push(json!({
            "name": "Clock", "docs": "", "constructor": null, "methods": [], "statics": []
        }));
        let report = compare(&manifest(old), &manifest(new));
        assert_eq!(report.count(Severity::Additive), 4);
        assert_eq!(report.count(Severity::Breaking), 0);
        assert_eq!(
            report
                .changes
                .iter()
                .map(|change| change.path.as_str())
                .collect::<Vec<_>>(),
            [
                "type.Mode",
                "constant.ENABLED",
                "function.ping",
                "class.Clock"
            ]
        );
    }

    #[test]
    fn removals_are_breaking_and_sorted_by_name() {
        let old = manifest_value();
        let mut new = old.clone();
        new["types"] = json!([]);
        let report = compare(&manifest(old), &manifest(new));
        assert_eq!(report.count(Severity::Breaking), 3);
        assert_eq!(
            report
                .changes
                .iter()
                .map(|change| change.path.as_str())
                .collect::<Vec<_>>(),
            ["type.Choice", "type.Failure", "type.Item"]
        );
    }

    #[test]
    fn docs_are_informational_even_when_nested_and_version_is_informational() {
        let old = manifest_value();
        let mut new = old.clone();
        new["crateVersion"] = json!("1.1.0");
        new["types"][0]["fields"][0]["docs"] = json!("better field docs");
        let report = compare(&manifest(old), &manifest(new));
        assert_eq!(report.count(Severity::Informational), 2);
        assert_eq!(report.count(Severity::Breaking), 0);
    }

    #[test]
    fn abi_is_reported_first_and_crate_identity_is_breaking() {
        let old = manifest_value();
        let mut new = old.clone();
        new["abi"] = json!("3.0");
        new["crateName"] = json!("renamed");
        new["types"] = json!([]);
        let report = compare(&manifest(old), &manifest(new));
        assert_eq!(report.changes[0].path, "abi");
        assert_eq!(report.changes[1].path, "crateName");
        assert!(report.has_breaking());
    }

    #[test]
    fn fail_policy_exit_codes_are_ci_friendly() {
        let none = Report::default();
        assert_eq!(exit_code(&none, false), 0);
        assert_eq!(exit_code(&none, true), 0);
        let additive = Report {
            changes: vec![Change {
                severity: Severity::Additive,
                path: "function.ping".into(),
                message: "added".into(),
            }],
        };
        assert_eq!(exit_code(&additive, false), 0);
        assert_eq!(exit_code(&additive, true), 1);
        let breaking = Report {
            changes: vec![Change {
                severity: Severity::Breaking,
                path: "abi".into(),
                message: "changed".into(),
            }],
        };
        assert_eq!(exit_code(&breaking, false), 1);
        assert_eq!(exit_code(&breaking, true), 1);
    }

    fn exit_code(report: &Report, fail_on_any: bool) -> u8 {
        super::exit_code(report, fail_on_any)
    }
}