agentmux_srv_core/registry/
unreadable.rs1use serde::Serialize;
14use serde_json::Value;
15
16#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
18pub struct UnreadableRecord {
19 pub id: String,
21 pub name: String,
23 pub provider: String,
25 pub schema_version: Option<u64>,
27 pub reason: String,
29}
30
31pub(super) struct Shape {
33 pub name_key: &'static str,
34 pub provider_key: Option<&'static str>,
35 pub version_keys: &'static [&'static str],
37 pub min: u32,
38 pub max: u32,
39}
40
41pub(super) fn explain(bytes: &[u8], stem: &str, shape: &Shape, why_not: &str) -> UnreadableRecord {
44 let mut out = UnreadableRecord { id: stem.to_string(), name: String::new(), provider: String::new(), schema_version: None, reason: String::new() };
45 let value: Value = match serde_json::from_slice(bytes) {
46 Ok(v) => v,
47 Err(e) => {
48 out.reason = format!("its record file is damaged and could not be read ({e}); it is left as it is");
49 return out;
50 }
51 };
52 let data = value.get("data");
53 let text = |key: &str| data.and_then(|d| d.get(key)).and_then(Value::as_str).unwrap_or_default().to_string();
54 out.name = text(shape.name_key);
55 if let Some(key) = shape.provider_key {
56 out.provider = text(key);
57 }
58 out.schema_version = value.get("schema_version").and_then(Value::as_u64);
59 out.reason = match out.schema_version {
60 Some(v) if v > u64::from(shape.max) => {
61 let by = shape.version_keys.iter().map(|k| text(k)).find(|s| !s.is_empty());
62 match by {
63 Some(version) => format!("made by a newer AgentMux, version {version} (record format {v}; this version reads up to {})", shape.max),
64 None => format!("made by a newer AgentMux (record format {v}; this version reads up to {})", shape.max),
65 }
66 }
67 Some(v) if v < u64::from(shape.min) => format!("made by an older AgentMux whose record format ({v}) this version no longer reads"),
68 Some(_) => format!("its record is incomplete or does not make sense to this version ({why_not})"),
69 None => "its record says nothing of its format, so this version does not touch it".to_string(),
70 };
71 out
72}
73
74#[cfg(test)]
75#[path = "unreadable_tests.rs"]
76mod tests;