known-values 0.15.5

Blockchain Commons Known Values.
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
use std::collections::HashMap;
#[cfg(feature = "directory-loading")]
use std::path::Path;

use super::known_value::KnownValue;

/// A store that maps between Known Values and their assigned names.
///
/// The `KnownValuesStore` provides a bidirectional mapping between:
/// - Numeric values (u64) and their corresponding KnownValue instances
/// - String names and their corresponding KnownValue instances
///
/// This enables efficient lookup in both directions, making it possible to:
/// - Find the name for a given numeric value
/// - Find the numeric value for a given name
/// - Retrieve complete KnownValue instances by either name or value
///
/// The store is typically populated with predefined Known Values from the
/// registry, but can also be extended with custom values.
///
/// # Examples
///
/// ```
/// use std::collections::HashMap;
///
/// use known_values::{KnownValue, KnownValuesStore};
///
/// // Create a store with predefined Known Values
/// let store = KnownValuesStore::new([
///     known_values::IS_A,
///     known_values::NOTE,
///     known_values::SIGNED,
/// ]);
///
/// // Look up a Known Value by name
/// let is_a = store.known_value_named("isA").unwrap();
/// assert_eq!(is_a.value(), 1);
///
/// // Look up a name for a raw value
/// let name = store.name(KnownValue::new(3));
/// assert_eq!(name, "signed");
///
/// // Insert a custom Known Value
/// let mut custom_store = store.clone();
/// custom_store
///     .insert(KnownValue::new_with_name(100u64, "customValue".to_string()));
/// assert_eq!(
///     custom_store
///         .known_value_named("customValue")
///         .unwrap()
///         .value(),
///     100
/// );
/// ```
#[derive(Clone, Debug)]
pub struct KnownValuesStore {
    known_values_by_raw_value: HashMap<u64, KnownValue>,
    known_values_by_assigned_name: HashMap<String, KnownValue>,
}

impl KnownValuesStore {
    /// Creates a new KnownValuesStore with the provided Known Values.
    ///
    /// This constructor takes any iterable of KnownValue instances and
    /// populates the store with them, creating mappings from both raw
    /// values and names to the corresponding KnownValue instances.
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::KnownValuesStore;
    ///
    /// // Create a store with predefined Known Values
    /// let store = KnownValuesStore::new([
    ///     known_values::IS_A,
    ///     known_values::NOTE,
    ///     known_values::SIGNED,
    /// ]);
    ///
    /// // Look up Known Values
    /// assert_eq!(store.known_value_named("isA").unwrap().value(), 1);
    /// assert_eq!(store.known_value_named("note").unwrap().value(), 4);
    /// ```
    pub fn new<T>(known_values: T) -> Self
    where
        T: IntoIterator<Item = KnownValue>,
    {
        let mut known_values_by_raw_value = HashMap::new();
        let mut known_values_by_assigned_name = HashMap::new();
        for known_value in known_values {
            Self::_insert(
                known_value,
                &mut known_values_by_raw_value,
                &mut known_values_by_assigned_name,
            );
        }
        Self {
            known_values_by_raw_value,
            known_values_by_assigned_name,
        }
    }

    /// Inserts a KnownValue into the store.
    ///
    /// If the KnownValue has an assigned name, it will be indexed by both its
    /// raw value and its name. If a KnownValue with the same raw value or name
    /// already exists in the store, it will be replaced.
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::{KnownValue, KnownValuesStore};
    ///
    /// let mut store = KnownValuesStore::default();
    /// store.insert(KnownValue::new_with_name(100u64, "customValue".to_string()));
    /// assert_eq!(store.known_value_named("customValue").unwrap().value(), 100);
    /// ```
    pub fn insert(&mut self, known_value: KnownValue) {
        Self::_insert(
            known_value,
            &mut self.known_values_by_raw_value,
            &mut self.known_values_by_assigned_name,
        );
    }

    /// Returns the assigned name for a KnownValue, if present in the store.
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::{KnownValue, KnownValuesStore};
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// assert_eq!(store.assigned_name(&known_values::IS_A), Some("isA"));
    /// assert_eq!(store.assigned_name(&KnownValue::new(999)), None);
    /// ```
    pub fn assigned_name(&self, known_value: &KnownValue) -> Option<&str> {
        self.known_values_by_raw_value
            .get(&known_value.value())
            .and_then(|known_value| known_value.assigned_name())
    }

