pict-rs/src/config.rs

94 lines
2.2 KiB
Rust
Raw Normal View History

2020-06-07 19:12:19 +00:00
use std::{collections::HashSet, net::SocketAddr, path::PathBuf};
2020-06-07 00:54:06 +00:00
2020-06-07 19:12:19 +00:00
#[derive(Clone, Debug, structopt::StructOpt)]
2020-06-07 01:44:26 +00:00
pub(crate) struct Config {
2020-06-07 00:54:06 +00:00
#[structopt(
short,
long,
2020-06-07 20:21:25 +00:00
env = "PICTRS_ADDR",
default_value = "0.0.0.0:8080",
help = "The address and port the server binds to. Default: 0.0.0.0:8080"
2020-06-07 00:54:06 +00:00
)]
addr: SocketAddr,
2020-06-07 20:21:25 +00:00
#[structopt(
short,
long,
env = "PICTRS_PATH",
help = "The path to the data directory, e.g. data/")]
2020-06-07 00:54:06 +00:00
path: PathBuf,
2020-06-07 01:44:26 +00:00
#[structopt(
short,
long,
2020-06-07 20:21:25 +00:00
env = "PICTRS_FORMAT",
2020-06-07 01:44:26 +00:00
help = "An image format to convert all uploaded files into, supports 'jpg' and 'png'"
)]
format: Option<Format>,
2020-06-07 19:12:19 +00:00
#[structopt(
short,
long,
help = "An optional list of filters to whitelist, supports 'identity', 'thumbnail', and 'blur'"
)]
whitelist: Option<Vec<String>>,
2020-06-07 00:54:06 +00:00
}
impl Config {
pub(crate) fn bind_address(&self) -> SocketAddr {
self.addr
}
pub(crate) fn data_dir(&self) -> PathBuf {
self.path.clone()
}
2020-06-07 01:44:26 +00:00
pub(crate) fn format(&self) -> Option<Format> {
self.format.clone()
}
2020-06-07 19:12:19 +00:00
pub(crate) fn filter_whitelist(&self) -> Option<HashSet<String>> {
self.whitelist
.as_ref()
.map(|wl| wl.iter().cloned().collect())
}
2020-06-07 01:44:26 +00:00
}
#[derive(Debug, thiserror::Error)]
#[error("Invalid format supplied, {0}")]
pub(crate) struct FormatError(String);
#[derive(Clone, Debug)]
pub(crate) enum Format {
Jpeg,
Png,
}
impl Format {
pub(crate) fn to_image_format(&self) -> image::ImageFormat {
match self {
Format::Jpeg => image::ImageFormat::Jpeg,
Format::Png => image::ImageFormat::Png,
}
}
pub(crate) fn to_mime(&self) -> mime::Mime {
match self {
Format::Jpeg => mime::IMAGE_JPEG,
Format::Png => mime::IMAGE_PNG,
}
}
}
impl std::str::FromStr for Format {
type Err = FormatError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"png" => Ok(Format::Png),
"jpg" => Ok(Format::Jpeg),
other => Err(FormatError(other.to_string())),
}
}
2020-06-07 00:54:06 +00:00
}