Skip to main content

citum_schema_data/
abbreviation.rs

1/*
2SPDX-License-Identifier: MIT OR Apache-2.0
3SPDX-FileCopyrightText: © 2023-2026 Bruce D'Arcus and Citum contributors
4*/
5
6//! Document-level abbreviation map type.
7
8use std::collections::HashMap;
9
10#[cfg(feature = "schema")]
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13
14/// Document-level map from full rendered strings to their abbreviations.
15///
16/// Keys are the full rendered string (exact, case-sensitive). Values are the
17/// replacement abbreviation. Applied after value extraction, before output.
18///
19/// Accepts both `abbreviation-map` (YAML frontmatter) and `abbreviation_map` forms.
20#[derive(Debug, Clone, Default, Serialize, Deserialize)]
21#[cfg_attr(feature = "schema", derive(JsonSchema))]
22#[serde(transparent)]
23pub struct AbbreviationMap(pub HashMap<String, String>);
24
25#[cfg(test)]
26mod tests {
27    use super::AbbreviationMap;
28
29    #[test]
30    fn given_flat_map_when_deserialized_then_entries_are_preserved() {
31        let json = r#"{
32            "Estates Gazette": "EG",
33            "Lloyd's Law Reports": "Lloyd's Rep"
34        }"#;
35
36        let map: AbbreviationMap = serde_json::from_str(json).unwrap_or_default();
37
38        assert_eq!(map.0.get("Estates Gazette"), Some(&"EG".to_string()));
39        assert_eq!(
40            map.0.get("Lloyd's Law Reports"),
41            Some(&"Lloyd's Rep".to_string())
42        );
43    }
44
45    #[test]
46    fn given_reserved_looking_keys_when_deserialized_then_keys_are_entries() {
47        let json = r#"{
48            "title": "ttl.",
49            "description": "desc.",
50            "metadata": "meta.",
51            "entries": "ent."
52        }"#;
53
54        let map: AbbreviationMap = serde_json::from_str(json).unwrap_or_default();
55
56        assert_eq!(map.0.get("title"), Some(&"ttl.".to_string()));
57        assert_eq!(map.0.get("description"), Some(&"desc.".to_string()));
58        assert_eq!(map.0.get("metadata"), Some(&"meta.".to_string()));
59        assert_eq!(map.0.get("entries"), Some(&"ent.".to_string()));
60    }
61
62    #[test]
63    fn given_flat_map_when_serialized_then_output_has_only_entries() {
64        let json = r#"{
65            "title": "ttl.",
66            "World Health Organization": "WHO"
67        }"#;
68        let map: AbbreviationMap = serde_json::from_str(json).unwrap_or_default();
69
70        let serialized = serde_json::to_value(&map).unwrap_or(serde_json::Value::Null);
71
72        assert_eq!(serialized.get("title"), Some(&serde_json::json!("ttl.")));
73        assert_eq!(
74            serialized.get("World Health Organization"),
75            Some(&serde_json::json!("WHO"))
76        );
77        assert!(serialized.get("metadata").is_none());
78    }
79}