    /// Returns a human-readable name for a KnownValue.
    ///
    /// If the KnownValue has an assigned name in the store, that name is
    /// returned. Otherwise, the KnownValue's default name (which may be its
    /// numeric value as a string) is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::{KnownValue, KnownValuesStore};
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// assert_eq!(store.name(known_values::IS_A), "isA");
    /// assert_eq!(store.name(KnownValue::new(999)), "999");
    /// ```
    pub fn name(&self, known_value: KnownValue) -> String {
        self.assigned_name(&known_value)
            .map(|name| name.to_string())
            .unwrap_or_else(|| known_value.name())
    }

    /// Looks up a KnownValue by its assigned name.
    ///
    /// Returns a reference to the KnownValue if found, or None if no KnownValue
    /// with the given name exists in the store.
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::KnownValuesStore;
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// let is_a = store.known_value_named("isA").unwrap();
    /// assert_eq!(is_a.value(), 1);
    ///
    /// assert!(store.known_value_named("nonexistent").is_none());
    /// ```
    pub fn known_value_named(
        &self,
        assigned_name: &str,
    ) -> Option<&KnownValue> {
        self.known_values_by_assigned_name.get(assigned_name)
    }

    /// Retrieves a KnownValue for a raw value, using a store if provided.
    ///
    /// This static method allows looking up a KnownValue by its raw numeric
    /// value:
    /// - If a store is provided and contains a mapping for the raw value, that
    ///   KnownValue is returned
    /// - Otherwise, a new KnownValue with no assigned name is created and
    ///   returned
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::KnownValuesStore;
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// // Known value from store
    /// let is_a = KnownValuesStore::known_value_for_raw_value(1, Some(&store));
    /// assert_eq!(is_a.name(), "isA");
    ///
    /// // Unknown value creates a new KnownValue
    /// let unknown =
    ///     KnownValuesStore::known_value_for_raw_value(999, Some(&store));
    /// assert_eq!(unknown.name(), "999");
    ///
    /// // No store provided also creates a new KnownValue
    /// let unknown = KnownValuesStore::known_value_for_raw_value(1, None);
    /// assert_eq!(unknown.name(), "1");
    /// ```
    pub fn known_value_for_raw_value(
        raw_value: u64,
        known_values: Option<&Self>,
    ) -> KnownValue {
        known_values
            .and_then(|known_values| {
                known_values.known_values_by_raw_value.get(&raw_value)
            })
            .cloned()
            .unwrap_or_else(|| KnownValue::new(raw_value))
    }

    /// Attempts to find a KnownValue by its name, using a store if provided.
    ///
    /// This static method allows looking up a KnownValue by its name:
    /// - If a store is provided and contains a mapping for the name, that
    ///   KnownValue is returned
    /// - Otherwise, None is returned
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::KnownValuesStore;
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// // Known value from store
    /// let is_a = KnownValuesStore::known_value_for_name("isA", Some(&store));
    /// assert_eq!(is_a.unwrap().value(), 1);
    ///
    /// // Unknown name returns None
    /// assert!(
    ///     KnownValuesStore::known_value_for_name("unknown", Some(&store))
    ///         .is_none()
    /// );
    ///
    /// // No store provided also returns None
    /// assert!(KnownValuesStore::known_value_for_name("isA", None).is_none());
    /// ```
    pub fn known_value_for_name(
        name: &str,
        known_values: Option<&Self>,
    ) -> Option<KnownValue> {
        known_values
            .and_then(|known_values| known_values.known_value_named(name))
            .cloned()
    }

