diff --git a/src/bin/installer.rs b/src/bin/installer.rs index b87130d59fc7349e464131caa6ed70eea9d73c4f..bd87130d49727c5ae8cc99b85205295e74cf9552 100644 --- a/src/bin/installer.rs +++ b/src/bin/installer.rs @@ -3,13 +3,13 @@ extern crate redox_installer; extern crate serde; extern crate toml; -use std::io::{Read, Write}; +use std::io::Write; use std::path::Path; use std::{env, fs, io, process}; use arg_parser::ArgParser; -use redox_installer::PackageConfig; +use redox_installer::{Config, PackageConfig}; fn main() { let stderr = io::stderr(); @@ -27,24 +27,11 @@ fn main() { // If not set on the command line or the filesystem config, then build packages from source. let repo_binary = parser.found("repo-binary"); - let mut config_data = String::new(); let mut config = if let Some(path) = parser.get_opt("config") { - match fs::File::open(&path) { - Ok(mut config_file) => match config_file.read_to_string(&mut config_data) { - Ok(_) => match toml::from_str(&config_data) { - Ok(config) => config, - Err(err) => { - writeln!(stderr, "installer: {}: failed to decode: {}", path, err).unwrap(); - process::exit(1); - } - }, - Err(err) => { - writeln!(stderr, "installer: {}: failed to read: {}", path, err).unwrap(); - process::exit(1); - } - }, + match Config::from_file(Path::new(&path)) { + Ok(config) => config, Err(err) => { - writeln!(stderr, "installer: {}: failed to open: {}", path, err).unwrap(); + writeln!(stderr, "installer: {err}").unwrap(); process::exit(1); } } diff --git a/src/bin/installer_tui.rs b/src/bin/installer_tui.rs index 74067a542eafa416363029903c227b3bc0b746f3..fb58264e75a88c4d6125fc4bbb4a3f864ba20dc2 100644 --- a/src/bin/installer_tui.rs +++ b/src/bin/installer_tui.rs @@ -309,24 +309,7 @@ fn main() { efi_partition_size: None, }; let res = with_whole_disk(&disk_path, &disk_option, |mount_path| -> Result<(), failure::Error> { - let mut config: Config = { - let path = root_path.join("filesystem.toml"); - match fs::read_to_string(&path) { - Ok(config_data) => { - match toml::from_str(&config_data) { - Ok(config) => { - config - }, - Err(err) => { - return Err(format_err!("{}: failed to decode: {}", path.display(), err)); - } - } - }, - Err(err) => { - return Err(format_err!("{}: failed to read: {}", path.display(), err)); - } - } - }; + let mut config: Config = Config::from_file(&root_path.join("filesystem.toml"))?; // Copy filesystem.toml, which is not packaged let mut files = vec![ diff --git a/src/config/mod.rs b/src/config/mod.rs index 0348f1e49f56ff3666cd3cd5346503fc10a92d7a..712803b2c8f6e4de1ce416433cbfd97954fa10c2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,7 +1,9 @@ use std::collections::BTreeMap; +use std::fs; +use std::path::Path; -pub mod general; pub mod file; +pub mod general; pub mod package; pub mod user; @@ -15,3 +17,20 @@ pub struct Config { #[serde(default)] pub users: BTreeMap<String, user::UserConfig>, } + +impl Config { + pub fn from_file(path: &Path) -> Result<Self, failure::Error> { + let config = match fs::read_to_string(&path) { + Ok(config_data) => match toml::from_str(&config_data) { + Ok(config) => config, + Err(err) => { + return Err(format_err!("{}: failed to decode: {}", path.display(), err)); + } + }, + Err(err) => { + return Err(format_err!("{}: failed to read: {}", path.display(), err)); + } + }; + Ok(config) + } +}