Skip to content

Commit 3aaef62

Browse files
committed
Get rid of thiserror dependency
1 parent 75c1604 commit 3aaef62

File tree

5 files changed

+106
-41
lines changed

5 files changed

+106
-41
lines changed

spdlog-internal/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ workspace = true
1414

1515
[dependencies]
1616
nom = "8.0.0"
17-
thiserror = "2.0.0"

spdlog-internal/src/pattern_parser/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::fmt::{self, Display};
22

33
use nom::error::Error as NomError;
4-
use thiserror::Error;
54

65
use super::PatternKind;
76
use crate::impossible;
87

9-
#[derive(Error, Debug, PartialEq)]
8+
#[derive(Debug, PartialEq)]
109
pub enum Error {
1110
ConflictName {
1211
existing: PatternKind<()>,
@@ -82,7 +81,7 @@ impl Display for Error {
8281
}
8382
}
8483

85-
#[derive(Error, Debug, Eq, PartialEq)]
84+
#[derive(Debug, Eq, PartialEq)]
8685
pub enum TemplateError {
8786
WrongPatternKindReference {
8887
is_builtin_as_custom: bool,

spdlog/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ serde = { version = "1.0.163", optional = true, features = ["derive"] }
6565
serde_json = { version = "1.0.120", optional = true }
6666
spdlog-internal = { version = "=0.2.0", path = "../spdlog-internal", optional = true }
6767
spdlog-macros = { version = "=0.3.0", path = "../spdlog-macros" }
68-
thiserror = "2.0.0"
6968
value-bag = { version = "1.11.1", features = ["owned", "inline-i128"] }
7069

7170
[target.'cfg(windows)'.dependencies]

spdlog/src/env_level.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{
22
collections::{hash_map::Entry, HashMap},
33
env::VarError,
4+
error::Error as StdError,
5+
fmt,
46
};
57

6-
use thiserror::Error;
7-
88
use crate::{sync::*, LevelFilter};
99

1010
pub(crate) type EnvLevel = HashMap<EnvLevelLogger, LevelFilter>;
@@ -20,20 +20,31 @@ pub(crate) enum EnvLevelLogger {
2020
}
2121

2222
/// The error type of environment level initialization.
23-
#[derive(Error, Debug)]
23+
#[derive(Debug)]
2424
pub enum EnvLevelError {
2525
/// Fetch environment variable error.
26-
#[error("fetch environment variable error: {0}")]
2726
FetchEnvVar(VarError),
2827

2928
/// Parse environment variable error, usually caused by incorrect format.
30-
#[error("parse environment variable error: {0}")]
3129
ParseEnvVar(
3230
/// Parse error description
3331
String,
3432
),
3533
}
3634

35+
impl StdError for EnvLevelError {}
36+
37+
impl fmt::Display for EnvLevelError {
38+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
match self {
40+
EnvLevelError::FetchEnvVar(err) => write!(f, "fetch environment variable error: {err}"),
41+
EnvLevelError::ParseEnvVar(description) => {
42+
write!(f, "parse environment variable error: {description}")
43+
}
44+
}
45+
}
46+
}
47+
3748
impl EnvLevelLogger {
3849
#[must_use]
3950
fn from_key(logger_name: &str) -> Self {

spdlog/src/error.rs

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,97 +13,82 @@ use std::{
1313
sync::Arc,
1414
};
1515

16-
use thiserror::Error;
17-
1816
pub use crate::env_level::EnvLevelError;
1917
#[cfg(feature = "multi-thread")]
2018
use crate::{sink::Task, RecordOwned};
2119

2220
/// Contains most errors of this crate.
23-
#[derive(Error, Debug)]
21+
#[derive(Debug)]
2422
#[non_exhaustive]
2523
pub enum Error {
2624
/// Returned by [`Formatter`]s when an error occurs in formatting a record.
2725
///
2826
/// [`Formatter`]: crate::formatter::Formatter
29-
#[error("format record error: {0}")]
3027
FormatRecord(fmt::Error),
3128

3229
/// Returned by [`Sink`]s when an error occurs in writing a record to the
3330
/// target.
3431
///
3532
/// [`Sink`]: crate::sink::Sink
36-
#[error("write record error: {0}")]
3733
WriteRecord(io::Error),
3834

3935
/// Returned by [`Sink`]s when an error occurs in flushing the buffer.
4036
///
4137
/// [`Sink`]: crate::sink::Sink
42-
#[error("flush buffer error: {0}")]
4338
FlushBuffer(io::Error),
4439

4540
/// Returned by [`Sink`]s when an error occurs in creating a directory.
4641
///
4742
/// [`Sink`]: crate::sink::Sink
48-
#[error("create directory error: {0}")]
4943
CreateDirectory(io::Error),
5044

5145
/// Returned by [`Sink`]s when an error occurs in opening a file.
5246
///
5347
/// [`Sink`]: crate::sink::Sink
54-
#[error("open file error: {0}")]
5548
OpenFile(io::Error),
5649

5750
/// Returned by [`Sink`]s when an error occurs in querying the metadata of a
5851
/// file.
5952
///
6053
/// [`Sink`]: crate::sink::Sink
61-
#[error("query file metadata error: {0}")]
6254
QueryFileMetadata(io::Error),
6355

6456
/// Returned by [`Sink`]s when an error occurs in renaming a file.
6557
///
6658
/// [`Sink`]: crate::sink::Sink
67-
#[error("rename file error: {0}")]
6859
RenameFile(io::Error),
6960

7061
/// Returned by [`Sink`]s when an error occurs in removing a file.
7162
///
7263
/// [`Sink`]: crate::sink::Sink
73-
#[error("remove file error: {0}")]
7464
RemoveFile(io::Error),
7565

7666
/// Returned by [`from_str`] when the string doesn't match any of the log
7767
/// levels.
7868
///
7969
/// [`from_str`]: std::str::FromStr::from_str
80-
#[error("attempted to convert a string that doesn't match an existing log level: {0}")]
8170
ParseLevel(String),
8271

8372
/// Returned if an invalid argument was passed in.
84-
#[error("invalid argument {0}")]
85-
InvalidArgument(#[from] InvalidArgumentError),
73+
InvalidArgument(InvalidArgumentError),
8674

8775
/// Returned by [`Sink`]s when an error occurs in sending to the channel.
8876
///
8977
/// [`Sink`]: crate::sink::Sink
9078
#[cfg(feature = "multi-thread")]
91-
#[error("failed to send message to channel: {0}")]
9279
SendToChannel(SendToChannelError, SendToChannelErrorDropped),
9380

9481
/// Returned by [`runtime_pattern!`] when the pattern is failed to be built
9582
/// at runtime.
9683
///
9784
/// [`runtime_pattern!`]: crate::formatter::runtime_pattern
9885
#[cfg(feature = "runtime-pattern")]
99-
#[error("failed to build pattern at runtime: {0}")]
10086
BuildPattern(BuildPatternError),
10187

10288
/// Returned by [`Formatter`]s when an error occurs in serializing a log.
10389
///
10490
/// [`Formatter`]: crate::formatter::Formatter
10591
#[cfg(feature = "serde")]
106-
#[error("failed to serialize log: {0}")]
10792
SerializeRecord(io::Error),
10893

10994
/// Returned from a downstream implementation of `spdlog-rs`. Its actual
@@ -112,21 +97,55 @@ pub enum Error {
11297
/// When downstream crates encounter errors, other more specific error
11398
/// variants should be used first, this variant should only be used as a
11499
/// last option when other variant types are incompatible.
115-
#[error("{0}")]
116100
Downstream(Box<dyn StdError + Send + Sync>),
117101

118102
/// Returned when multiple errors occurred.
119-
#[error("{0:?}")]
120103
Multiple(Vec<Error>),
121104

122105
#[cfg(test)]
123106
#[doc(hidden)]
124-
#[error("{0}")]
125107
__ForInternalTestsUseOnly(i32),
126108
}
127109

110+
impl StdError for Error {}
111+
112+
impl Display for Error {
113+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114+
match self {
115+
Self::FormatRecord(err) => write!(f, "format record error: {err}"),
116+
Self::WriteRecord(err) => write!(f, "write record error: {err}"),
117+
Self::FlushBuffer(err) => write!(f, "flush buffer error: {err}"),
118+
Self::CreateDirectory(err) => write!(f, "create directory error: {err}"),
119+
Self::OpenFile(err) => write!(f, "open file error: {err}"),
120+
Self::QueryFileMetadata(err) => write!(f, "query file metadata error: {err}"),
121+
Self::RenameFile(err) => write!(f, "rename file error: {err}"),
122+
Self::RemoveFile(err) => write!(f, "remove file error: {err}"),
123+
Self::ParseLevel(level_str) => {
124+
write!(f, "attempted to convert a string that doesn't match an existing log level: {level_str}")
125+
}
126+
Self::InvalidArgument(err) => write!(f, "invalid argument {err}"),
127+
#[cfg(feature = "multi-thread")]
128+
Self::SendToChannel(err, _) => write!(f, "failed to send message to channel: {err}"),
129+
#[cfg(feature = "runtime-pattern")]
130+
Self::BuildPattern(err) => write!(f, "failed to build pattern at runtime: {err}"),
131+
#[cfg(feature = "serde")]
132+
Self::SerializeRecord(err) => write!(f, "failed to serialize log: {err}"),
133+
Self::Downstream(err) => write!(f, "{err}"),
134+
Self::Multiple(errs) => write!(f, "{errs:?}"),
135+
#[cfg(test)]
136+
Self::__ForInternalTestsUseOnly(i) => write!(f, "{i}"),
137+
}
138+
}
139+
}
140+
141+
impl From<InvalidArgumentError> for Error {
142+
fn from(err: InvalidArgumentError) -> Self {
143+
Self::InvalidArgument(err)
144+
}
145+
}
146+
128147
/// Indicates that an invalid parameter was specified.
129-
#[derive(Error, Debug)]
148+
#[derive(Debug)]
130149
#[non_exhaustive]
131150
pub enum InvalidArgumentError {
132151
/// Invalid logger name.
@@ -135,32 +154,48 @@ pub enum InvalidArgumentError {
135154
/// requirements.
136155
///
137156
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
138-
#[error("'logger name': {0}")]
139-
LoggerName(#[from] SetLoggerNameError),
157+
LoggerName(SetLoggerNameError),
140158

141159
/// Invalid [`RotationPolicy`].
142160
///
143161
/// See the documentation of [`RotationPolicy`] for the input requirements.
144162
///
145163
/// [`RotationPolicy`]: crate::sink::RotationPolicy
146-
#[error("'rotation policy': {0}")]
147164
RotationPolicy(String),
148165

149166
/// Invalid thread pool capacity.
150167
#[deprecated(
151168
since = "0.5.0",
152169
note = "non-zero thread pool capacity is now guarded by NonZeroUsize type"
153170
)]
154-
#[error("'thread pool capacity': {0}")]
155171
ThreadPoolCapacity(String),
156172
}
157173

174+
impl StdError for InvalidArgumentError {}
175+
176+
impl Display for InvalidArgumentError {
177+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178+
match self {
179+
Self::LoggerName(err) => write!(f, "'logger name': {err}"),
180+
Self::RotationPolicy(value) => write!(f, "'rotation policy': {value}"),
181+
#[allow(deprecated)]
182+
Self::ThreadPoolCapacity(value) => write!(f, "'thread pool capacity': {value}"),
183+
}
184+
}
185+
}
186+
187+
impl From<SetLoggerNameError> for InvalidArgumentError {
188+
fn from(err: SetLoggerNameError) -> Self {
189+
Self::LoggerName(err)
190+
}
191+
}
192+
158193
/// Indicates that an invalid logger name was set.
159194
///
160195
/// See the documentation of [`LoggerBuilder::name`] for the name requirements.
161196
///
162197
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
163-
#[derive(Error, Debug)]
198+
#[derive(Debug)]
164199
pub struct SetLoggerNameError {
165200
name: String,
166201
}
@@ -178,6 +213,8 @@ impl SetLoggerNameError {
178213
}
179214
}
180215

216+
impl StdError for SetLoggerNameError {}
217+
181218
impl Display for SetLoggerNameError {
182219
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
183220
write!(f, "name '{}' contains disallowed characters", self.name)
@@ -186,22 +223,33 @@ impl Display for SetLoggerNameError {
186223

187224
/// Indicates that an error occurred while sending to channel.
188225
#[cfg(feature = "multi-thread")]
189-
#[derive(Error, Debug)]
226+
#[derive(Debug)]
190227
#[non_exhaustive]
191228
pub enum SendToChannelError {
192229
/// The channel is full.
193230
///
194231
/// The variant returned only when [`OverflowPolicy::DropIncoming`] is used.
195232
///
196233
/// [`OverflowPolicy::DropIncoming`]: crate::sink::async_sink::OverflowPolicy::DropIncoming
197-
#[error("the channel is full")]
198234
Full,
199235

200236
/// The channel is disconnected.
201-
#[error("the channel is disconnected")]
202237
Disconnected,
203238
}
204239

240+
#[cfg(feature = "multi-thread")]
241+
impl StdError for SendToChannelError {}
242+
243+
#[cfg(feature = "multi-thread")]
244+
impl Display for SendToChannelError {
245+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246+
match self {
247+
Self::Full => write!(f, "the channel is full"),
248+
Self::Disconnected => write!(f, "the channel is disconnected"),
249+
}
250+
}
251+
}
252+
205253
/// Contains data that is dropped after sending to the channel failed.
206254
///
207255
/// You can handle them manually or just ignore them.
@@ -271,10 +319,19 @@ impl SendToChannelErrorDropped {
271319

272320
/// Indicates that an error occurred while building a pattern at compile-time.
273321
#[cfg(feature = "runtime-pattern")]
274-
#[derive(Error, Debug)]
275-
#[error("{0}")]
322+
#[derive(Debug)]
276323
pub struct BuildPatternError(pub(crate) spdlog_internal::pattern_parser::Error);
277324

325+
#[cfg(feature = "runtime-pattern")]
326+
impl StdError for BuildPatternError {}
327+
328+
#[cfg(feature = "runtime-pattern")]
329+
impl Display for BuildPatternError {
330+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331+
write!(f, "{}", self.0)
332+
}
333+
}
334+
278335
/// The result type of this crate.
279336
pub type Result<T> = result::Result<T, Error>;
280337

0 commit comments

Comments
 (0)