forked from facebook/buck2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess.rs
More file actions
210 lines (186 loc) · 6.77 KB
/
Copy pathaccess.rs
File metadata and controls
210 lines (186 loc) · 6.77 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
use std::str::FromStr;
use std::sync::Arc;
use buck2_error::BuckErrorContext;
use buck2_hash::StdBuckHashMap;
use buck2_util::env_vars::substitute_env_vars;
use gazebo::eq_chain;
use crate::legacy_configs::configs::ConfigValue;
use crate::legacy_configs::configs::LegacyBuckConfig;
use crate::legacy_configs::configs::LegacyBuckConfigSection;
use crate::legacy_configs::configs::LegacyBuckConfigValue;
use crate::legacy_configs::key::BuckconfigKeyRef;
use crate::legacy_configs::view::LegacyBuckConfigView;
/// Read the `[buck2_metadata]` section from a `LegacyBuckConfig` and resolve any `$VAR`
/// references. Entries whose env vars are not set are skipped with a warning.
pub fn parse_buckconfig_metadata(config: &LegacyBuckConfig) -> StdBuckHashMap<String, String> {
let mut map = StdBuckHashMap::default();
let Some(section) = config.get_section("buck2_metadata") else {
return map;
};
for (key, value) in section.iter() {
match substitute_env_vars(value.as_str()) {
Ok(resolved) => {
map.insert(key.to_owned(), resolved);
}
Err(e) => {
tracing::warn!("Skipping [buck2_metadata] key `{}`: {:#}", key, e);
}
}
}
map
}
impl LegacyBuckConfigView for &LegacyBuckConfig {
fn get(&mut self, key: BuckconfigKeyRef) -> buck2_error::Result<Option<Arc<str>>> {
Ok(LegacyBuckConfig::get(self, key).map(|v| v.to_owned().into()))
}
}
impl LegacyBuckConfigSection {
/// configs are equal if the data they resolve in is equal, regardless of the origin of the config
pub(crate) fn compare(&self, other: &Self) -> bool {
eq_chain!(
self.values.len() == other.values.len(),
self.values.iter().all(|(name, value)| other
.values
.get(name)
.is_some_and(|other_val| other_val.as_str() == value.as_str()))
)
}
pub fn iter(&self) -> impl Iterator<Item = (&str, LegacyBuckConfigValue<'_>)> {
self.values
.iter()
.map(move |(key, value)| (key.as_str(), LegacyBuckConfigValue { value }))
}
pub fn keys(&self) -> impl Iterator<Item = &String> {
self.values.keys()
}
pub fn get(&self, key: &str) -> Option<LegacyBuckConfigValue<'_>> {
self.values
.get(key)
.map(move |value| LegacyBuckConfigValue { value })
}
}
impl LegacyBuckConfig {
fn get_config_value(&self, key: BuckconfigKeyRef) -> Option<&ConfigValue> {
let BuckconfigKeyRef { section, property } = key;
self.0
.values
.get(section)
.and_then(|s| s.values.get(property))
}
pub fn get(&self, key: BuckconfigKeyRef) -> Option<&str> {
self.get_config_value(key).map(|s| s.as_str())
}
/// Iterate all entries.
pub fn iter(&self) -> impl Iterator<Item = (&str, impl IntoIterator<Item = (&str, &str)>)> {
self.0.values.iter().map(|(section, section_values)| {
(
section.as_str(),
section_values
.values
.iter()
.map(|(key, value)| (key.as_str(), value.as_str())),
)
})
}
fn parse_impl<T: FromStr>(key: BuckconfigKeyRef, value: &str) -> buck2_error::Result<T>
where
buck2_error::Error: From<<T as FromStr>::Err>,
{
let BuckconfigKeyRef { section, property } = key;
value
.parse()
.map_err(buck2_error::Error::from)
.with_buck_error_context(|| {
format!(
"Invalid value for buckconfig `{}.{}`: conversion to {} failed, value as `{}`",
section.to_owned(),
property.to_owned(),
std::any::type_name::<T>(),
value.to_owned(),
)
})
}
pub fn parse<T: FromStr>(&self, key: BuckconfigKeyRef) -> buck2_error::Result<Option<T>>
where
buck2_error::Error: From<<T as FromStr>::Err>,
{
self.get_config_value(key)
.map(|s| {
Self::parse_impl(key, s.as_str()).with_buck_error_context(|| {
format!("Defined {}", s.source.as_legacy_buck_config_location())
})
})
.transpose()
}
pub fn parse_value<T: FromStr>(
key: BuckconfigKeyRef,
value: Option<&str>,
) -> buck2_error::Result<Option<T>>
where
buck2_error::Error: From<<T as FromStr>::Err>,
{
value.map(|s| Self::parse_impl(key, s)).transpose()
}
pub fn parse_list<T: FromStr>(
&self,
key: BuckconfigKeyRef,
) -> buck2_error::Result<Option<Vec<T>>>
where
buck2_error::Error: From<<T as FromStr>::Err>,
{
Self::parse_list_value(key, self.get(key))
}
pub fn parse_list_value<T: FromStr>(
key: BuckconfigKeyRef,
value: Option<&str>,
) -> buck2_error::Result<Option<Vec<T>>>
where
buck2_error::Error: From<<T as FromStr>::Err>,
{
/// A wrapper type so we can use .parse() on this.
struct ParseList<T>(Vec<T>);
impl<T> FromStr for ParseList<T>
where
T: FromStr,
{
type Err = <T as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(
s.split(',').map(T::from_str).collect::<Result<_, _>>()?,
))
}
}
Ok(Self::parse_value::<ParseList<T>>(key, value)?.map(|l| l.0))
}
pub fn sections(&self) -> impl Iterator<Item = &String> {
self.0.values.keys()
}
pub fn all_sections(&self) -> impl Iterator<Item = (&String, &LegacyBuckConfigSection)> + '_ {
self.0.values.iter()
}
pub fn get_section(&self, section: &str) -> Option<&LegacyBuckConfigSection> {
self.0.values.get(section)
}
/// configs are equal if the data they resolve in is equal, regardless of the origin of the config
pub(crate) fn compare(&self, other: &Self) -> bool {
eq_chain!(
self.0.values.len() == other.0.values.len(),
self.0.values.iter().all(|(section_name, section)| {
other
.0
.values
.get(section_name)
.is_some_and(|other_sec| other_sec.compare(section))
})
)
}
}