feat: Implemented file hierarchy initialization

- Bump version to 0.5.1
- Added Config::save method
- cli: Added init command
This commit is contained in:
Alexey 2025-12-05 17:16:40 +03:00
commit 2960b6dfc4
5 changed files with 111 additions and 25 deletions

View file

@ -1,12 +1,12 @@
//! Configuration file that handles (de-)serializing other components
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
use serde::Deserialize;
use std::{fs::{self, DirEntry}, io::Write, path::{Path, PathBuf}};
use serde::{Deserialize, Serialize};
use crate::{SquadObject, account::Account, error::Error, quest::Quest};
/// Struct for containing paths to other (de-)serializable things
#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
/// Path to config directory
@ -112,6 +112,42 @@ impl Config {
}
}
/// Serialize config into TOML.
/// Config will be saved as `path/config.toml`
///
/// # Examples
/// ```rust
/// use squad_quest::config::Config;
///
/// let path = "cfg".into();
///
/// let config = Config::default();
///
/// if let Err(error) = config.save(path) {
/// // handle error
/// }
/// ```
pub fn save(&self, path: PathBuf) -> Result<(), Error> {
let mut path = path;
path.push("config.toml");
let str = match toml::to_string_pretty(&self) {
Ok(string) => string,
Err(error) => return Err(Error::TomlSerializeError(error)),
};
let mut file = match fs::File::create(path) {
Ok(f) => f,
Err(error) => return Err(Error::IoError(error)),
};
if let Err(error) = file.write_all(str.as_bytes()) {
return Err(Error::IoError(error));
}
Ok(())
}
/// Deserialize config from TOML
///
/// # Examples