kanban_persistence_json/
lib.rs1pub mod atomic_writer;
2pub mod backend_factory;
3pub mod conflict;
4pub mod json_backend;
5pub mod json_file_store;
6pub mod migration;
7
8pub use backend_factory::JsonBackendFactory;
9pub use conflict::FileMetadata;
10pub use json_backend::JsonDataStore;
11pub use json_file_store::{JsonEnvelope, JsonFileStore};
12
13use kanban_persistence::{PersistenceError, PersistenceStore, StoreFactory};
14use std::sync::Arc;
15
16pub struct JsonStoreFactory;
17
18impl StoreFactory for JsonStoreFactory {
19 fn name(&self) -> &str {
20 "json"
21 }
22
23 fn matches_content(&self, header: &[u8]) -> bool {
24 let mut iter = header.iter();
25 if header.starts_with(&[0xEF, 0xBB, 0xBF]) {
27 iter = header[3..].iter();
28 }
29 let first = iter.find(|b| !b.is_ascii_whitespace());
31 matches!(first, Some(b'{') | Some(b'['))
32 }
33
34 fn create(
35 &self,
36 locator: &str,
37 ) -> Result<Arc<dyn PersistenceStore + Send + Sync>, PersistenceError> {
38 Ok(Arc::new(JsonFileStore::new(locator)))
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_matches_content_json_object() {
48 let factory = JsonStoreFactory;
49 assert!(factory.matches_content(b"{\"boards\": []}"));
50 }
51
52 #[test]
53 fn test_matches_content_json_array() {
54 let factory = JsonStoreFactory;
55 assert!(factory.matches_content(b"[1, 2, 3]"));
56 }
57
58 #[test]
59 fn test_matches_content_json_with_bom() {
60 let factory = JsonStoreFactory;
61 let mut data = vec![0xEF, 0xBB, 0xBF]; data.extend_from_slice(b" {\"key\": true}");
63 assert!(factory.matches_content(&data));
64 }
65
66 #[test]
67 fn test_matches_content_not_json() {
68 let factory = JsonStoreFactory;
69 assert!(!factory.matches_content(b"SQLite format 3\0"));
70 assert!(!factory.matches_content(b""));
71 assert!(!factory.matches_content(b"plain text"));
72 assert!(!factory.matches_content(b" \t\n "));
73 }
74}