-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use color-eyre instead of error-chain for errors
I prefer this crate.
- Loading branch information
Showing
4 changed files
with
116 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
// SPDX-FileCopyrightText: 2024 Luflosi <[email protected]> | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
mod errors { | ||
error_chain! {} | ||
} | ||
use errors::{Error, Result, ResultExt}; | ||
|
||
use argon2::password_hash::PasswordHash; | ||
use color_eyre::eyre::{eyre, Result, WrapErr}; | ||
use serde_derive::Deserialize; | ||
use std::collections::HashMap; | ||
use std::fs; | ||
|
@@ -71,9 +67,10 @@ pub struct User<'a> { | |
impl Config<'_> { | ||
pub fn read(filename: &Path) -> Result<Config<'static>> { | ||
let contents = fs::read_to_string(filename) | ||
.chain_err(|| format!("Cannot read config file `{}`", filename.display()))?; | ||
.wrap_err_with(|| format!("Cannot read config file `{}`", filename.display()))?; | ||
let config_parse_err_msg = || format!("Cannot parse config file `{}`", filename.display()); | ||
let raw_config: RawConfig = toml::from_str(&contents).chain_err(config_parse_err_msg)?; | ||
let raw_config: RawConfig = | ||
toml::from_str(&contents).wrap_err_with(config_parse_err_msg)?; | ||
let users: Result<HashMap<_, _>> = raw_config | ||
.users | ||
.into_iter() | ||
|
@@ -86,20 +83,18 @@ impl Config<'_> { | |
}; | ||
if props.ipv6prefixlen > 128 { | ||
let prefixlen = props.ipv6prefixlen; | ||
return Err(Error::from(format!( | ||
"Prefix is longer than 128 bits: {prefixlen}" | ||
)) | ||
.chain_err(ipv6prefixlen_parse_err_msg) | ||
.chain_err(config_parse_err_msg)); | ||
return Err(eyre!("Prefix is longer than 128 bits: {prefixlen}")) | ||
.wrap_err_with(ipv6prefixlen_parse_err_msg) | ||
.wrap_err_with(config_parse_err_msg); | ||
}; | ||
} | ||
// TODO: figure out how to do this without leaking memory. I wish PasswordHash::new() took a String instead of &str | ||
let raw_hash = Box::leak(Box::new(raw_user.hash)); | ||
let user = User { | ||
// TODO: get rid of this piece of the code by somehow implementing deserialization for PasswordHash | ||
hash: PasswordHash::new(raw_hash) | ||
.chain_err(|| format!("Cannot parse password hash of user {username}")) | ||
.chain_err(config_parse_err_msg)?, | ||
.wrap_err_with(|| format!("Cannot parse password hash of user {username}")) | ||
.wrap_err_with(config_parse_err_msg)?, | ||
domains: raw_user.domains, | ||
}; | ||
Ok((username, user)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters