weavegraph 0.3.0

Graph-driven, concurrent agent workflow framework with versioned state, deterministic barrier merges, and rich diagnostics.
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
//! Collection utilities and common patterns for the Weavegraph framework.
//!
//! This module provides type-safe, ergonomic utilities for working with
//! common collection patterns throughout the codebase, particularly for
//! extra data maps, state snapshots, and channel operations.

use rustc_hash::FxHashMap;
use serde_json::Value;
use std::collections::HashMap;
use thiserror::Error;

/// Errors that can occur during collection operations.
#[derive(Debug, Error)]
#[cfg_attr(feature = "diagnostics", derive(miette::Diagnostic))]
pub enum CollectionError {
    /// Attempted to access a key that doesn't exist.
    #[error("Key '{key}' not found in collection")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(code(weavegraph::collections::missing_key))
    )]
    MissingKey {
        /// The key that was not found
        key: String,
    },

    /// Invalid type conversion during value extraction.
    #[error("Invalid type conversion for key '{key}': expected {expected}, found {found}")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(code(weavegraph::collections::type_mismatch))
    )]
    TypeMismatch {
        /// The key where the type mismatch occurred
        key: String,
        /// The expected type as a string
        expected: String,
        /// The actual type that was found
        found: String,
    },

    /// JSON serialization/deserialization error.
    #[error("JSON operation failed: {source}")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(code(weavegraph::collections::json))
    )]
    Json {
        /// The underlying JSON error
        #[from]
        source: serde_json::Error,
    },
}

/// Creates a new `FxHashMap` for string keys and JSON values.
///
/// This is the standard pattern throughout the codebase for extra data storage.
/// Uses `FxHashMap` for better performance with string keys.
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::new_extra_map;
/// use serde_json::json;
///
/// let mut extra = new_extra_map();
/// extra.insert("key".to_string(), json!("value"));
/// extra.insert("count".to_string(), json!(42));
/// ```
#[must_use]
#[inline]
pub fn new_extra_map() -> FxHashMap<String, Value> {
    FxHashMap::default()
}

/// Creates a new `FxHashMap` with the specified capacity.
///
/// Useful when you know the approximate size of the map ahead of time
/// to avoid reallocations during insertion.
///
/// # Parameters
/// * `capacity` - Initial capacity hint for the map
///
/// # Returns
/// A new `FxHashMap` with the specified capacity.
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::new_extra_map_with_capacity;
///
/// // Pre-allocate for known size
/// let mut extra = new_extra_map_with_capacity(10);
/// ```
#[must_use]
#[inline]
pub fn new_extra_map_with_capacity(capacity: usize) -> FxHashMap<String, Value> {
    FxHashMap::with_capacity_and_hasher(capacity, Default::default())
}

/// Creates a new `FxHashMap` from key-value pairs.
///
/// Convenience function for creating extra maps with initial data.
///
/// # Parameters
/// * `pairs` - Iterator of (key, value) pairs
///
/// # Returns
/// A new `FxHashMap` populated with the given pairs.
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::extra_map_from_pairs;
/// use serde_json::json;
///
/// let extra = extra_map_from_pairs([
///     ("name", json!("test")),
///     ("count", json!(42)),
/// ]);
/// ```
#[must_use]
pub fn extra_map_from_pairs<I, K, V>(pairs: I) -> FxHashMap<String, Value>
where
    I: IntoIterator<Item = (K, V)>,
    K: Into<String>,
    V: Into<Value>,
{
    pairs
        .into_iter()
        .map(|(k, v)| (k.into(), v.into()))
        .collect()
}