    /// Returns a human-readable name for a KnownValue, using a store if
    /// provided.
    ///
    /// This static method allows getting a name for a KnownValue:
    /// - If a store is provided and contains a mapping for the KnownValue, its
    ///   assigned name is returned
    /// - Otherwise, the KnownValue's default name (which may be its numeric
    ///   value as a string) is returned
    ///
    /// # Examples
    ///
    /// ```
    /// use known_values::{KnownValue, KnownValuesStore};
    ///
    /// let store = KnownValuesStore::new([known_values::IS_A, known_values::NOTE]);
    ///
    /// // Known value from store
    /// let name = KnownValuesStore::name_for_known_value(
    ///     known_values::IS_A,
    ///     Some(&store),
    /// );
    /// assert_eq!(name, "isA");
    ///
    /// // Unknown value in store uses KnownValue's name method
    /// let name = KnownValuesStore::name_for_known_value(
    ///     KnownValue::new(999),
    ///     Some(&store),
    /// );
    /// assert_eq!(name, "999");
    ///
    /// // No store provided also uses KnownValue's name method
    /// let name = KnownValuesStore::name_for_known_value(known_values::IS_A, None);
    /// assert_eq!(name, "isA");
    /// ```
    pub fn name_for_known_value(
        known_value: KnownValue,
        known_values: Option<&Self>,
    ) -> String {
        known_values
            .and_then(|known_values| known_values.assigned_name(&known_value))
            .map(|assigned_name| assigned_name.to_string())
            .unwrap_or_else(|| known_value.name())
    }

    /// Internal helper method to insert a KnownValue into the store's maps.
    ///
    /// When inserting a value with a codepoint that already exists, this method
    /// removes the old name from the name index before adding the new one.
    fn _insert(
        known_value: KnownValue,
        known_values_by_raw_value: &mut HashMap<u64, KnownValue>,
        known_values_by_assigned_name: &mut HashMap<String, KnownValue>,
    ) {
        // If there's an existing value with the same codepoint, remove its name
        // from the name index to avoid stale entries
        if let Some(old_value) =
            known_values_by_raw_value.get(&known_value.value())
            && let Some(old_name) = old_value.assigned_name()
        {
            known_values_by_assigned_name.remove(old_name);
        }

        known_values_by_raw_value
            .insert(known_value.value(), known_value.clone());
        if let Some(name) = known_value.assigned_name() {
            known_values_by_assigned_name.insert(name.to_string(), known_value);
        }
    }

    /// Loads and inserts known values from a directory containing JSON registry
    /// files.
    ///
    /// This method scans the specified directory for `.json` files and parses
    /// them as known value registries. Values from JSON files override
    /// existing values in the store when codepoints match.
    ///
    /// This method is only available when the `directory-loading` feature is
    /// enabled.
    ///
    /// # Arguments
    ///
    /// * `path` - The directory to scan for JSON registry files.
    ///
    /// # Returns
    ///
    /// Returns `Ok(count)` with the number of values loaded, or an error if
    /// directory traversal fails.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use known_values::KnownValuesStore;
    /// use std::path::Path;
    ///
    /// let mut store = KnownValuesStore::default();
    /// let count = store.load_from_directory(Path::new("/etc/known-values"))?;
    /// println!("Loaded {} values", count);
    /// ```
    #[cfg(feature = "directory-loading")]
    pub fn load_from_directory(
        &mut self,
        path: &Path,
    ) -> Result<usize, crate::LoadError> {
        let values = crate::directory_loader::load_from_directory(path)?;
        let count = values.len();
        for value in values {
            self.insert(value);
        }
        Ok(count)
    }

    /// Loads known values from multiple directories using the provided
    /// configuration.
    ///
    /// Directories are processed in order. When multiple entries have the same
    /// codepoint, values from later directories override values from earlier
    /// directories.
    ///
    /// This method is only available when the `directory-loading` feature is
    /// enabled.
    ///
    /// # Arguments
    ///
    /// * `config` - The directory configuration specifying search paths.
    ///
    /// # Returns
    ///
    /// A `LoadResult` containing information about the loading operation.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// use known_values::{KnownValuesStore, DirectoryConfig};
    ///
    /// let mut store = KnownValuesStore::default();
    /// let config = DirectoryConfig::default_only();
    /// let result = store.load_from_config(&config);
    ///
    /// println!("Loaded {} values", result.values_count());
    /// if result.has_errors() {
    ///     for (path, error) in &result.errors {
    ///         eprintln!("Error: {}: {}", path.display(), error);
    ///     }
    /// }
    /// ```
    #[cfg(feature = "directory-loading")]
    pub fn load_from_config(
        &mut self,
        config: &crate::DirectoryConfig,
    ) -> crate::LoadResult {
        let result = crate::directory_loader::load_from_config(config);
        for value in result.values.values() {
            self.insert(value.clone());
        }
        result
    }
}

/// Default implementation creates an empty KnownValuesStore.
impl Default for KnownValuesStore {
    fn default() -> Self { Self::new([]) }
}