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);
60
61impl<T: Clone + MergeFrom> MergeFrom for Option<T> {
62 fn merge_from(&mut self, other: &Self) {
63 let Some(other) = other else {
64 return;
65 };
66
67 if let Some(this) = self {
68 this.merge_from(other);
69 } else {
70 self.replace(other.clone());
71 }
72 }
73}
74
75impl<T: Clone> MergeFrom for Vec<T> {
76 fn merge_from(&mut self, other: &Self) {
77 *self = other.clone()
78 }
79}
80
81impl<T: MergeFrom> MergeFrom for Box<T> {
82 fn merge_from(&mut self, other: &Self) {
83 self.as_mut().merge_from(other.as_ref())
84 }
85}
86
87// Implementations for collections that extend/merge their contents
88impl<K, V> MergeFrom for collections::HashMap<K, V>
89where
90 K: Clone + std::hash::Hash + Eq,
91 V: Clone + MergeFrom,
92{
93 fn merge_from(&mut self, other: &Self) {
94 for (key, value) in other {
95 if let Some(existing) = self.get_mut(key) {
96 existing.merge_from(value);
97 } else {
98 self.insert(key.clone(), value.clone());
99 }
100 }
101 }
102}
103
104impl<K, V> MergeFrom for collections::BTreeMap<K, V>
105where
106 K: Clone + std::hash::Hash + Eq + Ord,
107 V: Clone + MergeFrom,
108{
109 fn merge_from(&mut self, other: &Self) {
110 for (key, value) in other {
111 if let Some(existing) = self.get_mut(key) {
112 existing.merge_from(value);
113 } else {
114 self.insert(key.clone(), value.clone());
115 }
116 }
117 }
118}
119
120impl<K, V> MergeFrom for collections::IndexMap<K, V>
121where
122 K: std::hash::Hash + Eq + Clone,
123 V: Clone + MergeFrom,
124{
125 fn merge_from(&mut self, other: &Self) {
126 for (key, value) in other {
127 if let Some(existing) = self.get_mut(key) {
128 existing.merge_from(value);
129 } else {
130 self.insert(key.clone(), value.clone());
131 }
132 }
133 }
134}
135
136impl<T> MergeFrom for collections::BTreeSet<T>
137where
138 T: Clone + Ord,
139{
140 fn merge_from(&mut self, other: &Self) {
141 for item in other {
142 self.insert(item.clone());
143 }
144 }
145}
146
147impl<T> MergeFrom for collections::HashSet<T>
148where
149 T: Clone + std::hash::Hash + Eq,
150{
151 fn merge_from(&mut self, other: &Self) {
152 for item in other {
153 self.insert(item.clone());
154 }
155 }
156}
157
158impl MergeFrom for serde_json::Value {
159 fn merge_from(&mut self, other: &Self) {
160 match (self, other) {
161 (serde_json::Value::Object(this), serde_json::Value::Object(other)) => {
162 for (key, value) in other {
163 if let Some(existing) = this.get_mut(key) {
164 existing.merge_from(value);
165 } else {
166 this.insert(key.clone(), value.clone());
167 }
168 }
169 }
170 (this, other) => *this = other.clone(),
171 }
172 }
173}