eidetica 0.2.0

Decentralized DB. Remember Everything. Everywhere. All At Once.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
//! Document-level CRDT API.
//!
//! This module provides the main public interface for CRDT documents in Eidetica.
//! The [`Doc`] type serves as the primary entry point for all CRDT operations,
//! providing a clean separation between document-level operations and internal
//! node structure management.
//!
//! # Design Philosophy
//!
//! The `Doc` type addresses the confusion in the original API where `crdt::Map`
//! served dual purposes as both the main document interface and internal node
//! representation. This separation provides:
//!
//! - **Clear API boundaries**: Users interact only with `Doc` for document operations
//! - **Better conceptual model**: Document vs internal node separation
//! - **Future flexibility**: Can optimize internal representation independently
//! - **Easier testing**: Document and node behaviors can be tested separately
//!
//! # Usage
//!
//! ```
//! use eidetica::crdt::{Doc, traits::CRDT};
//! use eidetica::crdt::doc::path;
//!
//! let mut doc = Doc::new();
//! doc.set("name", "Alice");
//! doc.set("age", 30);
//! doc.set_path(path!("user.profile.bio"), "Software developer").unwrap();
//!
//! // Merge with another document
//! let mut doc2 = Doc::new();
//! doc2.set("name", "Bob");
//! doc2.set("city", "New York");
//!
//! let merged = doc.merge(&doc2).unwrap();
//! ```

use std::{collections::HashMap, fmt, str::FromStr};

use crate::crdt::{
    CRDTError,
    traits::{CRDT, Data},
};

// Submodules
pub mod list;
#[cfg(test)]
mod node_tests;
pub mod path;
pub mod value;

// Convenience re-exports for core Doc types
pub use list::List;
pub use path::{Path, PathBuf, PathError};
pub use value::Value;

// Re-export the macro from crate root
pub use crate::path;

/// The main CRDT document type for Eidetica.
///
/// `Doc` provides the primary interface for CRDT document operations,
/// providing a unified tree structure for CRDT operations.
/// This type handles document-level operations like merging, serialization,
/// and high-level data access patterns.
///
/// # Core Operations
///
/// - **Data access**: `get()`, `get_text()`, `get_int()`, etc.
/// - **Data modification**: `set()`, `remove()`, `set_path()`, etc.
/// - **CRDT operations**: `merge()` for conflict-free merging
/// - **Path operations**: Dot-notation access to nested structures
///
/// # Examples
///
/// ## Basic Operations
/// ```
/// # use eidetica::crdt::Doc;
/// let mut doc = Doc::new();
/// doc.set("name", "Alice");
/// doc.set("age", 30);
///
/// assert_eq!(doc.get_as::<&str>("name"), Some("Alice"));
/// assert_eq!(doc.get_as::<i64>("age"), Some(30));
/// ```
///
/// ## Path Operations
/// ```
/// # use eidetica::crdt::Doc;
/// # use eidetica::crdt::doc::path;
/// let mut doc = Doc::new();
/// doc.set("user.profile.name", "Alice");
///
/// assert_eq!(doc.get_as::<&str>("user.profile.name"), Some("Alice"));
/// ```
///
/// ## CRDT Merging
/// ```
/// # use eidetica::crdt::{Doc, traits::CRDT};
/// let mut doc1 = Doc::new();
/// doc1.set("name", "Alice");
/// doc1.set("age", 30);
///
/// let mut doc2 = Doc::new();
/// doc2.set("name", "Bob");
/// doc2.set("city", "NYC");
///
/// let merged = doc1.merge(&doc2).unwrap();
/// assert_eq!(merged.get_as::<&str>("name"), Some("Bob")); // Last write wins
/// assert_eq!(merged.get_as::<i64>("age"), Some(30));        // Preserved from doc1
/// assert_eq!(merged.get_as::<&str>("city"), Some("NYC"));   // Added from doc2
/// ```
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Doc {
    /// Child nodes indexed by string keys
    children: HashMap<String, Value>,
}

impl Doc {
    /// Creates a new empty document
    pub fn new() -> Self {
        Self {
            children: HashMap::new(),
        }
    }

