1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::util::config::Config;
use serde::de;
use std::fmt;
use std::marker;
use std::mem;
use std::path::{Path, PathBuf};
#[derive(Debug, PartialEq, Clone)]
pub struct Value<T> {
pub val: T,
pub definition: Definition,
}
pub type OptValue<T> = Option<Value<T>>;
pub(crate) const VALUE_FIELD: &str = "$__cargo_private_value";
pub(crate) const DEFINITION_FIELD: &str = "$__cargo_private_definition";
pub(crate) const NAME: &str = "$__cargo_private_Value";
pub(crate) static FIELDS: [&str; 2] = [VALUE_FIELD, DEFINITION_FIELD];
#[derive(Clone, Debug, Eq)]
pub enum Definition {
Path(PathBuf),
Environment(String),
Cli,
}
impl Definition {
pub fn root<'a>(&'a self, config: &'a Config) -> &'a Path {
match self {
Definition::Path(p) => p.parent().unwrap().parent().unwrap(),
Definition::Environment(_) | Definition::Cli => config.cwd(),
}
}
pub fn is_higher_priority(&self, other: &Definition) -> bool {
matches!(
(self, other),
(Definition::Cli, Definition::Environment(_))
| (Definition::Cli, Definition::Path(_))
| (Definition::Environment(_), Definition::Path(_))
)
}
}
impl PartialEq for Definition {
fn eq(&self, other: &Definition) -> bool {
mem::discriminant(self) == mem::discriminant(other)
}
}
impl fmt::Display for Definition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Definition::Path(p) => p.display().fmt(f),
Definition::Environment(key) => write!(f, "environment variable `{}`", key),
Definition::Cli => write!(f, "--config cli option"),
}
}
}
impl<'de, T> de::Deserialize<'de> for Value<T>
where
T: de::Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Value<T>, D::Error>
where
D: de::Deserializer<'de>,
{
struct ValueVisitor<T> {
_marker: marker::PhantomData<T>,
}
impl<'de, T> de::Visitor<'de> for ValueVisitor<T>
where
T: de::Deserialize<'de>,
{
type Value = Value<T>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a value")
}
fn visit_map<V>(self, mut visitor: V) -> Result<Value<T>, V::Error>
where
V: de::MapAccess<'de>,
{
let value = visitor.next_key::<ValueKey>()?;
if value.is_none() {
return Err(de::Error::custom("value not found"));
}
let val: T = visitor.next_value()?;
let definition = visitor.next_key::<DefinitionKey>()?;
if definition.is_none() {
return Err(de::Error::custom("definition not found"));
}
let definition: Definition = visitor.next_value()?;
Ok(Value { val, definition })
}
}
deserializer.deserialize_struct(
NAME,
&FIELDS,
ValueVisitor {
_marker: marker::PhantomData,
},
)
}
}
struct FieldVisitor {
expected: &'static str,
}
impl<'de> de::Visitor<'de> for FieldVisitor {
type Value = ();
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a valid value field")
}
fn visit_str<E>(self, s: &str) -> Result<(), E>
where
E: de::Error,
{
if s == self.expected {
Ok(())
} else {
Err(de::Error::custom("expected field with custom name"))
}
}
}
struct ValueKey;
impl<'de> de::Deserialize<'de> for ValueKey {
fn deserialize<D>(deserializer: D) -> Result<ValueKey, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_identifier(FieldVisitor {
expected: VALUE_FIELD,
})?;
Ok(ValueKey)
}
}
struct DefinitionKey;
impl<'de> de::Deserialize<'de> for DefinitionKey {
fn deserialize<D>(deserializer: D) -> Result<DefinitionKey, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_identifier(FieldVisitor {
expected: DEFINITION_FIELD,
})?;
Ok(DefinitionKey)
}
}
impl<'de> de::Deserialize<'de> for Definition {
fn deserialize<D>(deserializer: D) -> Result<Definition, D::Error>
where
D: de::Deserializer<'de>,
{
let (discr, value) = <(u32, String)>::deserialize(deserializer)?;
match discr {
0 => Ok(Definition::Path(value.into())),
1 => Ok(Definition::Environment(value)),
2 => Ok(Definition::Cli),
_ => panic!("unexpected discriminant {} value {}", discr, value),
}
}
}