/// Merges multiple extra maps into one, with later maps overriding earlier ones.
///
/// This is useful for combining extra data from multiple sources in a
/// predictable way.
///
/// # Parameters
/// * `maps` - Iterator of maps to merge
///
/// # Returns
/// A new map containing all entries, with conflicts resolved by last-wins.
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::{new_extra_map, merge_extra_maps};
/// use serde_json::json;
///
/// let mut map1 = new_extra_map();
/// map1.insert("a".into(), json!(1));
/// map1.insert("b".into(), json!(2));
///
/// let mut map2 = new_extra_map();
/// map2.insert("b".into(), json!(3)); // Overrides map1
/// map2.insert("c".into(), json!(4));
///
/// let merged = merge_extra_maps([&map1, &map2]);
/// assert_eq!(merged["b"], json!(3)); // Last wins
/// ```
#[must_use]
pub fn merge_extra_maps<'a, I>(maps: I) -> FxHashMap<String, Value>
where
    I: IntoIterator<Item = &'a FxHashMap<String, Value>>,
{
    let mut result = new_extra_map();
    for map in maps {
        result.extend(map.iter().map(|(k, v)| (k.clone(), v.clone())));
    }
    result
}

/// Extension trait for working with extra data maps in a type-safe manner.
///
/// This trait provides convenient methods for inserting and retrieving typed values
/// from JSON-based extra data maps. It's designed specifically for the common pattern
/// of storing heterogeneous data in `FxHashMap<String, Value>` structures throughout
/// the Weavegraph framework.
///
/// # Design Principles
///
/// - **Type Safety**: Methods ensure type correctness at compile time where possible
/// - **Error Handling**: Retrieval methods return `Result` for proper error handling
/// - **Ergonomics**: Convenient insertion methods that accept `Into` traits
/// - **JSON Compatibility**: Full support for JSON serialization/deserialization
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
///
/// let mut extra = new_extra_map();
///
/// // Insert different types of data
/// extra.insert_string("name", "Alice");
/// extra.insert_number("age", 30);
/// extra.insert_bool("active", true);
///
/// // Retrieve with type checking
/// assert_eq!(extra.get_string("name").unwrap(), "Alice");
/// assert_eq!(extra.get_number("age").unwrap(), 30.into());
/// assert_eq!(extra.get_bool("active").unwrap(), true);
///
/// // Type validation
/// assert!(extra.has_typed("name", "string"));
/// assert!(!extra.has_typed("name", "number"));
/// ```
pub trait ExtraMapExt {
    /// Insert a string value into the extra map.
    ///
    /// This method provides a convenient way to insert string values without
    /// manually wrapping them in `Value::String`.
    ///
    /// # Parameters
    /// * `key` - Map key (will be converted to String)
    /// * `value` - String value (will be converted to String)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    ///
    /// let mut extra = new_extra_map();
    /// extra.insert_string("username", "alice123");
    /// extra.insert_string("display_name", String::from("Alice Smith"));
    /// ```
    fn insert_string(&mut self, key: impl Into<String>, value: impl Into<String>);

    /// Insert a numeric value into the extra map.
    ///
    /// Accepts any value that can be converted to `serde_json::Number`,
    /// including integers and floats.
    ///
    /// # Parameters
    /// * `key` - Map key (will be converted to String)
    /// * `value` - Numeric value (will be converted to serde_json::Number)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    /// use serde_json::Number;
    ///
    /// let mut extra = new_extra_map();
    /// extra.insert_number("count", 42);
    ///
    /// // For floats, create Number explicitly
    /// let price = Number::from_f64(19.99).unwrap();
    /// extra.insert_number("price", price);
    /// ```
    fn insert_number(&mut self, key: impl Into<String>, value: impl Into<serde_json::Number>);

    /// Insert a boolean value into the extra map.
    ///
    /// # Parameters
    /// * `key` - Map key (will be converted to String)
    /// * `value` - Boolean value
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    ///
    /// let mut extra = new_extra_map();
    /// extra.insert_bool("enabled", true);
    /// extra.insert_bool("verified", false);
    /// ```
    fn insert_bool(&mut self, key: impl Into<String>, value: bool);