    /// Returns true if this document has no data (excluding tombstones)
    pub fn is_empty(&self) -> bool {
        self.children.values().all(|v| matches!(v, Value::Deleted))
    }

    /// Returns the number of direct keys (excluding tombstones)
    pub fn len(&self) -> usize {
        self.children
            .values()
            .filter(|v| !matches!(v, Value::Deleted))
            .count()
    }

    /// Returns true if the document contains the given key
    pub fn contains_key(&self, key: impl AsRef<Path>) -> bool {
        let path_buf = PathBuf::from_str(key.as_ref().as_str()).unwrap(); // Infallible
        self.get(&path_buf).is_some()
    }

    /// Returns true if the given key contains a tombstone (deleted value).
    ///
    /// This method provides access to CRDT tombstone information for advanced use cases,
    /// testing, and debugging. See [`Doc::is_tombstone`] for detailed documentation.
    pub fn is_tombstone(&self, key: impl AsRef<Path>) -> bool {
        // For path-based access, we need to check the final component
        let path_str = key.as_ref().as_str();
        if path_str.contains('.') {
            // For nested paths, we can't easily check tombstones
            // FIXME: This is a limitation of the current design
            false
        } else {
            // For direct keys, check our children directly
            matches!(self.children.get(path_str), Some(Value::Deleted))
        }
    }

    /// Gets a value by key or path (immutable reference)
    pub fn get(&self, key: impl AsRef<Path>) -> Option<&Value> {
        let path = key.as_ref();
        let segments: Vec<_> = path.components().collect();

        if segments.is_empty() {
            return None;
        }

        // Start with the first segment to get into our structure
        let first_segment = segments.first()?;
        let mut current_value = match self.children.get(*first_segment) {
            Some(Value::Deleted) => return None, // Hide tombstones
            value => value?,
        };

        // Navigate through remaining segments
        for segment in &segments[1..] {
            match current_value {
                Value::Doc(doc) => {
                    current_value = match doc.children.get(*segment) {
                        Some(Value::Deleted) => return None, // Hide tombstones
                        value => value?,
                    };
                }
                Value::List(list) => {
                    // Try to parse segment as list index
                    let index: usize = segment.parse().ok()?;
                    current_value = list.get(index)?;
                }
                _ => return None, // Can't navigate further
            }
        }

        Some(current_value)
    }

    /// Gets a mutable reference to a value by key or path
    pub fn get_mut(&mut self, key: impl AsRef<Path>) -> Option<&mut Value> {
        let path = key.as_ref();
        let segments: Vec<_> = path.components().collect();

        if segments.is_empty() {
            return None;
        }

        let mut current = self;

        // Navigate to the parent of the target
        for segment in &segments[..segments.len() - 1] {
            match current.children.get_mut(*segment) {
                Some(Value::Doc(doc)) => {
                    // Now Value::Doc contains a Doc, so we can navigate
                    current = doc;
                }
                _ => return None, // Can't navigate further
            }
        }

        // Get the final value
        let final_key = segments.last()?;
        match current.children.get_mut(*final_key) {
            Some(Value::Deleted) => None, // Hide tombstones
            value => value,
        }
    }

    /// Gets a value by key with automatic type conversion using TryFrom
    ///
    /// Returns Some(T) if the value exists and can be converted to type T.
    /// Returns None if the key doesn't exist or type conversion fails.
    ///
    /// This is the recommended method for type-safe value retrieval as it provides
    /// a cleaner Option-based interface compared to the Result-based `get_as()` method.
    ///
    /// # Examples
    ///
    /// ```
    /// # use eidetica::crdt::Doc;
    /// let mut doc = Doc::new();
    /// doc.set("name", "Alice");
    /// doc.set("age", 30);
    /// doc.set("active", true);
    ///
    /// // Returns Some when value exists and type matches
    /// assert_eq!(doc.get_as::<&str>("name"), Some("Alice"));
    /// assert_eq!(doc.get_as::<i64>("age"), Some(30));
    /// assert_eq!(doc.get_as::<bool>("active"), Some(true));
    ///
    /// // Returns None when key doesn't exist
    /// assert_eq!(doc.get_as::<String>("missing"), None);
    ///
    /// // Returns None when type doesn't match
    /// assert_eq!(doc.get_as::<i64>("name"), None);
    /// ```
    pub fn get_as<'a, T>(&'a self, key: impl AsRef<Path>) -> Option<T>
    where
        T: TryFrom<&'a Value, Error = CRDTError>,
    {
        let value = self.get(key)?;
        T::try_from(value).ok()
    }

    /// Sets a value at the given key or path, returns the old value if present
    ///
    /// This method automatically creates intermediate nodes for nested paths.
    /// Path creation is always successful due to normalization, but setting
    /// through existing scalar values will fail.
    pub fn set(&mut self, key: impl AsRef<Path>, value: impl Into<Value>) -> Option<Value> {
        let path_str = key.as_ref().as_str();
        let path_buf = PathBuf::from_str(path_str).unwrap(); // Infallible

        // For simple keys (no dots), use direct assignment
        if !path_str.contains('.') {
            let old = self.children.insert(path_str.to_string(), value.into());
            match old {
                Some(Value::Deleted) => None, // Don't return tombstones
                value => value,
            }
        } else {
            // For paths, use set_path - errors should be handled by caller
            // but we return None for this non-Result interface
            // Path operations can fail (e.g., trying to set through scalar)
            // For this Option-returning interface, we return None
            // Use try_set() for Result-based error handling
            self.set_path(&path_buf, value).unwrap_or_default()
        }
    }

    /// Sets a value at the given key or path with Result error handling
    ///
    /// This provides a Result-based interface for cases where you need to
    /// handle path operation errors (e.g., setting through scalar values).
    pub fn try_set(
        &mut self,
        key: impl AsRef<Path>,
        value: impl Into<Value>,
    ) -> crate::Result<Option<Value>> {
        let path_str = key.as_ref().as_str();
        let path_buf = PathBuf::from_str(path_str).unwrap(); // Infallible

        // Check for empty key - this is not allowed
        if path_str.is_empty() {
            return Err(crate::crdt::CRDTError::InvalidPath {
                path: "empty path (not allowed for setting values)".to_string(),
            }
            .into());
        }

        // For simple keys (no dots), use direct assignment
        if !path_str.contains('.') {
            let old = self.children.insert(path_str.to_string(), value.into());
            Ok(match old {
                Some(Value::Deleted) => None, // Don't return tombstones
                value => value,
            })
        } else {
            // For paths, use set_path and return the Result
            self.set_path(&path_buf, value).map_err(Into::into)
        }
    }

    /// Sets a value at a path using dot notation, creating intermediate nodes as needed
    pub fn set_path(
        &mut self,
        path: impl AsRef<Path>,
        value: impl Into<Value>,
    ) -> Result<Option<Value>, CRDTError> {
        let path = path.as_ref();
        let segments: Vec<_> = path.components().collect();

        if segments.is_empty() {
            return Err(CRDTError::InvalidPath {
                path: "(empty path)".to_string(),
            });
        }

        let mut current = self;

        // Navigate to the parent, creating intermediate nodes as needed
        for segment in &segments[..segments.len() - 1] {
            let entry = current
                .children
                .entry(segment.to_string())
                .or_insert_with(|| Value::Doc(Doc::new()));
            match entry {
                Value::Doc(doc) => {
                    current = doc;
                }
                Value::Deleted => {
                    // Replace tombstone with new node
                    *entry = Value::Doc(Doc::new());
                    match entry {
                        Value::Doc(doc) => current = doc,
                        _ => unreachable!(),
                    }
                }
                _ => {
                    // Replace scalar value with new node to allow navigation
                    *entry = Value::Doc(Doc::new());
                    match entry {
                        Value::Doc(doc) => current = doc,
                        _ => unreachable!(),
                    }
                }
            }
        }

        // Set the final value
        let final_key = segments.last().unwrap();
        let old = current.children.insert(final_key.to_string(), value.into());

        Ok(match old {
            Some(Value::Deleted) => None, // Don't return tombstones
            value => value,
        })
    }

    /// Removes a value by key or path, returns the old value if present.
    ///
    /// This method implements CRDT semantics by creating a tombstone marker.
    /// For paths with dots, this removes the value at the nested location.
    pub fn remove(&mut self, key: impl AsRef<Path>) -> Option<Value> {
        let path_str = key.as_ref().as_str();

        // For simple keys (no dots), use direct remove
        if !path_str.contains('.') {
            let key = path_str.to_string();
            let old_value = self.children.get(&key).cloned();
            self.children.insert(key, Value::Deleted);
            match old_value {
                Some(Value::Deleted) => None, // Don't return tombstones
                value => value,
            }
        } else {
            // For paths, get the current value first, then set to tombstone
            let _current = self.get(key)?.clone();
            // FIXME: We can't easily remove nested paths in the current CRDT design
            // This is a limitation we'll need to address in the future
            // For now, we'll just return None for nested paths
            None
        }
    }

    /// Marks a key as deleted by setting it to a tombstone.
    ///
    /// For paths with dots, this is limited to direct keys only.
    pub fn delete(&mut self, key: impl AsRef<Path>) -> bool {
        let path_str = key.as_ref().as_str();

        // For simple keys (no dots), use direct delete
        if !path_str.contains('.') {
            self.remove(path_str).is_some()
        } else {
            // Nested path deletion is not supported in current design
            false
        }
    }

    /// Gets a value by path with automatic type conversion using TryFrom
    ///
    /// Similar to `get_as()` but works with dot-notation paths for nested access.
    ///
    /// # Examples
    ///
    /// ```
    /// # use eidetica::crdt::Doc;
    /// let mut doc = Doc::new();
    /// doc.set("user.profile.name", "Alice");
    /// doc.set("user.profile.age", 30);
    ///
    /// // Type inference with path access
    /// let name = doc.get_as::<String>("user.profile.name");
    /// let age = doc.get_as::<i64>("user.profile.age");
    ///
    /// assert_eq!(name, Some("Alice".to_string()));
    /// assert_eq!(age, Some(30));
    /// ```
    /// Returns an iterator over all key-value pairs (excluding tombstones)
    pub fn iter(&self) -> impl Iterator<Item = (&String, &Value)> {
        self.children
            .iter()
            .filter(|(_, v)| !matches!(v, Value::Deleted))
    }

    /// Returns a mutable iterator over all key-value pairs (excluding tombstones)
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&String, &mut Value)> {
        self.children.iter_mut()
    }

    /// Returns an iterator over all keys (excluding tombstones)
    pub fn keys(&self) -> impl Iterator<Item = &String> {
        self.children
            .iter()
            .filter(|(_, v)| !matches!(v, Value::Deleted))
            .map(|(k, _)| k)
    }

    /// Returns an iterator over all values (excluding tombstones)
    pub fn values(&self) -> impl Iterator<Item = &Value> {
        self.children
            .values()
            .filter(|v| !matches!(v, Value::Deleted))
    }

    /// Returns a mutable iterator over all values (excluding tombstones)
    pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Value> {
        self.children.values_mut()
    }

    /// Clears all data from this document
    pub fn clear(&mut self) {
        let keys: Vec<_> = self.children.keys().cloned().collect();
        for key in keys {
            self.children.insert(key, Value::Deleted);
        }
    }

    /// Converts to a JSON-like string representation for human-readable output.
    ///
    /// See [`Doc::to_json_string`] for detailed documentation.
    pub fn to_json_string(&self) -> String {
        let mut result = String::with_capacity(self.children.len() * 16);
        result.push('{');
        let mut first = true;
        for (key, value) in self.iter() {
            if !first {
                result.push(',');
            }
            result.push_str(&format!("\"{}\":{}", key, value.to_json_string()));
            first = false;
        }
        result.push('}');
        result
    }
}

