Skip to content

Commit

Permalink
Simplify parsing max expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
matze committed Jul 29, 2024
1 parent a044a18 commit f12224d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ run-time behavior:
until wastebin responds with 408, by default it is set to 5 seconds.
* `WASTEBIN_MAX_BODY_SIZE` number of bytes to accept for POST requests. Defaults
to 1 MB.
* `WASTEBIN_MAX_PASTE_EXPIRATION` maximum allowed lifetime of a paste in seconds. Leave empty or set to -1 for no limit. Defaults to no limit.
* `WASTEBIN_MAX_PASTE_EXPIRATION` maximum allowed lifetime of a paste in
seconds. Defaults to unlimited.
* `WASTEBIN_PASSWORD_SALT` salt used to hash user passwords used for encrypting
pastes.
* `WASTEBIN_SIGNING_KEY` sets the key to sign cookies. If not set, a random key
Expand Down
16 changes: 1 addition & 15 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ pub enum Error {
HttpTimeout(ParseIntError),
#[error("failed to parse {VAR_MAX_PASTE_EXPIRATION}: {0}")]
MaxPasteExpiration(ParseIntError),
#[error(
"{VAR_MAX_PASTE_EXPIRATION} is too large (max {}), pass -1 if you mean no expiry",
u32::MAX
)]
ExpirationTooLarge,
}

pub struct BasePath(String);
Expand Down Expand Up @@ -186,15 +181,6 @@ pub fn http_timeout() -> Result<Duration, Error> {
pub fn max_paste_expiration() -> Result<Option<u32>, Error> {
std::env::var(VAR_MAX_PASTE_EXPIRATION)
.ok()
.and_then(|raw_max_exp| -> Option<Result<u32, Error>> {
match raw_max_exp.parse::<i64>() {
Ok(-1) => None,
Ok(max_exp) if max_exp >= i64::from(u32::MAX) => {
Some(Err(Error::ExpirationTooLarge))
}
Ok(max_exp) => Some(Ok(u32::try_from(max_exp).expect("fitting value"))),
Err(why) => Some(Err(Error::MaxPasteExpiration(why))),
}
})
.map(|value| value.parse::<u32>().map_err(Error::MaxPasteExpiration))
.transpose()
}

0 comments on commit f12224d

Please sign in to comment.