Skip to content

Commit

Permalink
[wicketd] replace snafu with thiserror (#4950)
Browse files Browse the repository at this point in the history
This is currently the only use of snafu in omicron (outside of
transitive
deps), and I figure it is simple enough that we can replace it with
thiserror,
reducing use of a direct dependency.

This was the first time I'd encountered snafu so I read a bit about it.
As far
as I understand, the main benefit of snafu is that it pushes you heavily
towards an error-per-module pattern. However, thiserror does permit that
pattern as well, and in practice it is only a little more verbose than
snafu to
do right (`map_err` vs `context`, though `snafu` introduces new types
that
aren't in the source code like `IoSnafu` and `ParseSnafu`).
  • Loading branch information
sunshowers authored Feb 1, 2024
1 parent e72625c commit 5208d21
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ slog-envlogger = "2.2"
slog-error-chain = { git = "https://github.com/oxidecomputer/slog-error-chain", branch = "main", features = ["derive"] }
slog-term = "2.9"
smf = "0.2"
snafu = "0.7"
sp-sim = { path = "sp-sim" }
sprockets-common = { git = "http://github.com/oxidecomputer/sprockets", rev = "77df31efa5619d0767ffc837ef7468101608aee9" }
sprockets-host = { git = "http://github.com/oxidecomputer/sprockets", rev = "77df31efa5619d0767ffc837ef7468101608aee9" }
Expand Down
1 change: 0 additions & 1 deletion wicketd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ tokio-stream.workspace = true
tokio-util.workspace = true
tough.workspace = true
trust-dns-resolver.workspace = true
snafu.workspace = true
toml.workspace = true
uuid.workspace = true

Expand Down
36 changes: 25 additions & 11 deletions wicketd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

//! Configuration related types used by wicketd
use camino::{Utf8Path, Utf8PathBuf};
use dropshot::ConfigLogging;
use serde::{Deserialize, Serialize};
use snafu::prelude::*;
use std::path::Path;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
Expand All @@ -19,17 +18,32 @@ impl Config {
/// Load a `Config` from the given TOML file
///
/// This config object can be used to create a wicketd server.
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Config, ConfigError> {
pub fn from_file<P: AsRef<Utf8Path>>(
path: P,
) -> Result<Config, ConfigError> {
let path = path.as_ref();
let data = std::fs::read_to_string(path).context(IoSnafu { path })?;
toml::from_str(&data).context(ParseSnafu { path })
let data = std::fs::read_to_string(path).map_err(|error| {
ConfigError::Io { error, path: path.to_owned() }
})?;
toml::from_str(&data).map_err(|error| ConfigError::Parse {
error,
path: path.to_owned(),
})
}
}

#[derive(Debug, Snafu)]
#[derive(Debug, Error)]
pub enum ConfigError {
#[snafu(display("Failed to read config file: {}", path.display()))]
Io { source: std::io::Error, path: PathBuf },
#[snafu(display("Failed to parse config file: {}", path.display()))]
Parse { source: toml::de::Error, path: PathBuf },
#[error("Failed to read config file: {path}")]
Io {
#[source]
error: std::io::Error,
path: Utf8PathBuf,
},
#[error("Failed to parse config file: {path}")]
Parse {
#[source]
error: toml::de::Error,
path: Utf8PathBuf,
},
}

0 comments on commit 5208d21

Please sign in to comment.