Skip to content

Commit e598bd5

Browse files
committed
Use serde to encode/decode settings
1 parent df4b9ae commit e598bd5

File tree

4 files changed

+31
-111
lines changed

4 files changed

+31
-111
lines changed

src/cli/self_update.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use std::str::FromStr;
5757
use anyhow::{anyhow, Context, Result};
5858
use cfg_if::cfg_if;
5959
use same_file::Handle;
60+
use serde::{Deserialize, Serialize};
6061

6162
use crate::currentprocess::terminalsource;
6263
use crate::{
@@ -98,7 +99,8 @@ pub(crate) const NEVER_SELF_UPDATE: bool = true;
9899
#[cfg(not(feature = "no-self-update"))]
99100
pub(crate) const NEVER_SELF_UPDATE: bool = false;
100101

101-
#[derive(Clone, Debug, Eq, PartialEq)]
102+
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
103+
#[serde(rename_all = "kebab-case")]
102104
pub enum SelfUpdateMode {
103105
Enable,
104106
Disable,

src/dist/dist.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use anyhow::{anyhow, bail, Context, Result};
1010
use chrono::NaiveDate;
1111
use once_cell::sync::Lazy;
1212
use regex::Regex;
13+
use serde::{Deserialize, Serialize};
1314
use thiserror::Error as ThisError;
1415

1516
pub(crate) use crate::dist::triple::*;
@@ -600,7 +601,8 @@ impl TryFrom<&ToolchainName> for ToolchainDesc {
600601
}
601602
}
602603

603-
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
604+
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
605+
#[serde(rename_all = "kebab-case")]
604606
pub enum Profile {
605607
Minimal,
606608
#[default]

src/settings.rs

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use std::path::{Path, PathBuf};
55
use std::str::FromStr;
66

77
use anyhow::{Context, Result};
8+
use serde::{Deserialize, Serialize};
89

910
use crate::cli::self_update::SelfUpdateMode;
1011
use crate::dist::dist::Profile;
1112
use crate::errors::*;
1213
use crate::notifications::*;
13-
use crate::utils::toml_utils::*;
1414
use crate::utils::utils;
1515

1616
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -28,8 +28,12 @@ impl SettingsFile {
2828
}
2929

3030
fn write_settings(&self) -> Result<()> {
31-
let s = self.cache.borrow().as_ref().unwrap().clone();
32-
utils::write_file("settings", &self.path, &s.stringify())?;
31+
let settings = self.cache.borrow();
32+
utils::write_file(
33+
"settings",
34+
&self.path,
35+
&settings.as_ref().unwrap().stringify()?,
36+
)?;
3337
Ok(())
3438
}
3539

@@ -74,14 +78,20 @@ impl SettingsFile {
7478
}
7579
}
7680

77-
#[derive(Clone, Debug, Default, Eq, PartialEq)]
81+
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
7882
pub struct Settings {
7983
pub version: MetadataVersion,
84+
#[serde(skip_serializing_if = "Option::is_none")]
8085
pub default_host_triple: Option<String>,
86+
#[serde(skip_serializing_if = "Option::is_none")]
8187
pub default_toolchain: Option<String>,
88+
#[serde(skip_serializing_if = "Option::is_none")]
8289
pub profile: Option<Profile>,
90+
#[serde(default)]
8391
pub overrides: BTreeMap<String, String>,
92+
#[serde(skip_serializing_if = "Option::is_none")]
8493
pub pgp_keys: Option<String>,
94+
#[serde(skip_serializing_if = "Option::is_none")]
8595
pub auto_self_update: Option<SelfUpdateMode>,
8696
}
8797

@@ -126,97 +136,19 @@ impl Settings {
126136
}
127137

128138
pub(crate) fn parse(data: &str) -> Result<Self> {
129-
let value = toml::from_str(data).context("error parsing settings")?;
130-
Self::from_toml(value, "")
139+
toml::from_str(data).context("error parsing settings")
131140
}
132141

133-
pub(crate) fn stringify(self) -> String {
134-
self.into_toml().to_string()
135-
}
136-
137-
pub(crate) fn from_toml(mut table: toml::value::Table, path: &str) -> Result<Self> {
138-
let version = get_string(&mut table, "version", path)?;
139-
let version = MetadataVersion::from_str(&version)?;
140-
141-
let auto_self_update = get_opt_string(&mut table, "auto_self_update", path)?
142-
.and_then(|mode| SelfUpdateMode::from_str(mode.as_str()).ok());
143-
let profile = get_opt_string(&mut table, "profile", path)?
144-
.and_then(|p| Profile::from_str(p.as_str()).ok());
145-
Ok(Self {
146-
version,
147-
default_host_triple: get_opt_string(&mut table, "default_host_triple", path)?,
148-
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
149-
profile,
150-
overrides: Self::table_to_overrides(&mut table, path)?,
151-
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
152-
auto_self_update,
153-
})
154-
}
155-
pub(crate) fn into_toml(self) -> toml::value::Table {
156-
let mut result = toml::value::Table::new();
157-
158-
result.insert(
159-
"version".to_owned(),
160-
toml::Value::String(self.version.as_str().to_owned()),
161-
);
162-
163-
if let Some(v) = self.default_host_triple {
164-
result.insert("default_host_triple".to_owned(), toml::Value::String(v));
165-
}
166-
167-
if let Some(v) = self.default_toolchain {
168-
result.insert("default_toolchain".to_owned(), toml::Value::String(v));
169-
}
170-
171-
if let Some(v) = self.profile {
172-
result.insert("profile".to_owned(), toml::Value::String(v.to_string()));
173-
}
174-
175-
if let Some(v) = self.pgp_keys {
176-
result.insert("pgp_keys".to_owned(), toml::Value::String(v));
177-
}
178-
179-
if let Some(v) = self.auto_self_update {
180-
result.insert(
181-
"auto_self_update".to_owned(),
182-
toml::Value::String(v.to_string()),
183-
);
184-
}
185-
186-
let overrides = Self::overrides_to_table(self.overrides);
187-
result.insert("overrides".to_owned(), toml::Value::Table(overrides));
188-
189-
result
190-
}
191-
192-
fn table_to_overrides(
193-
table: &mut toml::value::Table,
194-
path: &str,
195-
) -> Result<BTreeMap<String, String>> {
196-
let mut result = BTreeMap::new();
197-
let pkg_table = get_table(table, "overrides", path)?;
198-
199-
for (k, v) in pkg_table {
200-
if let toml::Value::String(t) = v {
201-
result.insert(k, t);
202-
}
203-
}
204-
205-
Ok(result)
206-
}
207-
208-
fn overrides_to_table(overrides: BTreeMap<String, String>) -> toml::value::Table {
209-
let mut result = toml::value::Table::new();
210-
for (k, v) in overrides {
211-
result.insert(k, toml::Value::String(v));
212-
}
213-
result
142+
fn stringify(&self) -> Result<String> {
143+
Ok(toml::to_string(self)?)
214144
}
215145
}
216146

217-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
147+
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
218148
pub(crate) enum MetadataVersion {
149+
#[serde(rename = "2")]
219150
V2,
151+
#[serde(rename = "12")]
220152
#[default]
221153
V12,
222154
}
@@ -255,7 +187,7 @@ mod tests {
255187
#[test]
256188
fn serialize_default() {
257189
let settings = Settings::default();
258-
let toml = settings.stringify();
190+
let toml = settings.stringify().unwrap();
259191
assert_eq!(
260192
toml,
261193
r#"version = "12"
@@ -281,8 +213,8 @@ mod tests {
281213
..Default::default()
282214
};
283215

284-
let toml = settings.stringify();
285-
assert_eq!(toml, BASIC,);
216+
let toml = settings.stringify().unwrap();
217+
assert_eq!(toml, BASIC);
286218
}
287219

288220
#[test]
@@ -296,9 +228,9 @@ mod tests {
296228
assert_eq!(settings.profile, Some(Profile::Default));
297229
}
298230

299-
const BASIC: &str = r#"default_toolchain = "stable-aarch64-apple-darwin"
231+
const BASIC: &str = r#"version = "12"
232+
default_toolchain = "stable-aarch64-apple-darwin"
300233
profile = "default"
301-
version = "12"
302234
303235
[overrides]
304236
"#;

src/utils/toml_utils.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,6 @@ pub(crate) fn get_string(table: &mut toml::value::Table, key: &str, path: &str)
1919
}
2020
}
2121

22-
pub(crate) fn get_opt_string(
23-
table: &mut toml::value::Table,
24-
key: &str,
25-
path: &str,
26-
) -> Result<Option<String>> {
27-
if let Ok(v) = get_value(table, key, path) {
28-
if let toml::Value::String(s) = v {
29-
Ok(Some(s))
30-
} else {
31-
Err(ExpectedType("string", path.to_owned() + key).into())
32-
}
33-
} else {
34-
Ok(None)
35-
}
36-
}
37-
3822
pub(crate) fn get_bool(table: &mut toml::value::Table, key: &str, path: &str) -> Result<bool> {
3923
get_value(table, key, path).and_then(|v| {
4024
if let toml::Value::Boolean(b) = v {

0 commit comments

Comments
 (0)