google_fonts/error.rs
1
2use std::error::Error;
3use std::fmt::{Display, Formatter, Result};
4
5/// An error that can occur while using the `google-fonts` crate.
6///
7/// This enum represents various errors that can be encountered while
8/// downloading, deserializing, caching, or otherwise handling fonts
9/// from Google Fonts. Each variant corresponds to a specific type
10/// of error that can occur during the process.
11///
12/// # Variants
13///
14/// - `Network`: Indicates an error that occurred while making a network request.
15/// - `Deserialize`: Indicates an error that occurred while deserializing JSON data.
16/// - `CacheDir`: Indicates an error that occurred while interacting with the cache directory.
17/// - `CacheFile`: Indicates an error that occurred while interacting with a cache file.
18#[derive(Debug)]
19pub enum FontError {
20 /// An error that occurred while making a network request.
21 ///
22 /// This variant wraps a `reqwest::Error`, which provides more details
23 /// about the specific network-related error that occurred.
24 ///
25 /// # Example
26 ///
27 /// ```rust
28 /// use google_fonts::FontError;
29 /// if let FontError::Network(e) = error {
30 /// println!("Network error: {}", e);
31 /// }
32 /// ```
33 Network(reqwest::Error),
34
35 /// An error that occurred while deserializing JSON data.
36 ///
37 /// This variant wraps a `serde_json::Error`, which provides more details
38 /// about the specific deserialization error that occurred.
39 ///
40 /// # Example
41 ///
42 /// ```rust
43 /// use google_fonts::FontError;
44 /// if let FontError::Deserialize(e) = error {
45 /// println!("Deserialization error: {}", e);
46 /// }
47 /// ```
48 Deserialize(serde_json::Error),
49
50 /// An error that occurred while interacting with the cache directory.
51 ///
52 /// This variant wraps a `StringError`, which provides more details
53 /// about the specific error related to the cache directory.
54 ///
55 /// # Example
56 ///
57 /// ```rust
58 /// use google_fonts::FontError;
59 /// if let FontError::CacheDir(e) = error {
60 /// println!("Cache directory error: {}", e);
61 /// }
62 /// ```
63 CacheDir(StringError),
64
65 /// An error that occurred while interacting with a cache file.
66 ///
67 /// This variant wraps a `std::io::Error`, which provides more details
68 /// about the specific I/O error that occurred.
69 ///
70 /// # Example
71 ///
72 /// ```rust
73 /// use google_fonts::FontError;
74 /// if let FontError::CacheFile(e) = error {
75 /// println!("Cache file error: {}", e);
76 /// }
77 /// ```
78 CacheFile(std::io::Error),
79}
80
81impl Display for FontError {
82 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
83 match self {
84 FontError::Network(e) => write!(f, "font network error: {}", e),
85 FontError::Deserialize(e) => write!(f, "deserialization error: {}", e),
86 FontError::CacheDir(e) => write!(f, "font cache directory error: {}", e),
87 FontError::CacheFile(e) => write!(f, "font cache file error: {}", e),
88 }
89 }
90}
91
92impl Error for FontError {
93 fn source(&self) -> Option<&(dyn Error + 'static)> {
94 match self {
95 FontError::Network(e) => Some(e),
96 FontError::Deserialize(e) => Some(e),
97 FontError::CacheDir(e) => Some(e),
98 FontError::CacheFile(e) => Some(e),
99 }
100 }
101}
102
103#[derive(Debug)]
104pub struct StringError {
105 msg: String,
106}
107impl StringError {
108 pub fn new(msg: &str) -> Self {
109 Self {
110 msg: msg.to_string(),
111 }
112 }
113}
114impl Display for StringError {
115 fn fmt(&self, f: &mut Formatter) -> Result {
116 write!(f, "{}", self.msg)
117 }
118}
119impl Error for StringError {}
120