|
12 | 12 | //! |
13 | 13 | //! impl Storeable for User {} |
14 | 14 | //! |
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}"), |
30 | 23 | //! } |
31 | | -//! ``` |
| 24 | +
|
| 25 | +//! match User::load_by_extension(save_path) { |
| 26 | +//! Ok(s) => println!("{s:?}"), |
| 27 | +//! Err(e) => println!("Error: {e}"), |
| 28 | +//! } |
| 29 | +//! //! ``` |
32 | 30 |
|
33 | 31 | 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; |
35 | 34 |
|
36 | | -#[derive(Debug)] |
| 35 | +#[derive(Debug, Error)] |
37 | 36 | 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.")] |
42 | 46 | ExtensionDoesNotExist, |
43 | 47 | } |
44 | 48 |
|
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 | | - |
93 | 49 | pub enum Format { |
94 | 50 | Json, |
95 | 51 | Toml, |
96 | 52 | } |
97 | 53 |
|
98 | 54 | 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()) { |
100 | 56 | match v { |
101 | 57 | "json" => Ok(Format::Json), |
102 | 58 | "toml" => Ok(Format::Toml), |
|
0 commit comments