    /// Insert any serializable value into the extra map.
    ///
    /// This method can handle complex types by serializing them to JSON.
    /// It's useful for storing structured data that needs to be preserved
    /// exactly as it was inserted.
    ///
    /// # Parameters
    /// * `key` - Map key (will be converted to String)
    /// * `value` - Any value that implements `serde::Serialize`
    ///
    /// # Returns
    /// * `Ok(())` - Value was successfully serialized and inserted
    /// * `Err(CollectionError::Json)` - Serialization failed
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct UserProfile {
    ///     id: u64,
    ///     email: String,
    /// }
    ///
    /// let mut extra = new_extra_map();
    /// let profile = UserProfile {
    ///     id: 123,
    ///     email: "alice@example.com".to_string(),
    /// };
    ///
    /// extra.insert_json("profile", &profile).unwrap();
    /// ```
    fn insert_json<T: serde::Serialize>(
        &mut self,
        key: impl Into<String>,
        value: T,
    ) -> Result<(), CollectionError>;

    /// Get a string value from the map with type validation.
    ///
    /// Returns the string value if the key exists and contains a string,
    /// otherwise returns an appropriate error.
    ///
    /// # Parameters
    /// * `key` - Map key to look up
    ///
    /// # Returns
    /// * `Ok(String)` - The string value
    /// * `Err(CollectionError::MissingKey)` - Key doesn't exist
    /// * `Err(CollectionError::TypeMismatch)` - Key exists but value is not a string
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    ///
    /// let mut extra = new_extra_map();
    /// extra.insert_string("name", "Alice");
    /// extra.insert_number("count", 42);
    ///
    /// assert_eq!(extra.get_string("name").unwrap(), "Alice");
    /// assert!(extra.get_string("count").is_err()); // Type mismatch
    /// assert!(extra.get_string("missing").is_err()); // Missing key
    /// ```
    fn get_string(&self, key: &str) -> Result<String, CollectionError>;

    /// Get a numeric value from the map with type validation.
    ///
    /// Returns the numeric value if the key exists and contains a number,
    /// otherwise returns an appropriate error.
    ///
    /// # Parameters
    /// * `key` - Map key to look up
    ///
    /// # Returns
    /// * `Ok(serde_json::Number)` - The numeric value
    /// * `Err(CollectionError::MissingKey)` - Key doesn't exist
    /// * `Err(CollectionError::TypeMismatch)` - Key exists but value is not a number
    fn get_number(&self, key: &str) -> Result<serde_json::Number, CollectionError>;

    /// Get a boolean value from the map with type validation.
    ///
    /// Returns the boolean value if the key exists and contains a boolean,
    /// otherwise returns an appropriate error.
    ///
    /// # Parameters
    /// * `key` - Map key to look up
    ///
    /// # Returns
    /// * `Ok(bool)` - The boolean value
    /// * `Err(CollectionError::MissingKey)` - Key doesn't exist
    /// * `Err(CollectionError::TypeMismatch)` - Key exists but value is not a boolean
    fn get_bool(&self, key: &str) -> Result<bool, CollectionError>;

    /// Get a value and deserialize it to the specified type.
    ///
    /// This method attempts to deserialize the JSON value at the given key
    /// to the requested type. It's useful for retrieving complex structured
    /// data that was stored using `insert_json`.
    ///
    /// # Parameters
    /// * `key` - Map key to look up
    ///
    /// # Returns
    /// * `Ok(T)` - Successfully deserialized value
    /// * `Err(CollectionError::MissingKey)` - Key doesn't exist
    /// * `Err(CollectionError::Json)` - Deserialization failed
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
    /// struct Config {
    ///     timeout: u64,
    ///     retries: u32,
    /// }
    ///
    /// let mut extra = new_extra_map();
    /// let config = Config { timeout: 5000, retries: 3 };
    /// extra.insert_json("config", &config).unwrap();
    ///
    /// let retrieved: Config = extra.get_typed("config").unwrap();
    /// assert_eq!(retrieved, config);
    /// ```
    fn get_typed<T: serde::de::DeserializeOwned>(&self, key: &str) -> Result<T, CollectionError>;

    /// Check if a key exists and has the expected type.
    ///
    /// This method provides a quick way to validate the presence and type
    /// of a value without retrieving it. Useful for conditional logic
    /// and validation scenarios.
    ///
    /// # Parameters
    /// * `key` - Map key to check
    /// * `expected_type` - Expected type as string ("string", "number", "bool", "array", "object", "null")
    ///
    /// # Returns
    /// `true` if the key exists and has the expected type, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::{new_extra_map, ExtraMapExt};
    ///
    /// let mut extra = new_extra_map();
    /// extra.insert_string("name", "Alice");
    /// extra.insert_number("age", 30);
    ///
    /// assert!(extra.has_typed("name", "string"));
    /// assert!(extra.has_typed("age", "number"));
    /// assert!(!extra.has_typed("name", "number"));
    /// assert!(!extra.has_typed("missing", "string"));
    /// ```
    fn has_typed(&self, key: &str, expected_type: &str) -> bool;
}

impl ExtraMapExt for FxHashMap<String, Value> {
    /// Inserts a string value into the FxHashMap as a JSON String value.
    ///
    /// This implementation converts both the key and value to their owned forms
    /// and wraps the value in `Value::String` for JSON compatibility.
    fn insert_string(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.insert(key.into(), Value::String(value.into()));
    }