impl CRDT for Doc {
    /// Merges another document into this one using CRDT semantics.
    ///
    /// This implements the core CRDT merge operation at the document level,
    /// providing deterministic conflict resolution following CRDT semantics.
    ///
    /// Merge combines the key-value pairs from both documents, resolving
    /// conflicts deterministically using CRDT rules.
    fn merge(&self, other: &Self) -> crate::Result<Self> {
        let mut result = self.clone();
        for (key, other_value) in &other.children {
            match result.children.get_mut(key) {
                Some(self_value) => {
                    self_value.merge(other_value);
                }
                None => {
                    result.children.insert(key.clone(), other_value.clone());
                }
            }
        }
        Ok(result)
    }
}

impl Default for Doc {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for Doc {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{{")?;
        let mut first = true;
        for (key, value) in self.iter() {
            if !first {
                write!(f, ", ")?;
            }
            write!(f, "{key}: {value}")?;
            first = false;
        }
        write!(f, "}}")
    }
}

impl FromIterator<(String, Value)> for Doc {
    fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
        let mut doc = Doc::new();
        for (key, value) in iter {
            doc.set(key, value);
        }
        doc
    }
}

// Builder pattern methods
impl Doc {
    /// Builder method to set a value and return self
    pub fn with(mut self, key: impl AsRef<Path>, value: impl Into<Value>) -> Self {
        self.set(key, value);
        self
    }

