Debian Codegraph @

src/merge.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 use alloc::{
4 collections::{BTreeMap, btree_map},
5 format,
6 string::String,
7 vec::Vec,
8 };
9
10 use crate::{
11 Color, Frequency, When,
12 de::{
13 self, CredentialProvider, GlobalCredentialProviders, PathAndArgs, RegistriesProtocol,
14 VersionControlSoftware,
15 },
16 error::{Context as _, Result},
17 value::Value,
18 };
19
20 // https://github.com/rust-lang/cargo/blob/0.74.0/src/cargo/util/config/mod.rs#L2107-L2115
21 // TODO:
22 // - https://github.com/rust-lang/cargo/pull/12515
23 // - https://github.com/rust-lang/cargo/blob/0.80.0/src/cargo/util/context/mod.rs#L2159-L2167
24 //
25 // > If `force` is true, primitive (non-container) types will override existing values.
26 // > If false, the original will be kept and the new value ignored.
27 // >
28 // > Container types (tables and arrays) are merged with existing values.
29 // >
30 // > Container and non-container types cannot be mixed.
31 pub(crate) trait Merge {
32 /// Merges given config into this config.
33 fn merge(&mut self, low: Self, force: bool) -> Result<()>;
34 }
35
36 macro_rules! merge_non_container {
37 ($($ty:tt)*) => {
38 impl Merge for $($ty)* {
39 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
40 if force {
41 *self = low;
42 }
43 Ok(())
44 }
45 }
46 impl Merge for Value<$($ty)*> {
47 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
48 if force {
49 *self = low;
50 }
51 Ok(())
52 }
53 }
54 };
55 }
56 merge_non_container!(bool);
57 merge_non_container!(i32);
58 merge_non_container!(u32);
59 merge_non_container!(String);
60 merge_non_container!(Color);
61 merge_non_container!(VersionControlSoftware);
62 merge_non_container!(Frequency);
63 merge_non_container!(When);
64 merge_non_container!(RegistriesProtocol);
65 merge_non_container!(CredentialProvider);
66 merge_non_container!(PathAndArgs); // https://github.com/taiki-e/cargo-config2/issues/26
67
68 fn merge_array<T>(this: &mut Vec<T>, mut low: Vec<T>) {
69 // https://doc.rust-lang.org/nightly/cargo/reference/config.html#hierarchical-structure
70 // > Arrays will be joined together with higher precedence items being placed later in the merged array.
71 low.append(this);
72 *this = low;
73 }
74
75 impl Merge for GlobalCredentialProviders {
76 fn merge(&mut self, low: Self, _force: bool) -> Result<()> {
77 merge_array(&mut self.0, low.0);
78 Ok(())
79 }
80 }
81
82 impl<T: Merge> Merge for Option<T> {
83 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
84 match (self, low) {
85 (_, None) => {}
86 (this @ None, low) => *this = low,
87 (Some(this), Some(low)) => this.merge(low, force)?,
88 }
89 Ok(())
90 }
91 }
92 impl Merge for de::StringOrArray {
93 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
94 match (self, low) {
95 (this @ de::StringOrArray::String(_), low @ de::StringOrArray::String(_)) => {
96 if force {
97 *this = low;
98 }
99 }
100 (de::StringOrArray::Array(this), de::StringOrArray::Array(low)) => {
101 merge_array(this, low);
102 }
103 (expected, actual) => {
104 bail!("expected {}, but found {}", expected.kind(), actual.kind());
105 }
106 }
107 Ok(())
108 }
109 }
110 impl Merge for de::StringList {
111 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
112 match (self.deserialized_repr, low.deserialized_repr) {
113 (de::StringListDeserializedRepr::String, de::StringListDeserializedRepr::String) => {
114 if force {
115 *self = low;
116 }
117 }
118 (de::StringListDeserializedRepr::Array, de::StringListDeserializedRepr::Array) => {
119 merge_array(&mut self.list, low.list);
120 }
121 (expected, actual) => {
122 bail!("expected {}, but found {}", expected.as_str(), actual.as_str());
123 }
124 }
125 Ok(())
126 }
127 }
128 impl Merge for de::EnvConfigValue {
129 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
130 match (self, low) {
131 (Self::Value(this), Self::Value(low)) => {
132 if force {
133 *this = low;
134 }
135 }
136 (
137 Self::Table { value: this_value, force: this_force, relative: this_relative },
138 Self::Table { value: low_value, force: low_force, relative: low_relative },
139 ) => {
140 this_value.merge(low_value, force)?;
141 this_force.merge(low_force, force)?;
142 this_relative.merge(low_relative, force)?;
143 }
144 (expected, actual) => {
145 bail!("expected {}, but found {}", expected.kind(), actual.kind());
146 }
147 }
148 Ok(())
149 }
150 }
151 impl Merge for de::Flags {
152 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
153 match (self.deserialized_repr, low.deserialized_repr) {
154 (de::StringListDeserializedRepr::String, de::StringListDeserializedRepr::String) => {
155 if force {
156 *self = low;
157 }
158 }
159 (de::StringListDeserializedRepr::Array, de::StringListDeserializedRepr::Array) => {
160 merge_array(&mut self.flags, low.flags);
161 }
162 (expected, actual) => {
163 bail!("expected {}, but found {}", expected.as_str(), actual.as_str());
164 }
165 }
166 Ok(())
167 }
168 }
169 impl<V: Merge + Clone + core::fmt::Debug> Merge for BTreeMap<String, V> {
170 fn merge(&mut self, low: Self, force: bool) -> Result<()> {
171 for (key, value) in low {
172 match self.entry(key.clone()) {
173 btree_map::Entry::Occupied(mut entry) => {
174 let entry = entry.get_mut();
175 entry.merge(value.clone(), force).with_context(|| {
176 format!(
177 "failed to merge key `{key}` between \
178 {entry:?} and {value:?}", /* TODO: do not use debug output */
179 )
180 })?;
181 }
182 btree_map::Entry::Vacant(entry) => {
183 entry.insert(value);
184 }
185 }
186 }
187 Ok(())
188 }
189 }