diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-07 11:15:48 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-04-07 11:15:48 +0000 |
commit | 2d717e4d0e78bd2132886a3339804f05f816f67a (patch) | |
tree | 35565ca622bfc87e72b69ed62a0f4dc914a697ba | |
parent | Use strum_macros::EnumIter instead of manually listing Enum variants (diff) | |
download | miniserve-2d717e4d0e78bd2132886a3339804f05f816f67a.tar.gz miniserve-2d717e4d0e78bd2132886a3339804f05f816f67a.zip |
Use strum on Enums to reduce boilerplate
Diffstat (limited to '')
-rw-r--r-- | src/archive.rs | 12 | ||||
-rw-r--r-- | src/listing.rs | 33 | ||||
-rw-r--r-- | src/renderer.rs | 6 |
3 files changed, 15 insertions, 36 deletions
diff --git a/src/archive.rs b/src/archive.rs index f588d56..55a80b5 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -5,27 +5,21 @@ use libflate::gzip::Encoder; use serde::Deserialize; use std::io; use std::path::PathBuf; +use strum_macros::{Display, EnumIter, EnumString}; use tar::Builder; -use strum_macros::EnumIter; use crate::errors; /// Available compression methods -#[derive(Debug, Deserialize, Clone, EnumIter)] +#[derive(Deserialize, Clone, EnumIter, EnumString, Display)] pub enum CompressionMethod { /// TAR GZ #[serde(alias = "targz")] + #[strum(serialize = "targz")] TarGz, } impl CompressionMethod { - pub fn to_string(&self) -> String { - match &self { - CompressionMethod::TarGz => "targz", - } - .to_string() - } - pub fn extension(&self) -> String { match &self { CompressionMethod::TarGz => "tar.gz", diff --git a/src/listing.rs b/src/listing.rs index b7070f3..5096c73 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -7,6 +7,7 @@ use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; +use strum_macros::{Display, EnumString}; use crate::archive; use crate::errors; @@ -14,7 +15,7 @@ use crate::renderer; use crate::themes; /// Query parameters -#[derive(Debug, Deserialize)] +#[derive(Deserialize)] struct QueryParameters { sort: Option<SortingMethod>, order: Option<SortingOrder>, @@ -23,52 +24,36 @@ struct QueryParameters { } /// Available sorting methods -#[derive(Debug, Deserialize, Clone)] +#[derive(Deserialize, Clone, EnumString, Display)] #[serde(rename_all = "lowercase")] pub enum SortingMethod { /// Sort by name + #[strum(serialize = "name")] Name, /// Sort by size + #[strum(serialize = "size")] Size, /// Sort by last modification date (natural sort: follows alphanumerical order) + #[strum(serialize = "date")] Date, } -impl SortingMethod { - pub fn to_string(&self) -> String { - match &self { - SortingMethod::Name => "name", - SortingMethod::Size => "size", - SortingMethod::Date => "date", - } - .to_string() - } -} - /// Available sorting orders -#[derive(Debug, Deserialize, Clone)] +#[derive(Deserialize, Clone, EnumString, Display)] pub enum SortingOrder { /// Ascending order #[serde(alias = "asc")] + #[strum(serialize = "asc")] Ascending, /// Descending order #[serde(alias = "desc")] + #[strum(serialize = "desc")] Descending, } -impl SortingOrder { - pub fn to_string(&self) -> String { - match &self { - SortingOrder::Ascending => "asc", - SortingOrder::Descending => "desc", - } - .to_string() - } -} - #[derive(PartialEq)] /// Possible entry types pub enum EntryType { diff --git a/src/renderer.rs b/src/renderer.rs index 1371ab6..909ee75 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -141,7 +141,7 @@ fn color_scheme_link( /// Partial: archive button fn archive_button(compress_method: archive::CompressionMethod) -> Markup { - let link = format!("?download={}", compress_method.to_string()); + let link = format!("?download={}", compress_method); let text = format!("Download .{}", compress_method.extension()); html! { @@ -163,8 +163,8 @@ fn parametrized_link( return format!( "{}?sort={}&order={}&theme={}", link, - method.to_string(), - order.to_string(), + method, + order, color_scheme.to_string() ); } |