    /// Inserts a numeric value into the FxHashMap as a JSON Number value.
    ///
    /// This implementation accepts any type that can be converted to `serde_json::Number`
    /// and wraps it in `Value::Number`.
    fn insert_number(&mut self, key: impl Into<String>, value: impl Into<serde_json::Number>) {
        self.insert(key.into(), Value::Number(value.into()));
    }

    /// Inserts a boolean value into the FxHashMap as a JSON Bool value.
    ///
    /// This implementation directly wraps the boolean in `Value::Bool`.
    fn insert_bool(&mut self, key: impl Into<String>, value: bool) {
        self.insert(key.into(), Value::Bool(value));
    }

    /// Serializes and inserts any serializable value into the FxHashMap.
    ///
    /// This implementation uses `serde_json::to_value` to convert the input
    /// to a JSON value, which may fail if serialization is not possible.
    /// The serialized value is then inserted into the map.
    fn insert_json<T: serde::Serialize>(
        &mut self,
        key: impl Into<String>,
        value: T,
    ) -> Result<(), CollectionError> {
        let json_value = serde_json::to_value(value)?;
        self.insert(key.into(), json_value);
        Ok(())
    }

    /// Retrieves a string value from the FxHashMap with type validation.
    ///
    /// This implementation checks that the value exists and is of type `Value::String`,
    /// returning appropriate errors for missing keys or type mismatches.
    /// The string is cloned to return an owned value.
    fn get_string(&self, key: &str) -> Result<String, CollectionError> {
        match self.get(key) {
            Some(Value::String(s)) => Ok(s.clone()),
            Some(other) => Err(CollectionError::TypeMismatch {
                key: key.to_string(),
                expected: "string".to_string(),
                found: format!("{:?}", other),
            }),
            None => Err(CollectionError::MissingKey {
                key: key.to_string(),
            }),
        }
    }

    /// Retrieves a numeric value from the FxHashMap with type validation.
    ///
    /// This implementation checks that the value exists and is of type `Value::Number`,
    /// returning appropriate errors for missing keys or type mismatches.
    /// The number is cloned to return an owned value.
    fn get_number(&self, key: &str) -> Result<serde_json::Number, CollectionError> {
        match self.get(key) {
            Some(Value::Number(n)) => Ok(n.clone()),
            Some(other) => Err(CollectionError::TypeMismatch {
                key: key.to_string(),
                expected: "number".to_string(),
                found: format!("{:?}", other),
            }),
            None => Err(CollectionError::MissingKey {
                key: key.to_string(),
            }),
        }
    }

