Skip to repository content

tenant.openagents/omega

No repository description is available.

OpenAgents Git authority 2026-07-28T04:47:15.549Z Public web read
NIP-34 coordinate30617:7649603503856e5148d571eac2766b288a8ff1e9e35d380337a1d2b0015b4f92:omega
MaintainersHidden in public view
References2 branches · 1 tag
Read-only clonegit clone https://openagents.com/git/tenant.openagents/omega.git
Browse files

merge_from.rs

175 lines · 4.7 KB · rust
1/// Trait for recursively merging settings structures.
2///
3/// When Zed starts it loads settings from `default.json` to initialize
4/// everything. These may be further refined by loading the user's settings,
5/// and any settings profiles; and then further refined by loading any
6/// local project settings.
7///
8/// The default behaviour of merging is:
9/// * For objects with named keys (HashMap, structs, etc.). The values are merged deeply
10///   (so if the default settings has languages.JSON.prettier.allowed = true, and the user's settings has
11///    languages.JSON.tab_size = 4; the merged settings file will have both settings).
12/// * For options, a None value is ignored, but Some values are merged recursively.
13/// * For other types (including Vec), a merge overwrites the current value.
14///
15/// If you want to break the rules you can (e.g. ExtendingVec, or SaturatingBool).
16#[allow(unused)]
17pub trait MergeFrom {
18    /// Merge from a source of the same type.
19    fn merge_from(&mut self, other: &Self);
20
21    /// Merge from an optional source of the same type.
22    fn merge_from_option(&mut self, other: Option<&Self>) {
23        if let Some(other) = other {
24            self.merge_from(other);
25        }
26    }
27}
28
29macro_rules! merge_from_overwrites {
30    ($($type:ty),+ $(,)?) => {
31        $(
32            impl MergeFrom for $type {
33                fn merge_from(&mut self, other: &Self) {
34                    *self = other.clone();
35                }
36            }
37        )+
38    }
39}
40
41merge_from_overwrites!(
42    u16,
43    u32,
44    u64,
45    usize,
46    i16,
47    i32,
48    i64,
49    bool,
50    f64,
51    f32,
52    char,
53    std::num::NonZeroUsize,
54    std::num::NonZeroU32,
55    String,
56    std::sync::Arc<str>,
57    std::path::PathBuf,
58    std::sync::Arc<std::path::Path>,
59    language_model_core::Speed,
60);
61
62impl<T: Clone + MergeFrom> MergeFrom for Option<T> {
63    fn merge_from(&mut self, other: &Self) {
64        let Some(other) = other else {
65            return;
66        };
67
68        if let Some(this) = self {
69            this.merge_from(other);
70        } else {
71            self.replace(other.clone());
72        }
73    }
74}
75
76impl<T: Clone> MergeFrom for Vec<T> {
77    fn merge_from(&mut self, other: &Self) {
78        *self = other.clone()
79    }
80}
81
82impl<T: MergeFrom> MergeFrom for Box<T> {
83    fn merge_from(&mut self, other: &Self) {
84        self.as_mut().merge_from(other.as_ref())
85    }
86}
87
88// Implementations for collections that extend/merge their contents
89impl<K, V> MergeFrom for collections::HashMap<K, V>
90where
91    K: Clone + std::hash::Hash + Eq,
92    V: Clone + MergeFrom,
93{
94    fn merge_from(&mut self, other: &Self) {
95        for (key, value) in other {
96            if let Some(existing) = self.get_mut(key) {
97                existing.merge_from(value);
98            } else {
99                self.insert(key.clone(), value.clone());
100            }
101        }
102    }
103}
104
105impl<K, V> MergeFrom for collections::BTreeMap<K, V>
106where
107    K: Clone + std::hash::Hash + Eq + Ord,
108    V: Clone + MergeFrom,
109{
110    fn merge_from(&mut self, other: &Self) {
111        for (key, value) in other {
112            if let Some(existing) = self.get_mut(key) {
113                existing.merge_from(value);
114            } else {
115                self.insert(key.clone(), value.clone());
116            }
117        }
118    }
119}
120
121impl<K, V> MergeFrom for collections::IndexMap<K, V>
122where
123    K: std::hash::Hash + Eq + Clone,
124    V: Clone + MergeFrom,
125{
126    fn merge_from(&mut self, other: &Self) {
127        for (key, value) in other {
128            if let Some(existing) = self.get_mut(key) {
129                existing.merge_from(value);
130            } else {
131                self.insert(key.clone(), value.clone());
132            }
133        }
134    }
135}
136
137impl<T> MergeFrom for collections::BTreeSet<T>
138where
139    T: Clone + Ord,
140{
141    fn merge_from(&mut self, other: &Self) {
142        for item in other {
143            self.insert(item.clone());
144        }
145    }
146}
147
148impl<T> MergeFrom for collections::HashSet<T>
149where
150    T: Clone + std::hash::Hash + Eq,
151{
152    fn merge_from(&mut self, other: &Self) {
153        for item in other {
154            self.insert(item.clone());
155        }
156    }
157}
158
159impl MergeFrom for serde_json::Value {
160    fn merge_from(&mut self, other: &Self) {
161        match (self, other) {
162            (serde_json::Value::Object(this), serde_json::Value::Object(other)) => {
163                for (key, value) in other {
164                    if let Some(existing) = this.get_mut(key) {
165                        existing.merge_from(value);
166                    } else {
167                        this.insert(key.clone(), value.clone());
168                    }
169                }
170            }
171            (this, other) => *this = other.clone(),
172        }
173    }
174}
175
Served at tenant.openagents/omega Member data and write actions are omitted.