    /// Builder method to set a boolean value
    pub fn with_bool(self, key: impl AsRef<Path>, value: bool) -> Self {
        self.with(key, Value::Bool(value))
    }

    /// Builder method to set an integer value
    pub fn with_int(self, key: impl AsRef<Path>, value: i64) -> Self {
        self.with(key, Value::Int(value))
    }

    /// Builder method to set a text value
    pub fn with_text(self, key: impl AsRef<Path>, value: impl Into<String>) -> Self {
        self.with(key, Value::Text(value.into()))
    }

    /// Builder method to set a list value
    pub fn with_list(self, key: impl AsRef<Path>, value: impl Into<List>) -> Self {
        self.with(key, Value::List(value.into()))
    }

    /// Builder method to set a nested Doc
    pub fn with_doc(self, key: impl AsRef<Path>, value: impl Into<Doc>) -> Self {
        self.with(key, Value::Doc(value.into()))
    }
}

// Additional methods for compatibility and advanced operations
impl Doc {
    /// Set a key-value pair with a raw Value (for advanced use)
    pub fn set_raw(&mut self, key: impl AsRef<Path>, value: Value) -> &mut Self {
        self.set(key, value);
        self
    }

    /// Set a key-value pair with automatic JSON serialization for any Serialize type
    pub fn set_json<T>(&mut self, key: impl AsRef<Path>, value: T) -> crate::Result<&mut Self>
    where
        T: serde::Serialize,
    {
        let path_str = key.as_ref().as_str();

        // For simple keys (no dots), set as JSON string
        if !path_str.contains('.') {
            let json =
                serde_json::to_string(&value).map_err(|e| CRDTError::SerializationFailed {
                    reason: e.to_string(),
                })?;
            self.set(path_str, Value::Text(json));
        } else {
            // For paths, serialize to JSON string and set as text
            let json =
                serde_json::to_string(&value).map_err(|e| CRDTError::SerializationFailed {
                    reason: e.to_string(),
                })?;
            self.set(key, Value::Text(json));
        }
        Ok(self)
    }

    /// Get a value by key with automatic JSON deserialization for any Deserialize type
    pub fn get_json<T>(&self, key: impl AsRef<Path>) -> crate::Result<T>
    where
        T: for<'de> serde::Deserialize<'de>,
    {
        let path_str = key.as_ref().as_str();

        // For simple keys (no dots), get and deserialize JSON
        if !path_str.contains('.') {
            match self.children.get(path_str) {
                Some(Value::Text(json)) => serde_json::from_str::<T>(json).map_err(|e| {
                    CRDTError::DeserializationFailed {
                        reason: format!("Failed to deserialize JSON for key '{path_str}': {e}"),
                    }
                    .into()
                }),
                Some(Value::Deleted) => Err(CRDTError::ElementNotFound {
                    key: path_str.to_string(),
                }
                .into()),
                Some(other) => Err(CRDTError::TypeMismatch {
                    expected: "Text (JSON string)".to_string(),
                    actual: format!("{other:?}"),
                }
                .into()),
                None => Err(CRDTError::ElementNotFound {
                    key: path_str.to_string(),
                }
                .into()),
            }
        } else {
            // For paths, get the value and try to deserialize as JSON
            let key_ref = key.as_ref();
            let value = self
                .get(key_ref)
                .ok_or_else(|| CRDTError::ElementNotFound {
                    key: path_str.to_string(),
                })?;

            match value {
                Value::Text(json) => serde_json::from_str(json).map_err(|e| {
                    CRDTError::DeserializationFailed {
                        reason: format!("Failed to deserialize JSON for path '{path_str}': {e}"),
                    }
                    .into()
                }),
                _ => Err(CRDTError::TypeMismatch {
                    expected: "JSON string".to_string(),
                    actual: format!("{value:?}"),
                }
                .into()),
            }
        }
    }

