Introduce Config::from_file for parsing the config toml

This commit is contained in:
bjorn3
2024-01-04 14:54:13 +01:00
parent 5cdec46800
commit 1f4bbece4a
3 changed files with 26 additions and 37 deletions
+5 -18
View File
@@ -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);
}
}
+1 -18
View File
@@ -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![
+20 -1
View File
@@ -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)
}
}