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