2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-11-12 23:43:57 +00:00
pict-rs/src/config.rs

74 lines
1.6 KiB
Rust
Raw Normal View History

2020-06-07 00:54:06 +00:00
use std::{net::SocketAddr, path::PathBuf};
#[derive(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,
help = "The address and port the server binds to, e.g. 127.0.0.1:80"
)]
addr: SocketAddr,
#[structopt(short, long, help = "The path to the data directory, e.g. data/")]
path: PathBuf,
2020-06-07 01:44:26 +00:00
#[structopt(
short,
long,
help = "An image format to convert all uploaded files into, supports 'jpg' and 'png'"
)]
format: Option<Format>,
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()
}
}
#[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
}