refactor(cli)!: Moved CLI stuff to crate::cli
- Bump version to 0.4.0 - Added Config::try_load - Added Config.verbose field - Made Config.path public - Added -q/--quiet flag to CLI BREAKING CHANGE: Moved CLI-related objects to squad-quest-cli::cli
This commit is contained in:
parent
47f55105dd
commit
790fa88fe3
10 changed files with 529 additions and 453 deletions
|
|
@ -11,7 +11,7 @@ use crate::{SquadObject, account::Account, error::Error, quest::Quest};
|
|||
pub struct Config {
|
||||
/// Path to config directory
|
||||
#[serde(skip)]
|
||||
path: PathBuf,
|
||||
pub path: PathBuf,
|
||||
|
||||
/// Path to serialized [quests][`crate::quest::Quest`] folder
|
||||
pub quests_path: PathBuf,
|
||||
|
|
@ -20,7 +20,10 @@ pub struct Config {
|
|||
pub accounts_path: PathBuf,
|
||||
|
||||
/// Path to serialized [map][`crate::map::Map`] file
|
||||
pub map: PathBuf
|
||||
pub map: PathBuf,
|
||||
|
||||
/// If true, print to std{out/err}
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
|
|
@ -29,7 +32,8 @@ impl Default for Config {
|
|||
path: ".".into(),
|
||||
quests_path: "quests".into(),
|
||||
accounts_path: "accounts".into(),
|
||||
map: "map.toml".into()
|
||||
map: "map.toml".into(),
|
||||
verbose: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -67,8 +71,12 @@ fn handle_account_entry(account_entry: DirEntry) -> Result<Account, Error>{
|
|||
}
|
||||
|
||||
impl Config {
|
||||
/// Deserialize config from TOML
|
||||
/// Logs all errors and returns default config if that happens
|
||||
/// Deserialize config from TOML.
|
||||
///
|
||||
/// This function wraps [try_load][Config::try_load].
|
||||
///
|
||||
/// Logs all errors if `config.verbose == true`.
|
||||
/// Returns default config on error.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
|
|
@ -81,25 +89,62 @@ impl Config {
|
|||
let dir = path.parent()
|
||||
.unwrap_or(Path::new("."))
|
||||
.to_owned();
|
||||
|
||||
match Self::try_load(path) {
|
||||
Ok(conf) => {
|
||||
if conf.verbose {
|
||||
println!("Successfully loaded config");
|
||||
}
|
||||
conf
|
||||
},
|
||||
Err(error) => {
|
||||
let conf = Config {
|
||||
path: dir,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
if conf.verbose {
|
||||
println!("Error while loading config: {error}");
|
||||
}
|
||||
|
||||
conf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserialize config from TOML
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use squad_quest::{config::Config,error::Error};
|
||||
/// # fn main() {
|
||||
/// # let _ = wrapper();
|
||||
/// # }
|
||||
/// # fn wrapper() -> Result<(), Error> {
|
||||
/// let path = "cfg/config.toml".into();
|
||||
/// let config = Config::try_load(path)?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn try_load(path: PathBuf) -> Result<Self, Error> {
|
||||
let dir = path.parent()
|
||||
.unwrap_or(Path::new("."))
|
||||
.to_owned();
|
||||
|
||||
match fs::read_to_string(path) {
|
||||
Ok(string) => {
|
||||
match toml::from_str::<Config>(&string) {
|
||||
Ok(mut conf) => {
|
||||
println!("Successfully loaded config");
|
||||
conf.path = dir;
|
||||
conf
|
||||
Ok(conf)
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Error on parsing config: {error}");
|
||||
let mut cfg = Config::default();
|
||||
cfg.path = dir;
|
||||
cfg
|
||||
Err(Error::TomlDeserializeError(error))
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Error on reading config path: {error}");
|
||||
Config::default()
|
||||
Err(Error::IoError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -150,23 +195,28 @@ impl Config {
|
|||
Ok(quest_entry) => {
|
||||
match handle_quest_entry(quest_entry) {
|
||||
Ok(quest) => out_vec.push(quest),
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading single quest: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading single quest: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading quests: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
if self.verbose {
|
||||
println!("Loaded {} quests successfully", out_vec.len());
|
||||
}
|
||||
|
||||
println!("Loaded {} quests successfully", out_vec.len());
|
||||
|
||||
out_vec
|
||||
}
|
||||
|
|
@ -217,23 +267,28 @@ impl Config {
|
|||
Ok(acc_entry) => {
|
||||
match handle_account_entry(acc_entry) {
|
||||
Ok(quest) => out_vec.push(quest),
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading single account: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading single account: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
Err(error) if self.verbose => {
|
||||
eprintln!("Error on loading accounts: {error}");
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
|
||||
println!("Loaded {} accounts successfully", out_vec.len());
|
||||
if self.verbose {
|
||||
println!("Loaded {} accounts successfully", out_vec.len());
|
||||
}
|
||||
|
||||
out_vec
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue