feat!: Account features

- Bump version to 0.2.0
- Added trait SquadObject
- Implemented SquadObject for Quest and Account
- Implemented Config::load_accounts
- Removed src/quest/error.rs
- Added account tests in tests/main.rs

BREAKING CHANGE: Quest::{load,delete,save} are now provided by
SquadObject trait
This commit is contained in:
Alexey 2025-12-02 14:33:38 +03:00
commit 0e8cdde697
12 changed files with 285 additions and 98 deletions

View file

@ -3,7 +3,7 @@
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
use serde::Deserialize;
use crate::{error::Error,quest::Quest};
use crate::{SquadObject, account::Account, error::Error, quest::Quest};
/// Struct for containing paths to other (de-)serializable things
#[derive(Deserialize)]
@ -50,6 +50,22 @@ fn handle_quest_entry(quest_entry: DirEntry) -> Result<Quest, Error>{
Quest::load(path)
}
fn handle_account_entry(account_entry: DirEntry) -> Result<Account, Error>{
let filetype = account_entry.file_type();
if let Err(error) = filetype {
return Err(Error::IoError(error));
}
let path = account_entry.path();
let filetype = filetype.unwrap();
if !filetype.is_file() {
return Err(Error::IsNotAFile(path));
}
Account::load(path)
}
impl Config {
/// Deserialize config from TOML
/// Logs all errors and returns default config if that happens
@ -154,4 +170,71 @@ impl Config {
out_vec
}
/// Returns full path to quests folder
/// This path will be relative to $PWD, not to config.
///
/// # Examples
/// ```rust
/// use squad_quest::config::Config;
///
/// let path = "cfg/config.toml".into();
/// let config = Config::load(path);
///
/// let accounts_path = config.full_accounts_path();
/// ```
pub fn full_accounts_path(&self) -> PathBuf {
let mut path = self.path.clone();
path.push(self.accounts_path.clone());
path
}
/// Load [Vec]<[Account]> from accounts folder.
/// Also logs errors and counts successfully loaded quests.
///
/// # Examples
/// ```rust
/// use squad_quest::{config::Config, account::Account};
///
///
/// let path = "cfg/config.toml".into();
/// let config = Config::load(path);
/// let accounts = config.load_accounts();
///
/// for account in accounts {
/// println!("Account {}", account.id);
/// }
/// ```
pub fn load_accounts(&self) -> Vec<Account> {
let mut out_vec = Vec::new();
let path = self.full_accounts_path();
match fs::read_dir(path) {
Ok(iter) => {
for entry in iter {
match entry {
Ok(acc_entry) => {
match handle_account_entry(acc_entry) {
Ok(quest) => out_vec.push(quest),
Err(error) => {
eprintln!("Error on loading single account: {error}");
}
}
},
Err(error) => {
eprintln!("Error on loading single account: {error}");
}
}
}
},
Err(error) => {
eprintln!("Error on loading accounts: {error}");
}
}
println!("Loaded {} accounts successfully", out_vec.len());
out_vec
}
}