//! One file's diff, as the web renderer's DiffNode read it: its header, and //! its rows, each read into what it is (a line added, removed or of //! context, a hunk's header, a note) by the view's `kinds`, with its line //! numbers, so a client draws them as they come. use super::*; /// The kinds a row may be, besides context: what `kinds` names each. const KINDS: [&str; 4] = ["added", "removed", "hunk", "note"]; /// `Diff`: `p: { path, from?, status?, added?, removed?, note?, rows?, /// digits? }`: `from` only where it is not `path`; `rows` (none for the /// header alone) each `{ kind, old?, new?, text }`, `kind` added, removed, /// hunk, note or context, `old` and `new` the line numbers as text; /// `digits` the widest of those, at least 1, for the gutters. pub(super) fn diff(p: &Value, scope: &Value) -> R> { let Some(path) = required("path", &p["path"], scope, present)?.as_str() else { return bad("path: not text"); }; let mut o = Map::new(); o.insert("path".into(), path.into()); if let Some(from) = text_of(&p["from"], scope)?.filter(|f| f != path) { o.insert("from".into(), from.into()); } if let Some(status) = text_of(&p["status"], scope)? { o.insert("status".into(), status.into()); } for key in ["added", "removed"] { if let Some(n) = value(&p[key], scope, Value::is_number)? { o.insert(key.into(), n.clone()); } } if let Some(note) = text_of(&p["note"], scope)? { o.insert("note".into(), note.into()); } let found = value(&p["rows"], scope, Value::is_array)?.and_then(Value::as_array); let rows: Vec = found .into_iter() .flatten() .filter(|r| r.is_object()) .map(|r| row(r, &p["kinds"])) .collect(); if !rows.is_empty() { let digits = rows .iter() .flat_map(|r| [&r["old"], &r["new"]]) .filter_map(Value::as_str) .map(|n| n.chars().count()) .fold(1, usize::max); o.insert("rows".into(), Value::Array(rows)); o.insert("digits".into(), digits.into()); } Ok(Some(make("Diff", o, vec![]))) } /// A row: its kind by what `kinds` names it (context where it names none), /// its line numbers and text as text, those that are none left out. fn row(r: &Value, kinds: &Value) -> Value { let k = &r["kind"]; let kind = KINDS .into_iter() .find(|name| !k.is_null() && kinds[*name] == *k) .unwrap_or("context"); let field = |key: &str| { let v = &r[key]; if v.is_string() || v.is_number() { write(v) } else { String::new() } }; let mut o = Map::new(); o.insert("kind".into(), kind.into()); for key in ["old", "new"] { let n = field(key); if !n.is_empty() { o.insert(key.into(), n.into()); } } o.insert("text".into(), field("text").into()); Value::Object(o) } #[cfg(test)] mod tests { use super::*; fn drawn(props: Value, scope: Value) -> Value { let node = diff(&props, &scope).ok().flatten().unwrap(); check::checked(&node); node["p"].clone() } #[test] fn rows_take_their_kind_from_the_view_and_any_other_is_context() { let p = json!({ "path": { "$": "new" }, "rows": { "$": "rows" }, "kinds": { "added": "a", "removed": "d", "hunk": "h", "note": "x" } }); let scope = json!({ "new": "a.js", "rows": [ { "kind": "h", "line": false, "text": "@@ -9,2 +9,3 @@" }, { "kind": "", "old": 9, "new": 9, "text": " keep" }, { "kind": "a", "new": 10, "text": "" }, { "kind": "d", "old": 10, "text": "gone" }, { "kind": "q", "old": "99", "new": 100, "text": "odd" }, { "old": 1, "new": 1, "text": { "not": "text" } }, "not a row", ] }); assert_eq!( drawn(p, scope), json!({ "path": "a.js", "digits": 3, "rows": [ { "kind": "hunk", "text": "@@ -9,2 +9,3 @@" }, { "kind": "context", "old": "9", "new": "9", "text": " keep" }, { "kind": "added", "new": "10", "text": "" }, { "kind": "removed", "old": "10", "text": "gone" }, { "kind": "context", "old": "99", "new": "100", "text": "odd" }, { "kind": "context", "old": "1", "new": "1", "text": "" }, ] }) ); } #[test] fn without_kinds_every_row_is_context() { let p = json!({ "path": "a.js", "rows": [{ "kind": "a", "text": "x" }] }); assert_eq!(drawn(p, json!({}))["rows"][0]["kind"], "context"); } #[test] fn a_header_alone_names_where_it_was_renamed_from_only_when_it_moved() { let p = json!({ "path": { "$": "new" }, "from": { "$": "old" }, "added": { "$": "added" }, "rows": { "$": "rows" } }); let same = drawn( p.clone(), json!({ "new": "a.js", "old": "a.js", "added": 3, "rows": [] }), ); assert_eq!(same, json!({ "path": "a.js", "added": 3 })); let moved = drawn(p, json!({ "new": "b.js", "old": "a.js" })); assert_eq!(moved["from"], "a.js"); } #[test] fn a_header_says_its_status_and_note() { let p = json!({ "path": "a.bin", "status": { "$": "status" }, "note": { "$": "note" } }); let scope = json!({ "status": "modified", "note": "Binary file" }); assert_eq!( drawn(p, scope), json!({ "path": "a.bin", "status": "modified", "note": "Binary file" }) ); } #[test] fn a_path_that_is_not_text_is_an_error() { let layout = json!({ "component": "Diff", "props": { "path": { "$": "path" } } }); let root = json!({}); let ctx = Ctx { origins: &[], thumbnails: vec![], trusted: true, root: &root, report: None, inline: None, lexicon: None, handles: None, paged: Vec::new(), more: More::default(), me: None, people: None, }; let error = resolve(&layout, &json!({ "path": 7 }), &ctx).unwrap(); check::checked(&error); assert_eq!( error, json!({ "c": "Error", "p": { "component": "Diff", "message": "path: not text" } }) ); assert!(resolve(&layout, &json!({}), &ctx).is_none()); } }