-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathtoolchain_override.rs
310 lines (275 loc) · 9.64 KB
/
toolchain_override.rs
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use crate::{
channel::{is_dateless_distributed_toolchain, LATEST, NIGHTLY},
constants::{DATE_FORMAT, FUEL_TOOLCHAIN_TOML_FILE},
download::DownloadCfg,
file,
path::get_fuel_toolchain_toml,
target_triple::TargetTriple,
toolchain::{DistToolchainDescription, Toolchain},
};
use anyhow::{bail, Result};
use semver::Version;
use serde::de::Error;
use serde::ser::SerializeStruct;
use serde::{Deserialize, Deserializer, Serialize};
use std::{collections::HashMap, fmt, path::PathBuf, str::FromStr};
use time::Date;
use toml_edit::{de, ser, value, Document};
use tracing::{info, warn};
// For composability with other functionality of fuelup, we want to add
// additional info to OverrideCfg (representation of 'fuel-toolchain.toml').
// In this case, we want the path to the toml file. More info might be
// needed in future.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ToolchainOverride {
pub cfg: OverrideCfg,
pub path: PathBuf,
}
// Representation of the entire 'fuel-toolchain.toml'.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct OverrideCfg {
pub toolchain: ToolchainCfg,
pub components: Option<HashMap<String, Version>>,
}
// Represents the [toolchain] table in 'fuel-toolchain.toml'.
#[derive(Clone, Debug, Deserialize)]
pub struct ToolchainCfg {
#[serde(deserialize_with = "deserialize_channel")]
pub channel: Channel,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Channel {
pub name: String,
pub date: Option<Date>,
}
impl Serialize for ToolchainCfg {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut c = serializer.serialize_struct("ToolchainCfg", 2)?;
c.serialize_field("channel", &self.channel.to_string())?;
c.end()
}
}
pub fn deserialize_channel<'de, D>(deserializer: D) -> Result<Channel, D::Error>
where
D: Deserializer<'de>,
{
let channel_str = String::deserialize(deserializer)?;
channel_str.parse().map_or_else(
|_| {
Err(Error::invalid_value(
serde::de::Unexpected::Str(&channel_str),
&"one of <latest-YYYY-MM-DD|nightly-YYYY-MM-DD|testnet|mainnet>",
))
},
Result::Ok,
)
}
impl fmt::Display for Channel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.date {
Some(d) => write!(f, "{}-{}", self.name, d),
None => write!(f, "{}", self.name),
}
}
}
impl FromStr for Channel {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
if is_dateless_distributed_toolchain(s) {
return Ok(Self {
name: s.to_string(),
date: None,
});
};
if let Some((name, d)) = s.split_once('-') {
Ok(Self {
name: name.to_string(),
date: Date::parse(d, DATE_FORMAT).ok(),
})
} else {
if s == LATEST || s == NIGHTLY {
bail!("'{s}' without date specifier is forbidden");
}
bail!("Invalid str for channel: '{}'", s);
}
}
}
impl ToolchainOverride {
// Creates a representation of a 'fuel-toolchain.toml' from a file path.
// This representation is an OverrideCfg and the file path.
pub(crate) fn from_path(path: PathBuf) -> Result<Self> {
let f = file::read_file(FUEL_TOOLCHAIN_TOML_FILE, path.as_path())?;
let cfg: OverrideCfg = OverrideCfg::from_toml(&f)?;
Ok(Self { cfg, path })
}
#[allow(clippy::indexing_slicing)]
pub fn to_toml(&self) -> Document {
let mut document = toml_edit::Document::new();
document["toolchain"]["channel"] = value(self.cfg.toolchain.channel.to_string());
if let Some(components) = &self.cfg.components {
for (k, v) in components {
document["components"][k] = value(v.to_string());
}
}
document
}
pub fn from_project_root() -> Option<ToolchainOverride> {
if let Some(fuel_toolchain_toml_file) = get_fuel_toolchain_toml() {
match ToolchainOverride::from_path(fuel_toolchain_toml_file) {
Ok(to) => Some(to),
Err(e) => {
warn!("warning: invalid 'fuel-toolchain.toml' in project root: {e}");
None
}
}
} else {
None
}
}
pub fn get_component_version(&self, component: &str) -> Option<&Version> {
if let Some(components) = &self.cfg.components {
components.get(component)
} else {
None
}
}
pub fn install_missing_components(&self, toolchain: &Toolchain, called: &str) -> Result<()> {
match &self.cfg.components {
None => warn!(
"warning: overriding toolchain '{}' in {} does not have any components listed",
&self.cfg.toolchain.channel, FUEL_TOOLCHAIN_TOML_FILE
),
Some(components) => {
for component in components.keys() {
if !toolchain.has_component(component) {
let target_triple = TargetTriple::from_component(component)?;
if let Ok(download_cfg) = DownloadCfg::new(called, target_triple, None) {
info!(
"installing missing component '{}' specified in {}",
component, FUEL_TOOLCHAIN_TOML_FILE
);
toolchain.add_component(download_cfg)?;
};
}
}
}
};
Ok(())
}
}
impl OverrideCfg {
pub fn new(toolchain: ToolchainCfg, components: Option<HashMap<String, Version>>) -> Self {
Self {
toolchain,
components,
}
}
// Creates a representation of a 'fuel-toolchain.toml' from a toml string.
// This is used in the implementation of ToolchainOverride, which is just
// an OverrideCfg with its file path.
pub(crate) fn from_toml(toml: &str) -> Result<Self> {
let cfg: OverrideCfg = de::from_str(toml)?;
if DistToolchainDescription::from_str(&cfg.toolchain.channel.to_string()).is_err() {
bail!("Invalid channel '{}'", &cfg.toolchain.channel)
}
if let Some(components) = cfg.components.as_ref() {
if components.is_empty() {
bail!("'[components]' table is declared with no components")
}
}
Ok(cfg)
}
pub fn to_string_pretty(self) -> Result<String, ser::Error> {
ser::to_string_pretty(&self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::channel::{MAINNET, NIGHTLY, TESTNET};
use indoc::indoc;
#[test]
fn parse_toolchain_override_latest_with_date() {
const TOML: &str = indoc! {r#"
[toolchain]
channel = "latest-2023-01-09"
"#};
let cfg = OverrideCfg::from_toml(TOML).unwrap();
assert_eq!(cfg.toolchain.channel.to_string(), "latest-2023-01-09");
assert!(cfg.components.is_none());
assert_eq!(TOML, cfg.to_string_pretty().unwrap());
}
#[test]
fn parse_toolchain_override_nightly_with_date() {
const TOML: &str = indoc! {r#"
[toolchain]
channel = "nightly-2023-01-09"
[components]
forc = "0.33.0"
"#};
let cfg = OverrideCfg::from_toml(TOML).unwrap();
assert_eq!(cfg.toolchain.channel.to_string(), "nightly-2023-01-09");
assert_eq!(
cfg.components.as_ref().unwrap().get("forc").unwrap(),
&Version::new(0, 33, 0)
);
assert_eq!(TOML, cfg.to_string_pretty().unwrap());
}
#[test]
fn parse_toolchain_override_channel_without_date_error() {
const LATEST: &str = indoc! {r#"
[toolchain]
channel = "latest"
"#};
const NIGHTLY: &str = indoc! {r#"
[toolchain]
channel = "nightly"
"#};
let result = OverrideCfg::from_toml(LATEST);
assert!(result.is_err());
let e = result.unwrap_err();
assert_eq!(e
.to_string(),
"invalid value: string \"latest\", expected one of <latest-YYYY-MM-DD|nightly-YYYY-MM-DD|testnet|mainnet> for key `toolchain.channel`".to_string());
let result = OverrideCfg::from_toml(NIGHTLY);
assert!(result.is_err());
let e = result.unwrap_err();
assert_eq!(e
.to_string(),
"invalid value: string \"nightly\", expected one of <latest-YYYY-MM-DD|nightly-YYYY-MM-DD|testnet|mainnet> for key `toolchain.channel`".to_string());
}
#[test]
fn parse_toolchain_override_invalid_tomls() {
const EMPTY_STR: &str = "";
const EMPTY_TOOLCHAIN: &str = indoc! {r#"
[toolchain]
"#};
const INVALID_CHANNEL: &str = indoc! {r#"
[toolchain]
channel = "invalid-channel"
"#};
const EMPTY_COMPONENTS: &str = indoc! {r#"
[toolchain]
channel = "testnet"
[components]
"#};
for toml in [
EMPTY_STR,
EMPTY_TOOLCHAIN,
INVALID_CHANNEL,
EMPTY_COMPONENTS,
] {
assert!(OverrideCfg::from_toml(toml).is_err());
}
}
#[test]
fn channel_from_str() {
assert!(Channel::from_str(TESTNET).is_ok());
assert!(Channel::from_str(MAINNET).is_ok());
assert!(Channel::from_str(NIGHTLY).is_err());
assert!(Channel::from_str(LATEST).is_err());
}
}