    /// Retrieves a boolean value from the FxHashMap with type validation.
    ///
    /// This implementation checks that the value exists and is of type `Value::Bool`,
    /// returning appropriate errors for missing keys or type mismatches.
    /// The boolean is dereferenced to return the primitive value.
    fn get_bool(&self, key: &str) -> Result<bool, CollectionError> {
        match self.get(key) {
            Some(Value::Bool(b)) => Ok(*b),
            Some(other) => Err(CollectionError::TypeMismatch {
                key: key.to_string(),
                expected: "boolean".to_string(),
                found: format!("{:?}", other),
            }),
            None => Err(CollectionError::MissingKey {
                key: key.to_string(),
            }),
        }
    }

    /// Deserializes a JSON value to the specified type.
    ///
    /// This implementation uses `serde_json::from_value` to convert the stored
    /// JSON value to the requested type. The value is cloned before deserialization.
    /// Returns JSON errors if deserialization fails.
    fn get_typed<T: serde::de::DeserializeOwned>(&self, key: &str) -> Result<T, CollectionError> {
        match self.get(key) {
            Some(value) => serde_json::from_value(value.clone())
                .map_err(|e| CollectionError::Json { source: e }),
            None => Err(CollectionError::MissingKey {
                key: key.to_string(),
            }),
        }
    }

    /// Checks if a key exists and matches the expected JSON type.
    ///
    /// This implementation performs pattern matching against the JSON value variants
    /// and the expected type string. It's optimized to avoid cloning or deserializing
    /// the actual value, making it suitable for validation scenarios.
    fn has_typed(&self, key: &str, expected_type: &str) -> bool {
        matches!(
            (self.get(key), expected_type),
            (Some(Value::String(_)), "string")
                | (Some(Value::Number(_)), "number")
                | (Some(Value::Bool(_)), "bool")
                | (Some(Value::Array(_)), "array")
                | (Some(Value::Object(_)), "object")
                | (Some(Value::Null), "null")
        )
    }
}

/// Extension trait for `HashMap` with string keys to provide common operations.
///
/// This trait extends both `HashMap<String, V>` and `FxHashMap<String, V>` with
/// convenient methods for common access patterns. These methods reduce boilerplate
/// code when working with string-keyed maps throughout the codebase.
///
/// # Design Goals
///
/// - **Ergonomics**: Reduce common boilerplate patterns
/// - **Flexibility**: Support both standard HashMap and FxHashMap
/// - **Performance**: Minimize allocations and clones where possible
/// - **Safety**: Provide safe alternatives to unwrap-heavy code
///
/// # Examples
///
/// ```rust
/// use weavegraph::utils::collections::StringMapExt;
/// use rustc_hash::FxHashMap;
///
/// let mut config: FxHashMap<String, i32> = FxHashMap::default();
/// config.insert("max_connections".to_string(), 100);
///
/// // Get with default - no panic if key missing
/// let connections = config.get_or_default("max_connections", 50); // Returns 100
/// let timeout = config.get_or_default("timeout", 30); // Returns 30 (default)
///
/// // Insert or update pattern
/// config.insert_or_update(
///     "retry_count".to_string(),
///     1,                    // Initial value if key doesn't exist
///     |count| *count += 1,  // Update function if key exists
/// );
/// ```
pub trait StringMapExt<V> {
    /// Get a value with a default if the key doesn't exist.
    ///
    /// This method provides a safe way to retrieve values from a map with
    /// a fallback default, avoiding the need for `unwrap()` or complex
    /// match statements.
    ///
    /// # Parameters
    /// * `key` - Key to look up in the map
    /// * `default` - Default value to return if key is not found
    ///
    /// # Returns
    /// The value associated with the key, or the default if key doesn't exist
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::StringMapExt;
    /// use std::collections::HashMap;
    ///
    /// let mut settings = HashMap::new();
    /// settings.insert("debug".to_string(), true);
    ///
    /// assert_eq!(settings.get_or_default("debug", false), true);
    /// assert_eq!(settings.get_or_default("verbose", false), false);
    /// ```
    fn get_or_default(&self, key: &str, default: V) -> V
    where
        V: Clone;

