From 1f4bbece4ae83e4c7725b1f7779ca027025091c6 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:54:13 +0100 Subject: [PATCH] Introduce Config::from_file for parsing the config toml --- src/bin/installer.rs | 23 +++++------------------ src/bin/installer_tui.rs | 19 +------------------ src/config/mod.rs | 21 ++++++++++++++++++++- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/bin/installer.rs b/src/bin/installer.rs index b87130d..bd87130 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 74067a5..fb58264 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 0348f1e..712803b 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) + } +} -- GitLab