|
| 1 | +use once_cell::sync::Lazy; |
| 2 | +use simd_json::OwnedValue; |
| 3 | +use simd_json::derived::ValueObjectAccess; |
| 4 | +use simd_json::prelude::ValueArrayAccess; |
| 5 | + |
| 6 | +// Parse once at startup |
| 7 | +static REGISTRY_BYTES: &[u8] = include_bytes!("../../../../assets/data/registries.json"); |
| 8 | + |
| 9 | +static LOADED_REGISTRY: Lazy<OwnedValue> = Lazy::new(|| { |
| 10 | + // simd-json mutates the buffer during parsing → needs a Vec<u8> |
| 11 | + let mut buf = REGISTRY_BYTES.to_vec(); |
| 12 | + simd_json::to_owned_value(&mut buf).expect("parse registries.json") |
| 13 | +}); |
| 14 | + |
| 15 | +/// Looks up a value in the loaded JSON registry by a given path. |
| 16 | +/// |
| 17 | +/// # Arguments |
| 18 | +/// * `path` - A string slice representing the path to the desired value in the JSON structure. |
| 19 | +/// Path segments are separated by `/`. Numeric segments are treated as array indices. |
| 20 | +/// |
| 21 | +/// # Returns |
| 22 | +/// * `Option<&OwnedValue>` - Returns a reference to the value if found, or `None` if the path does not exist. |
| 23 | +/// |
| 24 | +/// # Examples |
| 25 | +/// ``` |
| 26 | +/// # use simd_json::prelude::{TypedScalarValue, ValueAsScalar}; |
| 27 | +/// # use ferrumc_registry::lookup; |
| 28 | +/// let value = lookup("minecraft:item/entries/minecraft:apple/protocol_id"); |
| 29 | +/// assert!(value.is_some()); |
| 30 | +/// assert!(value.unwrap().is_u64()); // check if the value is a number |
| 31 | +/// assert_eq!(value.unwrap().as_u64().unwrap(), 840); |
| 32 | +/// ``` |
| 33 | +pub fn lookup(path: &str) -> Option<&OwnedValue> { |
| 34 | + let mut cur: &OwnedValue = &LOADED_REGISTRY; |
| 35 | + |
| 36 | + if path.is_empty() { |
| 37 | + return Some(cur); // return the root if path is empty |
| 38 | + } |
| 39 | + |
| 40 | + for seg in path.split('/') { |
| 41 | + // allow numeric segments as array indices |
| 42 | + if let Ok(idx) = seg.parse::<usize>() { |
| 43 | + cur = cur.get_idx(idx)?; // works if current is an array |
| 44 | + } else { |
| 45 | + cur = cur.get(seg)?; // works if current is an object |
| 46 | + } |
| 47 | + } |
| 48 | + Some(cur) |
| 49 | +} |
| 50 | + |
| 51 | +/// Looks up a value in the loaded JSON registry by a given path and returns an owned clone. |
| 52 | +/// |
| 53 | +/// # Arguments |
| 54 | +/// * `path` - A string slice representing the path to the desired value in the JSON structure. |
| 55 | +/// |
| 56 | +/// # Returns |
| 57 | +/// * `Option<OwnedValue>` - Returns an owned clone of the value if found, or `None` if the path does not exist. |
| 58 | +/// |
| 59 | +/// # Examples |
| 60 | +/// ``` |
| 61 | +/// # use simd_json::prelude::{TypedScalarValue, ValueAsScalar}; |
| 62 | +/// # use ferrumc_registry::lookup_owned; |
| 63 | +/// let value = lookup_owned("minecraft:item/entries/minecraft:apple/protocol_id"); |
| 64 | +/// assert_eq!(value.unwrap().as_u64().unwrap(), 840); |
| 65 | +/// ``` |
| 66 | +pub fn lookup_owned(path: &str) -> Option<OwnedValue> { |
| 67 | + lookup(path).cloned() |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + use simd_json::prelude::ValueAsScalar; |
| 74 | + #[test] |
| 75 | + fn test_lookup() { |
| 76 | + let value = lookup("minecraft:item/entries/minecraft:apple/protocol_id"); |
| 77 | + assert!(value.is_some()); |
| 78 | + let value = value.unwrap(); |
| 79 | + let numeric_value = value.as_u64(); |
| 80 | + assert!(numeric_value.is_some()); |
| 81 | + assert_eq!(numeric_value.unwrap(), 840); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_lookup_owned() { |
| 86 | + let value = lookup_owned("minecraft:item/entries/minecraft:apple/protocol_id"); |
| 87 | + assert!(value.is_some()); |
| 88 | + let value = value.unwrap(); |
| 89 | + let numeric_value = value.as_u64(); |
| 90 | + assert!(numeric_value.is_some()); |
| 91 | + assert_eq!(numeric_value.unwrap(), 840); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_lookup_non_existent() { |
| 96 | + let value = lookup("minecraft:item/entries/minecraft:non_existent/protocol_id"); |
| 97 | + assert!(value.is_none()); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_lookup_empty_path() { |
| 102 | + // Edge case: empty path should return the root |
| 103 | + let value = lookup(""); |
| 104 | + assert!(value.is_some()); |
| 105 | + assert_eq!(value.unwrap(), &*LOADED_REGISTRY); |
| 106 | + } |
| 107 | +} |
0 commit comments