forked from rust-lang/rustup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfallback_settings.rs
More file actions
33 lines (29 loc) · 1.27 KB
/
fallback_settings.rs
File metadata and controls
33 lines (29 loc) · 1.27 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
use serde::Deserialize;
use std::io;
use std::path::Path;
use anyhow::{Context, Result};
use crate::utils::utils;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Default)]
pub struct FallbackSettings {
pub default_toolchain: Option<String>,
}
impl FallbackSettings {
pub(crate) fn new<P: AsRef<Path>>(path: P) -> Result<Option<Self>> {
// Users cannot fix issues with missing/unreadable/invalid centralised files, but logging isn't setup early so
// we can't simply trap all errors and log diagnostics. Ideally we would, and then separate these into different
// sorts of issues, logging messages about errors that should be fixed. Instead we trap some conservative errors
// that hopefully won't lead to too many tickets.
match utils::read_file("fallback settings", path.as_ref()) {
Err(e) => match e.downcast_ref::<io::Error>() {
Some(io_err) => match io_err.kind() {
io::ErrorKind::NotFound | io::ErrorKind::PermissionDenied => Ok(None),
_ => Err(e),
},
None => Err(e),
},
Ok(file_contents) => Ok(Some(
toml::from_str(&file_contents).context("error parsing settings")?,
)),
}
}
}