Skip to content

Commit bc0454f

Browse files
cli: Have a borrowed variant
As most errors are &'static anyways
1 parent 455a330 commit bc0454f

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

cli/src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,41 @@ use time::{OffsetDateTime, UtcOffset};
1414
const BINARY_NAME: &str = env!("CARGO_BIN_NAME");
1515
const H_STYLE: anstyle::Style = anstyle::Style::new().bold().underline();
1616

17-
struct Error(String);
17+
enum Error {
18+
Owned(String),
19+
Borrowed(&'static str),
20+
}
1821

1922
impl fmt::Debug for Error {
2023
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21-
write!(f, "{}", self.0)
24+
match self {
25+
Self::Owned(s) => f.write_str(s),
26+
Self::Borrowed(s) => f.write_str(s),
27+
}
2228
}
2329
}
2430

2531
impl From<std::io::Error> for Error {
2632
fn from(err: std::io::Error) -> Error {
27-
Error(err.to_string())
33+
Self::Owned(err.to_string())
2834
}
2935
}
3036

3137
impl From<oo7::file::Error> for Error {
3238
fn from(err: oo7::file::Error) -> Error {
33-
Error(err.to_string())
39+
Self::Owned(err.to_string())
3440
}
3541
}
3642

3743
impl From<oo7::dbus::Error> for Error {
3844
fn from(err: oo7::dbus::Error) -> Error {
39-
Error(err.to_string())
45+
Self::Owned(err.to_string())
4046
}
4147
}
4248

4349
impl Error {
44-
fn new(s: &str) -> Self {
45-
Self(String::from(s))
50+
fn new(s: &'static str) -> Self {
51+
Self::Borrowed(s)
4652
}
4753
}
4854

@@ -207,7 +213,7 @@ impl Commands {
207213
service
208214
.with_alias(alias)
209215
.await?
210-
.ok_or_else(|| Error(format!("Collection '{alias}' not found")))?
216+
.ok_or_else(|| Error::Owned(format!("Collection '{alias}' not found")))?
211217
} else {
212218
service.default_collection().await?
213219
};

0 commit comments

Comments
 (0)