Skip to content

Commit

Permalink
First sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmohrin committed Sep 14, 2020
1 parent 30b7c5f commit 927fc78
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
49 changes: 32 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::env;
use std::fs;
use std::io;
use std::io::{Error as IoError, Read, Write};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::time::Duration;

use ansi_term::{Color, Style};
Expand Down Expand Up @@ -225,26 +226,40 @@ fn map_io_err_to_config_err(e: IoError) -> TealdeerError {
ConfigError(format!("Io Error: {}", e))
}

fn load_raw_config(path: &Path) -> Result<RawConfig, io::Error> {
if path.exists() && path.is_file() {
let mut config_file = fs::File::open(path)?;
let mut contents = String::new();
let _ = config_file.read_to_string(&mut contents)?;
toml::from_str(&contents).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
} else {
Err(io::Error::new(
io::ErrorKind::NotFound,
"Given path was not a file.",
))
}
}

impl Config {
pub fn load(enable_styles: bool) -> Result<Self, TealdeerError> {
pub fn load(config_path: &Option<String>, enable_styles: bool) -> Result<Self, TealdeerError> {
debug!("Loading config");

// Determine path
let config_file_path = get_config_path()
.map_err(|e| ConfigError(format!("Could not determine config path: {}", e)))?;

// Load raw config
let raw_config: RawConfig = if config_file_path.exists() && config_file_path.is_file() {
let mut config_file =
fs::File::open(config_file_path).map_err(map_io_err_to_config_err)?;
let mut contents = String::new();
let _ = config_file
.read_to_string(&mut contents)
.map_err(map_io_err_to_config_err)?;
toml::from_str(&contents)
.map_err(|err| ConfigError(format!("Failed to parse config file: {}", err)))?
let raw_config = if let Some(path) = config_path {
// If a specific path was given, there really should be a file there,
// so *all* errors are propagated.
load_raw_config(&PathBuf::from(path)).map_err(map_io_err_to_config_err)?
} else {
RawConfig::new()
let path = get_config_path()
.map_err(|e| ConfigError(format!("Could not determine config path: {}", e)))?;
match load_raw_config(&path) {
Ok(config) => config,
Err(err) => match err.kind() {
// In contrast, if no file is found at the implicitly determined location, the
// default configuration should be used. All other errors are propagated though.
io::ErrorKind::NotFound => RawConfig::new(),
_ => return Err(map_io_err_to_config_err(err)),
},
}
};

// Convert to config
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Options:
-q --quiet Suppress informational messages
--config-path Show config file path
--seed-config Create a basic config
--config <path> Override config file location
--color <when> Control when to use color [always, auto, never] [default: auto]
Examples:
Expand Down Expand Up @@ -107,6 +108,7 @@ struct Args {
flag_quiet: bool,
flag_config_path: bool,
flag_seed_config: bool,
flag_config: Option<String>,
flag_markdown: bool,
flag_color: ColorOptions,
}
Expand Down Expand Up @@ -337,7 +339,7 @@ fn main() {
};

// Look up config file, if none is found fall back to default config.
let config = match Config::load(enable_styles) {
let config = match Config::load(&args.flag_config, enable_styles) {
Ok(config) => config,
Err(ConfigError(msg)) => {
eprintln!("Could not load config: {}", msg);
Expand Down

0 comments on commit 927fc78

Please sign in to comment.