    /// Set a key-value pair where the value is a string
    pub fn set_string(&mut self, key: impl AsRef<Path>, value: impl Into<String>) -> &mut Self {
        self.set(key, Value::Text(value.into()));
        self
    }

    /// Set a key-value pair where the value is a nested Doc
    pub fn set_doc(&mut self, key: impl AsRef<Path>, value: Doc) -> &mut Self {
        self.set(key, Value::Doc(value));
        self
    }

    /// Get a reference to a nested Doc by key
    pub fn get_doc(&self, key: impl AsRef<Path>) -> Option<&Doc> {
        match self.get(key)? {
            Value::Doc(node) => Some(node),
            _ => None,
        }
    }

    /// Get a mutable reference to a nested Doc by key
    pub fn get_doc_mut(&mut self, key: impl AsRef<Path>) -> Option<&mut Doc> {
        match self.get_mut(key)? {
            Value::Doc(node) => Some(node),
            _ => None,
        }
    }

    /// Get a reference to the internal HashMap for advanced access
    pub fn as_hashmap(&self) -> &HashMap<String, Value> {
        &self.children
    }

    /// Get a mutable reference to the internal HashMap for advanced access
    pub fn as_hashmap_mut(&mut self) -> &mut HashMap<String, Value> {
        &mut self.children
    }

    /// List operations compatibility methods
    pub fn list_add<K>(&mut self, key: K, value: Value) -> crate::Result<String>
    where
        K: Into<String>,
    {
        let key = key.into();

        // Get or create the list
        let list = match self.children.get_mut(&key) {
            Some(Value::List(list)) => list,
            Some(Value::Deleted) => {
                // Replace tombstone with new list
                let mut new_list = List::new();
                let index = new_list.push(value);
                self.children.insert(key, Value::List(new_list));
                return Ok(index.to_string());
            }
            Some(_) => {
                return Err(CRDTError::TypeMismatch {
                    expected: "List".to_string(),
                    actual: "other type".to_string(),
                }
                .into());
            }
            None => {
                // Create new list
                let mut new_list = List::new();
                let index = new_list.push(value);
                self.children.insert(key, Value::List(new_list));
                return Ok(index.to_string());
            }
        };

        Ok(list.push(value).to_string())
    }

    /// List remove operation - tombstones element by position ID
    pub fn list_remove<K>(&mut self, key: K, id: &str) -> crate::Result<bool>
    where
        K: Into<String> + AsRef<str>,
    {
        let index: usize = id.parse().map_err(|_| CRDTError::InvalidPath {
            path: format!("Invalid list index: {id}"),
        })?;

        match self.children.get_mut(key.as_ref()) {
            Some(Value::List(list)) => Ok(list.remove(index).is_some()),
            Some(_) => Err(CRDTError::TypeMismatch {
                expected: "List".to_string(),
                actual: "other type".to_string(),
            }
            .into()),
            None => Ok(false), // Key doesn't exist
        }
    }

    /// Get an element by its ID from a list
    pub fn list_get<K>(&self, key: K, id: &str) -> Option<&Value>
    where
        K: AsRef<str>,
    {
        let index: usize = id.parse().ok()?;
        match self.children.get(key.as_ref()) {
            Some(Value::List(list)) => list.get(index),
            Some(Value::Deleted) => None, // Hide tombstones
            _ => None,
        }
    }

    /// Get all element IDs from a list in order
    pub fn list_ids<K>(&self, key: K) -> Vec<String>
    where
        K: AsRef<str>,
    {
        match self.children.get(key.as_ref()) {
            Some(Value::List(list)) => {
                // Return index-based IDs as strings
                (0..list.len()).map(|i| i.to_string()).collect()
            }
            _ => Vec::new(),
        }
    }