    /// Insert if the key doesn't exist, otherwise update with a function.
    ///
    /// This method implements the common "insert or update" pattern efficiently.
    /// If the key exists, the update function is called with a mutable reference
    /// to the existing value. If the key doesn't exist, the provided value is inserted.
    ///
    /// This is particularly useful for counters, accumulators, and other scenarios
    /// where you need to modify existing values or insert new ones.
    ///
    /// # Parameters
    /// * `key` - Key to insert or update
    /// * `value` - Value to insert if key doesn't exist
    /// * `update_fn` - Function to call with existing value if key exists
    ///
    /// # Examples
    ///
    /// ```rust
    /// use weavegraph::utils::collections::StringMapExt;
    /// use rustc_hash::FxHashMap;
    ///
    /// let mut counters = FxHashMap::default();
    ///
    /// // First call inserts the initial value
    /// counters.insert_or_update(
    ///     "page_views".to_string(),
    ///     1,
    ///     |count| *count += 1,
    /// );
    /// assert_eq!(counters["page_views"], 1);
    ///
    /// // Subsequent calls update the existing value
    /// counters.insert_or_update(
    ///     "page_views".to_string(),
    ///     1,
    ///     |count| *count += 1,
    /// );
    /// assert_eq!(counters["page_views"], 2);
    /// ```
    fn insert_or_update<F>(&mut self, key: String, value: V, update_fn: F)
    where
        F: FnOnce(&mut V);
}

impl<V> StringMapExt<V> for HashMap<String, V> {
    /// Gets a value from the HashMap with a default fallback.
    ///
    /// This implementation uses the standard HashMap's `get` method with `cloned()`
    /// to return an owned value, falling back to the provided default if the key
    /// is not found. The standard HashMap uses SipHash for better security against
    /// hash collision attacks.
    fn get_or_default(&self, key: &str, default: V) -> V
    where
        V: Clone,
    {
        self.get(key).cloned().unwrap_or(default)
    }

    /// Inserts a new value or updates an existing one using the entry API.
    ///
    /// This implementation leverages HashMap's entry API for efficient
    /// insert-or-update operations. If the key exists, `and_modify` calls the
    /// update function. If not, `or_insert` adds the initial value.
    /// This avoids double-lookup that would occur with separate contains/get/insert calls.
    fn insert_or_update<F>(&mut self, key: String, value: V, update_fn: F)
    where
        F: FnOnce(&mut V),
    {
        self.entry(key).and_modify(update_fn).or_insert(value);
    }
}

impl<V> StringMapExt<V> for FxHashMap<String, V> {
    /// Gets a value from the FxHashMap with a default fallback.
    ///
    /// This implementation uses FxHashMap's `get` method with `cloned()`
    /// to return an owned value, falling back to the provided default if the key
    /// is not found. FxHashMap uses a faster hash function (FxHash) that's suitable
    /// for trusted input and provides better performance than the standard HashMap.
    fn get_or_default(&self, key: &str, default: V) -> V
    where
        V: Clone,
    {
        self.get(key).cloned().unwrap_or(default)
    }

    /// Inserts a new value or updates an existing one using the entry API.
    ///
    /// This implementation leverages FxHashMap's entry API for efficient
    /// insert-or-update operations. The behavior is identical to HashMap's
    /// implementation but benefits from FxHashMap's faster hashing for string keys.
    /// This is particularly beneficial in the Weavegraph framework where string keys are common.
    fn insert_or_update<F>(&mut self, key: String, value: V, update_fn: F)
    where
        F: FnOnce(&mut V),
    {
        self.entry(key).and_modify(update_fn).or_insert(value);
    }
}