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:
parent
1dc7d94785
commit
0e8cdde697
12 changed files with 285 additions and 98 deletions
|
|
@ -3,7 +3,7 @@
|
|||
use std::{fs, io::Write, path::PathBuf};
|
||||
|
||||
use serde::{ Serialize, Deserialize };
|
||||
use crate::error::Error;
|
||||
use crate::{SquadObject, error::Error};
|
||||
use toml::value::Date;
|
||||
|
||||
/// Difficulty of the quest
|
||||
|
|
@ -85,29 +85,12 @@ impl Default for Quest {
|
|||
}
|
||||
}
|
||||
|
||||
impl Quest {
|
||||
/// Parse quest TOML or return error
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use squad_quest::{error::Error,quest::Quest};
|
||||
/// # fn main() {
|
||||
/// # let _ = wrapper();
|
||||
/// # }
|
||||
///
|
||||
/// # fn wrapper() -> Result<(), Error> {
|
||||
/// let path = "quests/0.toml".into();
|
||||
///
|
||||
/// let quest = Quest::load(path)?;
|
||||
/// #
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn load(path: PathBuf) -> Result<Self, Error> {
|
||||
impl SquadObject for Quest {
|
||||
fn load(path: PathBuf) -> Result<Self, Error> {
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(string) => {
|
||||
match toml::from_str::<Quest>(&string) {
|
||||
Ok(quest) => Ok(quest),
|
||||
match toml::from_str::<Self>(&string) {
|
||||
Ok(object) => Ok(object),
|
||||
Err(error) => Err(Error::TomlDeserializeError(error))
|
||||
}
|
||||
},
|
||||
|
|
@ -115,20 +98,7 @@ impl Quest {
|
|||
}
|
||||
}
|
||||
|
||||
/// Check if given file is a quest, then delete it or raise an error.
|
||||
/// If file is not a quest, raises [QuestError::TomlDeserializeError]
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use squad_quest::{error::Error,quest::Quest};
|
||||
///
|
||||
/// let path = "quests/0.toml".into();
|
||||
///
|
||||
/// if let Err(error) = Quest::delete(path) {
|
||||
/// // handle the error
|
||||
/// }
|
||||
/// ```
|
||||
pub fn delete(path: PathBuf) -> Result<(), Error> {
|
||||
fn delete(path: PathBuf) -> Result<(), Error> {
|
||||
match Quest::load(path.clone()) {
|
||||
Ok(_) => {
|
||||
if let Err(error) = fs::remove_file(path) {
|
||||
|
|
@ -141,29 +111,7 @@ impl Quest {
|
|||
}
|
||||
}
|
||||
|
||||
/// Save quest to given folder in TOML format.
|
||||
/// File will be saved as `{id}.toml`.
|
||||
/// If file exists, this method will override it.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # fn main() {
|
||||
/// use squad_quest::{error::Error,quest::Quest};
|
||||
/// use std::path::PathBuf;
|
||||
///
|
||||
/// let quest = Quest::default();
|
||||
///
|
||||
/// let path: PathBuf = "quests".into();
|
||||
/// # let path2 = path.clone();
|
||||
///
|
||||
/// if let Err(error) = quest.save(path) {
|
||||
/// // handle the error
|
||||
/// }
|
||||
/// # let filename = format!("{}.toml", quest.id);
|
||||
/// # let _ = Quest::delete(path2.with_file_name(filename));
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn save(&self, path: PathBuf) -> Result<(), Error> {
|
||||
fn save(&self, path: PathBuf) -> Result<(), Error> {
|
||||
let filename = format!("{}.toml", self.id);
|
||||
let mut full_path = path;
|
||||
full_path.push(filename);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue