Skip to content

Commit 8b00547

Browse files
committed
refactor: Use thiserror for error handling
1 parent 5f242f4 commit 8b00547

3 files changed

Lines changed: 51 additions & 74 deletions

File tree

Cargo.lock

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "easy_storage"
3-
version = "0.3.2"
3+
version = "0.4.1"
44
edition = "2024"
55
authors = [ "Uliboooo <uliboulibo@gmail.com>"]
66
description = "Provides a trait to easily save and load structs in JSON or TOML format."
@@ -11,4 +11,4 @@ repository = "https://github.com/Uliboooo/easy_storage"
1111
serde = { version = "1.0.*", features = ["derive"] }
1212
toml = { version = "0.9.*"}
1313
serde_json = { version = "1.0.*"}
14-
14+
thiserror = "2.0.16"

src/lib.rs

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,91 +12,47 @@
1212
//!
1313
//! impl Storeable for User {}
1414
//!
15-
//! fn main() {
16-
//! let user = User {
17-
//! name: "Alice".to_string(),
18-
//! email: "alice@alice.com".to_string(),
19-
//! };
20-
//! let save_path = std::env::current_dir().unwrap().join("test").join("user.toml");
21-
//! match user.save_by_extension(&save_path, true) {
22-
//! Ok(_) => println!("success."),
23-
//! Err(e) => println!("Error: {e}"),
24-
//! }
25-
//!
26-
//! match User::load_by_extension(save_path) {
27-
//! Ok(s) => println!("{s:?}"),
28-
//! Err(e) => println!("Error: {e}"),
29-
//! }
15+
//! let user = User {
16+
//! name: "Alice".to_string(),
17+
//! email: "alice@alice.com".to_string(),
18+
//! };
19+
//! let save_path = std::env::current_dir().unwrap().join("test").join("user.toml");
20+
//! match user.save_by_extension(&save_path, true) {
21+
//! Ok(_) => println!("success."),
22+
//! Err(e) => println!("Error: {e}"),
3023
//! }
31-
//! ```
24+
25+
//! match User::load_by_extension(save_path) {
26+
//! Ok(s) => println!("{s:?}"),
27+
//! Err(e) => println!("Error: {e}"),
28+
//! }
29+
//! //! ```
3230
3331
use serde::{Serialize, de::DeserializeOwned};
34-
use std::{fmt::Display, fs::OpenOptions, io::Write, path::Path};
32+
use std::{fs::OpenOptions, io::Write, path::Path};
33+
use thiserror::Error;
3534

36-
#[derive(Debug)]
35+
#[derive(Debug, Error)]
3736
pub enum Error {
38-
IoE(std::io::Error),
39-
JsonE(serde_json::Error),
40-
ParTomlE(toml::ser::Error),
41-
DesTomlE(toml::de::Error),
37+
#[error("IO error: {0}")]
38+
IoE(#[from] std::io::Error),
39+
#[error("serde error: {0}")]
40+
JsonE(#[from] serde_json::Error),
41+
#[error("parse toml error: {0}")]
42+
ParTomlE(#[from] toml::ser::Error),
43+
#[error("parse toml error: {0}")]
44+
DesTomlE(#[from] toml::de::Error),
45+
#[error("extension does not exist.")]
4246
ExtensionDoesNotExist,
4347
}
4448

45-
impl From<std::io::Error> for Error {
46-
fn from(value: std::io::Error) -> Self {
47-
Self::IoE(value)
48-
}
49-
}
50-
51-
impl From<serde_json::Error> for Error {
52-
fn from(value: serde_json::Error) -> Self {
53-
Self::JsonE(value)
54-
}
55-
}
56-
57-
impl From<toml::ser::Error> for Error {
58-
fn from(value: toml::ser::Error) -> Self {
59-
Self::ParTomlE(value)
60-
}
61-
}
62-
63-
impl From<toml::de::Error> for Error {
64-
fn from(value: toml::de::Error) -> Self {
65-
Self::DesTomlE(value)
66-
}
67-
}
68-
69-
impl Display for Error {
70-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71-
match self {
72-
Error::IoE(e) => write!(f, "{e}"),
73-
Error::JsonE(e) => write!(f, "{e}"),
74-
Error::ParTomlE(e) => write!(f, "{e}"),
75-
Error::DesTomlE(e) => write!(f, "{e}"),
76-
Error::ExtensionDoesNotExist => write!(f, "extension does not exist."),
77-
}
78-
}
79-
}
80-
81-
impl std::error::Error for Error {
82-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
83-
match self {
84-
Error::IoE(e) => Some(e),
85-
Error::JsonE(e) => Some(e),
86-
Error::ParTomlE(e) => Some(e),
87-
Error::DesTomlE(e) => Some(e),
88-
Error::ExtensionDoesNotExist => None,
89-
}
90-
}
91-
}
92-
9349
pub enum Format {
9450
Json,
9551
Toml,
9652
}
9753

9854
fn path_to_format<P: AsRef<Path>>(path: P) -> Result<Format, Error> {
99-
if let Some(v) = path.as_ref().extension().map(|f| f.to_str()).flatten() {
55+
if let Some(v) = path.as_ref().extension().and_then(|f| f.to_str()) {
10056
match v {
10157
"json" => Ok(Format::Json),
10258
"toml" => Ok(Format::Toml),

0 commit comments

Comments
 (0)