Skip to content

Commit

Permalink
Use a .toml file to store settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Diggsey committed May 8, 2016
1 parent 0de142a commit d1ae0eb
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 235 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ scopeguard = "0.1.2"
rustc-serialize = "0.3"
sha2 = "0.1.2"
markdown = { git="https://github.com/Diggsey/markdown.rs.git" }
toml = "0.1.27"

[target."cfg(windows)".dependencies]
winapi = "0.2.4"
Expand Down
24 changes: 5 additions & 19 deletions src/rustup-cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use self_update;
use std::io::{Write, Read, BufRead};
use std::process::Command;
use std::{cmp, iter};
use std::str::FromStr;
use std;
use term2;

Expand Down Expand Up @@ -321,19 +320,15 @@ pub fn list_toolchains(cfg: &Cfg) -> Result<()> {
}

pub fn list_overrides(cfg: &Cfg) -> Result<()> {
let mut overrides = try!(cfg.override_db.list());

overrides.sort();
let overrides = try!(cfg.settings_file.with(|s| Ok(s.overrides.clone())));

if overrides.is_empty() {
println!("no overrides");
} else {
for o in overrides {
split_override::<String>(&o, ';').map(|li|
println!("{:<40}\t{:<20}",
utils::format_path_for_display(&li.0),
li.1)
);
for (k, v) in overrides {
println!("{:<40}\t{:<20}",
utils::format_path_for_display(&k),
v)
}
}
Ok(())
Expand All @@ -344,15 +339,6 @@ pub fn version() -> &'static str {
concat!(env!("CARGO_PKG_VERSION"), include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
}

fn split_override<T: FromStr>(s: &str, separator: char) -> Option<(T, T)> {
s.find(separator).and_then(|index| {
match (T::from_str(&s[..index]), T::from_str(&s[index + 1..])) {
(Ok(l), Ok(r)) => Some((l, r)),
_ => None
}
})
}


pub fn report_error(e: &Error) {
err!("{}", e);
Expand Down
12 changes: 5 additions & 7 deletions src/rustup-cli/multirust_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,13 @@ fn remove_override(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref path = m.value_of("override")
.map(|p| PathBuf::from(p)).unwrap_or(cwd);

if try!(cfg.override_db.find(path, cfg.notify_handler.as_ref())).is_none() {
if try!(cfg.settings_file.with_mut(|s| {
Ok(s.remove_override(path, cfg.notify_handler.as_ref()))
})) {
info!("override toolchain for '{}' removed", path.display());
} else {
info!("no override toolchain for '{}'", path.display());
return Ok(());
}

try!(cfg.override_db.remove(path,
&cfg.temp_cfg,
cfg.notify_handler.as_ref()));
info!("override toolchain for '{}' removed", path.display());
Ok(())
}

Expand Down
13 changes: 5 additions & 8 deletions src/rustup-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,13 @@ fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn override_remove(cfg: &Cfg) -> Result<()> {
let ref path = try!(utils::current_dir());

let ref override_db = cfg.override_db;
let notify_handler = cfg.notify_handler.as_ref();

if try!(override_db.find(path, notify_handler)).is_none() {
if try!(cfg.settings_file.with_mut(|s| {
Ok(s.remove_override(path, cfg.notify_handler.as_ref()))
})) {
info!("override toolchain for '{}' removed", path.display());
} else {
info!("no override toolchain for '{}'", path.display());
return Ok(());
}

try!(override_db.remove(path, &cfg.temp_cfg, notify_handler));
info!("override toolchain for '{}' removed", path.display());
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/rustup-dist/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use toml;

use toml_utils::*;
use rustup_utils::toml_utils::*;
use errors::*;
use super::manifest::Component;

Expand Down
6 changes: 1 addition & 5 deletions src/rustup-dist/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ error_chain! {
Parsing(e: Vec<toml::ParserError>) {
description("error parsing manifest")
}
ExpectedType(t: &'static str, n: String) {
description("expected type")
display("expected type: '{}' for '{}'", t, n)
}
UnsupportedVersion(v: String) {
description("unsupported manifest version")
display("manifest version '{}' is not supported", v)
Expand All @@ -116,7 +112,7 @@ fn component_unavailable_msg(cs: &[Component]) -> String {
assert!(!cs.is_empty());

let mut buf = vec![];

if cs.len() == 1 {
let _ = write!(buf, "component '{}' for '{}' is unavailable for download",
cs[0].pkg, cs[0].target);
Expand Down
1 change: 0 additions & 1 deletion src/rustup-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ pub mod manifestation;
pub mod download;
pub mod manifest;
pub mod config;
mod toml_utils;
2 changes: 1 addition & 1 deletion src/rustup-dist/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use errors::*;
use toml;
use toml_utils::*;
use rustup_utils::toml_utils::*;

use std::collections::HashMap;
use dist::TargetTriple;
Expand Down
1 change: 1 addition & 0 deletions src/rustup-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ libc = "0.2.0"
native-tls = { git = "https://github.com/sfackler/rust-native-tls.git" }
rustc-serialize = "0.3.19"
sha2 = "0.1.2"
toml = "0.1.27"

[target.'cfg(not(any(target_os = "windows", target_os = "macos")))'.dependencies]
openssl-sys = "0.7.11"
Expand Down
4 changes: 4 additions & 0 deletions src/rustup-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ error_chain! {
description("could not create directory")
display("could not crate {} directory: '{}'", name, path.display())
}
ExpectedType(t: &'static str, n: String) {
description("expected type")
display("expected type: '{}' for '{}'", t, n)
}
FilteringFile {
name: &'static str,
src: PathBuf,
Expand Down
2 changes: 2 additions & 0 deletions src/rustup-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate error_chain;
extern crate native_tls;
extern crate rustc_serialize;
extern crate sha2;
extern crate toml;
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
extern crate openssl_sys;

Expand Down Expand Up @@ -37,6 +38,7 @@ pub mod notifications;
pub mod raw;
pub mod tty;
pub mod utils;
pub mod toml_utils;

pub use errors::*;
pub use notifications::{Notification, NotifyHandler};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ pub fn get_string(table: &mut toml::Table, key: &str, path: &str) -> Result<Stri
})
}

pub fn get_opt_string(table: &mut toml::Table, key: &str, path: &str) -> Result<Option<String>> {
if let Ok(v) = get_value(table, key, path) {
if let toml::Value::String(s) = v {
Ok(Some(s))
} else {
Err(ErrorKind::ExpectedType("string", path.to_owned() + key).into())
}
} else {
Ok(None)
}
}

pub fn get_bool(table: &mut toml::Table, key: &str, path: &str) -> Result<bool> {
get_value(table, key, path).and_then(|v| {
if let toml::Value::Boolean(b) = v {
Expand Down
Loading

0 comments on commit d1ae0eb

Please sign in to comment.