diff --git a/Cargo.lock b/Cargo.lock index c79c8e4..ce998dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1180,6 +1180,7 @@ dependencies = [ "reqwest", "serde", "serde_json", + "sha2", "thiserror 2.0.17", "toml", ] diff --git a/mlf-cli/Cargo.toml b/mlf-cli/Cargo.toml index be1bec7..6219b41 100644 --- a/mlf-cli/Cargo.toml +++ b/mlf-cli/Cargo.toml @@ -23,6 +23,7 @@ toml = "0.8" reqwest = { version = "0.12", features = ["blocking", "json"] } chrono = { version = "0.4", features = ["serde"] } hickory-resolver = "0.24" +sha2 = "0.10" # Optional code generator plugins mlf-codegen-typescript = { path = "../codegen-plugins/mlf-codegen-typescript", optional = true } diff --git a/mlf-cli/src/fetch.rs b/mlf-cli/src/fetch.rs index 86175e2..a82ca0b 100644 --- a/mlf-cli/src/fetch.rs +++ b/mlf-cli/src/fetch.rs @@ -4,6 +4,7 @@ use hickory_resolver::config::*; use hickory_resolver::Resolver; use miette::Diagnostic; use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; use std::collections::HashMap; use std::path::Path; use thiserror::Error; @@ -62,6 +63,8 @@ pub struct CacheEntry { pub nsid: String, pub fetched_at: DateTime, pub did: String, + #[serde(default)] + pub hash: String, } impl LexiconCache { @@ -83,13 +86,14 @@ impl LexiconCache { Ok(()) } - pub fn add_entry(&mut self, nsid: String, did: String) { + pub fn add_entry(&mut self, nsid: String, did: String, hash: String) { self.lexicons.insert( nsid.clone(), CacheEntry { nsid, fetched_at: Utc::now(), did, + hash, }, ); } @@ -296,8 +300,11 @@ pub fn fetch_lexicon(nsid: &str, project_root: &std::path::Path) -> Result<(), F std::fs::write(&mlf_path, mlf_content)?; println!(" → Converted to MLF at {}", mlf_path.display()); + // Calculate hash of JSON content + let hash = calculate_hash(&json_str); + // Update cache - cache.add_entry(record_nsid.clone(), did.clone()); + cache.add_entry(record_nsid.clone(), did.clone(), hash); } // Save cache @@ -459,3 +466,10 @@ fn extract_nsid_from_record(record: &AtProtoRecord) -> Result String { + let mut hasher = Sha256::new(); + hasher.update(content.as_bytes()); + format!("{:x}", hasher.finalize()) +}