Skip to content

Commit 7aed7d0

Browse files
committed
Use serde to encode/decode config
1 parent 3f80e43 commit 7aed7d0

File tree

5 files changed

+10
-125
lines changed

5 files changed

+10
-125
lines changed

src/dist/config.rs

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,34 @@ use std::fmt;
22
use std::str::FromStr;
33

44
use anyhow::{Context, Result};
5+
use serde::{Deserialize, Serialize};
56

67
use super::manifest::Component;
78
use crate::errors::*;
8-
use crate::utils::toml_utils::*;
99

10-
#[derive(Clone, Debug, Default)]
10+
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
1111
pub struct Config {
1212
pub config_version: ConfigVersion,
1313
pub components: Vec<Component>,
1414
}
1515

1616
impl Config {
17-
pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
18-
let config_version = get_string(&mut table, "config_version", path)?;
19-
let config_version = ConfigVersion::from_str(&config_version)?;
20-
21-
let components = get_array(&mut table, "components", path)?;
22-
let components =
23-
Self::toml_to_components(components, &format!("{}{}.", path, "components"))?;
24-
25-
Ok(Self {
26-
config_version,
27-
components,
28-
})
29-
}
30-
pub(crate) fn into_toml(self) -> toml::value::Table {
31-
let mut result = toml::value::Table::new();
32-
result.insert(
33-
"config_version".to_owned(),
34-
toml::Value::String(self.config_version.as_str().to_owned()),
35-
);
36-
let components = Self::components_to_toml(self.components);
37-
if !components.is_empty() {
38-
result.insert("components".to_owned(), toml::Value::Array(components));
39-
}
40-
result
41-
}
42-
4317
pub(crate) fn parse(data: &str) -> Result<Self> {
44-
let value = toml::from_str(data).context("error parsing config")?;
45-
Self::from_toml(value, "")
46-
}
47-
48-
pub(crate) fn stringify(self) -> String {
49-
self.into_toml().to_string()
18+
toml::from_str(data).context("error parsing config")
5019
}
5120

52-
fn toml_to_components(arr: toml::value::Array, path: &str) -> Result<Vec<Component>> {
53-
let mut result = Vec::new();
54-
55-
for (i, v) in arr.into_iter().enumerate() {
56-
if let toml::Value::Table(t) = v {
57-
let path = format!("{path}[{i}]");
58-
result.push(Component::from_toml(t, &path, false)?);
59-
}
60-
}
61-
62-
Ok(result)
63-
}
64-
65-
fn components_to_toml(components: Vec<Component>) -> toml::value::Array {
66-
let mut result = toml::value::Array::new();
67-
for v in components {
68-
result.push(toml::Value::Table(v.into_toml()));
69-
}
70-
result
21+
pub(crate) fn stringify(&self) -> Result<String> {
22+
Ok(toml::to_string(&self)?)
7123
}
7224

7325
pub(crate) fn new() -> Self {
7426
Default::default()
7527
}
7628
}
7729

78-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
30+
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
7931
pub(crate) enum ConfigVersion {
32+
#[serde(rename = "1")]
8033
#[default]
8134
V1,
8235
}

src/dist/manifest.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ use std::str::FromStr;
2121
use anyhow::{anyhow, bail, Context, Result};
2222
use serde::{Deserialize, Serialize};
2323

24+
use super::{config::Config, dist::ToolchainDesc};
2425
use crate::dist::dist::{Profile, TargetTriple};
2526
use crate::errors::*;
2627
use crate::toolchain::distributable::DistributableToolchain;
27-
use crate::utils::toml_utils::*;
28-
29-
use super::{config::Config, dist::ToolchainDesc};
3028

3129
/// Used by the `installed_components` function
3230
pub(crate) struct ComponentStatus {
@@ -529,36 +527,7 @@ impl Component {
529527
is_extension: false,
530528
}
531529
}
532-
pub(crate) fn from_toml(
533-
mut table: toml::value::Table,
534-
path: &str,
535-
is_extension: bool,
536-
) -> Result<Self> {
537-
Ok(Self {
538-
pkg: get_string(&mut table, "pkg", path)?,
539-
target: get_string(&mut table, "target", path).map(|s| {
540-
if s == "*" {
541-
None
542-
} else {
543-
Some(TargetTriple::new(s))
544-
}
545-
})?,
546-
is_extension,
547-
})
548-
}
549-
pub(crate) fn into_toml(self) -> toml::value::Table {
550-
let mut result = toml::value::Table::new();
551-
result.insert(
552-
"target".to_owned(),
553-
toml::Value::String(
554-
self.target
555-
.map(|t| t.to_string())
556-
.unwrap_or_else(|| "*".to_owned()),
557-
),
558-
);
559-
result.insert("pkg".to_owned(), toml::Value::String(self.pkg));
560-
result
561-
}
530+
562531
pub(crate) fn name(&self, manifest: &Manifest) -> String {
563532
let pkg = self.short_name(manifest);
564533
if let Some(ref t) = self.target {

src/dist/manifestation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl Manifestation {
287287
// name/target. Needs to be fixed in rust-installer.
288288
let mut new_config = Config::new();
289289
new_config.components = update.final_component_list;
290-
let config_str = new_config.stringify();
290+
let config_str = new_config.stringify()?;
291291
let rel_config_path = prefix.rel_manifest_file(CONFIG_FILE);
292292
let config_path = prefix.path().join(&rel_config_path);
293293
tx.modify_file(rel_config_path)?;

src/utils/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Utility functions for Rustup
22
pub(crate) mod notifications;
33
pub mod raw;
4-
pub(crate) mod toml_utils;
54
pub(crate) mod units;
65
#[allow(clippy::module_inception)]
76
pub mod utils;

src/utils/toml_utils.rs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)