    /// Get list length (excluding tombstones)
    pub fn list_len<K>(&self, key: K) -> usize
    where
        K: AsRef<str>,
    {
        match self.children.get(key.as_ref()) {
            Some(Value::List(list)) => list.len(),
            _ => 0,
        }
    }

    /// Check if list is empty
    pub fn list_is_empty<K>(&self, key: K) -> bool
    where
        K: AsRef<str>,
    {
        match self.children.get(key.as_ref()) {
            Some(Value::List(list)) => list.is_empty(),
            _ => true, // Non-existent lists are considered empty
        }
    }

    /// Clear list
    pub fn list_clear<K>(&mut self, key: K) -> crate::Result<()>
    where
        K: AsRef<str>,
    {
        match self.children.get_mut(key.as_ref()) {
            Some(Value::List(list)) => {
                list.clear();
                Ok(())
            }
            Some(_) => Err(CRDTError::TypeMismatch {
                expected: "List".to_string(),
                actual: "other type".to_string(),
            }
            .into()),
            None => Ok(()), // Nothing to clear
        }
    }

    /// Convenience methods for ergonomic access with better error handling
    ///
    /// These methods provide cleaner syntax for common CRDT operations.
    ///
    /// Mutable access methods for working with Values directly
    ///
    /// These methods provide cleaner access to mutable Values for in-place modification.
    ///
    /// Gets or inserts a value with a default, returns a mutable reference
    ///
    /// # Examples
    ///
    /// ```
    /// # use eidetica::crdt::Doc;
    /// # use eidetica::crdt::doc::Value;
    /// let mut doc = Doc::new();
    ///
    /// // Key doesn't exist - will insert default
    /// doc.get_or_insert("counter", 0);
    /// assert_eq!(doc.get_as::<i64>("counter").unwrap(), 0);
    ///
    /// // Key exists - will keep existing value
    /// doc.set("counter", 5);
    /// doc.get_or_insert("counter", 100);
    /// assert_eq!(doc.get_as::<i64>("counter").unwrap(), 5);
    /// ```
    pub fn get_or_insert(
        &mut self,
        key: impl AsRef<Path> + Clone,
        default: impl Into<Value>,
    ) -> &mut Value {
        if !self.contains_key(key.clone()) {
            self.set(key.clone(), default);
        }
        self.get_mut(key).expect("Key should exist after insert")
    }

    /// Modifies a value in-place using a closure
    ///
    /// If the key exists and can be converted to type T, the closure is called
    /// with a mutable reference to the typed value. After the closure returns,
    /// the modified value is converted back and stored.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The key doesn't exist (`CRDTError::ElementNotFound`)
    /// - The value cannot be converted to type T (`CRDTError::TypeMismatch`)
    ///
    /// # Examples
    ///
    /// ```
    /// # use eidetica::crdt::Doc;
    /// let mut doc = Doc::new();
    /// doc.set("count", 5);
    /// doc.set("text", "hello");
    ///
    /// // Modify counter
    /// doc.modify::<i64, _>("count", |count| {
    ///     *count += 10;
    /// })?;
    /// assert_eq!(doc.get_as::<i64>("count"), Some(15));
    ///
    /// // Modify string
    /// doc.modify::<String, _>("text", |text| {
    ///     text.push_str(" world");
    /// })?;
    /// assert_eq!(doc.get_as::<String>("text"), Some("hello world".to_string()));
    /// # Ok::<(), eidetica::Error>(())
    /// ```
    pub fn modify<T, F>(&mut self, key: impl AsRef<Path> + Clone, f: F) -> crate::Result<()>
    where
        T: for<'a> TryFrom<&'a Value, Error = CRDTError> + Into<Value>,
        F: FnOnce(&mut T),
    {
        // Try to get and convert the current value
        let mut value = self.get_as::<T>(key.clone()).ok_or_else(|| {
            crate::Error::CRDT(CRDTError::ElementNotFound {
                key: key.as_ref().as_str().to_string(),
            })
        })?;

        // Apply the modification
        f(&mut value);

        // Store the modified value back
        self.set(key, value);
        Ok(())
    }
}

// Conversion implementations
// Node is now an alias for Doc, so no conversion implementations needed
// The standard From<T> for T implementation in core handles this automatically

// Data trait implementation
impl Data for Doc {}