soothfast_docs/
lockfile.rs1use std::collections::BTreeMap;
6use std::path::Path;
7
8use serde_json::{Value, json};
9
10pub const LOCKFILE: &str = "soothfast.lock";
12
13pub type Binds = BTreeMap<String, String>;
15
16pub fn read(root: &Path) -> Result<Binds, String> {
19 let path = root.join(LOCKFILE);
20 if !path.exists() {
21 return Ok(Binds::new());
22 }
23 let text = std::fs::read_to_string(&path).map_err(|e| e.to_string())?;
24 let doc: Value = serde_json::from_str(&text).map_err(|e| e.to_string())?;
25 let mut binds = Binds::new();
26 if let Some(map) = doc["binds"].as_object() {
27 for (k, v) in map {
28 if let Some(fp) = v.as_str() {
29 binds.insert(k.clone(), fp.to_string());
30 }
31 }
32 }
33 Ok(binds)
34}
35
36pub fn write(root: &Path, binds: &Binds) -> Result<(), String> {
38 let doc = json!({ "version": 1, "binds": binds });
39 let text = serde_json::to_string_pretty(&doc).map_err(|e| e.to_string())?;
40 std::fs::write(root.join(LOCKFILE), text + "\n").map_err(|e| e.to_string())
41}
42
43pub fn merge(existing: &Binds, fresh: &Binds, full_scope: bool) -> Binds {
47 if full_scope {
48 fresh.clone()
49 } else {
50 let mut merged = existing.clone();
51 merged.extend(fresh.iter().map(|(k, v)| (k.clone(), v.clone())));
52 merged
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 fn binds(pairs: &[(&str, &str)]) -> Binds {
61 pairs
62 .iter()
63 .map(|(k, v)| (k.to_string(), v.to_string()))
64 .collect()
65 }
66
67 #[test]
68 fn partial_scope_merge_preserves_out_of_scope_entries() {
69 let existing = binds(&[("a::x", "1111"), ("b::y", "2222")]);
70 let fresh = binds(&[("a::x", "9999")]);
71 let merged = merge(&existing, &fresh, false);
72 assert_eq!(merged.get("a::x").map(String::as_str), Some("9999"));
73 assert_eq!(merged.get("b::y").map(String::as_str), Some("2222"));
74 assert_eq!(merged.len(), 2);
75 }
76
77 #[test]
78 fn full_scope_merge_drops_binds_that_no_longer_exist() {
79 let existing = binds(&[("a::x", "1111"), ("gone::z", "2222")]);
80 let fresh = binds(&[("a::x", "9999")]);
81 assert_eq!(merge(&existing, &fresh, true), fresh);
82 }
83}