From 8ed18a551696ba4f89d7dfbdeaa879b21e079d33 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Mon, 1 Apr 2019 20:36:35 +0200 Subject: Color schemes --- src/listing.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index c4daf88..0746f5c 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -11,6 +11,10 @@ use std::time::SystemTime; use crate::archive; use crate::errors; use crate::renderer; +use crate::themes; + +/// Default color scheme, when none is set through query parameters +const DEFAULT_COLORSCHEME: themes::ColorScheme = themes::ColorScheme::ArchLinux; /// Query parameters #[derive(Debug, Deserialize)] @@ -18,6 +22,7 @@ struct QueryParameters { sort: Option, order: Option, download: Option, + theme: Option, } /// Available sorting methods @@ -77,6 +82,9 @@ pub enum EntryType { /// Entry is a file File, + + /// Entry is a symlink + Symlink, } /// Entry @@ -114,9 +122,20 @@ impl Entry { } } + /// Returns wether the entry is a directory pub fn is_dir(&self) -> bool { self.entry_type == EntryType::Directory } + + /// Returns wether the entry is a file + pub fn is_file(&self) -> bool { + self.entry_type == EntryType::File + } + + /// Returns wether the entry is a symlink + pub fn is_symlink(&self) -> bool { + self.entry_type == EntryType::Symlink + } } pub fn file_handler(req: &HttpRequest) -> Result { @@ -138,15 +157,16 @@ pub fn directory_listing( let is_root = base.parent().is_none() || req.path() == random_route; let page_parent = base.parent().map(|p| p.display().to_string()); - let (sort_method, sort_order, download) = + let (sort_method, sort_order, download, color_scheme) = if let Ok(query) = Query::::extract(req) { ( query.sort.clone(), query.order.clone(), query.download.clone(), + query.theme.clone(), ) } else { - (None, None, None) + (None, None, None, None) }; let mut entries: Vec = Vec::new(); @@ -174,7 +194,15 @@ pub fn directory_listing( Err(_) => None, }; - if metadata.is_dir() { + if metadata.file_type().is_symlink() { + entries.push(Entry::new( + file_name, + EntryType::Symlink, + file_url, + None, + last_modification_date, + )); + } else if metadata.is_dir() { entries.push(Entry::new( file_name, EntryType::Directory, @@ -227,6 +255,8 @@ pub fn directory_listing( } } + let color_scheme = color_scheme.unwrap_or(DEFAULT_COLORSCHEME); + if let Some(compression_method) = &download { log::info!( "Creating an archive ({extension}) of {path}...", @@ -265,6 +295,7 @@ pub fn directory_listing( page_parent, sort_method, sort_order, + color_scheme, ) .into_string(), )) -- cgit v1.2.3 From fae39ac193c9a0b55f85095f2cd4e53149c9c128 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Tue, 2 Apr 2019 07:24:49 +0200 Subject: Renamed ArchLinux to Archlinux --- src/listing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 0746f5c..4a20db2 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -14,7 +14,7 @@ use crate::renderer; use crate::themes; /// Default color scheme, when none is set through query parameters -const DEFAULT_COLORSCHEME: themes::ColorScheme = themes::ColorScheme::ArchLinux; +const DEFAULT_COLORSCHEME: themes::ColorScheme = themes::ColorScheme::Archlinux; /// Query parameters #[derive(Debug, Deserialize)] -- cgit v1.2.3 From 0304ea92390c06ccbbd7d29a88ecedc9718d72c2 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 3 Apr 2019 20:07:33 +0200 Subject: Added CLI argument to set default theme --- src/listing.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 4a20db2..64eb575 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -13,9 +13,6 @@ use crate::errors; use crate::renderer; use crate::themes; -/// Default color scheme, when none is set through query parameters -const DEFAULT_COLORSCHEME: themes::ColorScheme = themes::ColorScheme::Archlinux; - /// Query parameters #[derive(Debug, Deserialize)] struct QueryParameters { @@ -150,6 +147,7 @@ pub fn directory_listing( req: &HttpRequest, skip_symlinks: bool, random_route: Option, + default_color_scheme: themes::ColorScheme, ) -> Result { let title = format!("Index of {}", req.path()); let base = Path::new(req.path()); @@ -255,7 +253,7 @@ pub fn directory_listing( } } - let color_scheme = color_scheme.unwrap_or(DEFAULT_COLORSCHEME); + let color_scheme = color_scheme.unwrap_or(default_color_scheme); if let Some(compression_method) = &download { log::info!( -- cgit v1.2.3 From 4e43a1b76cbf8582c9da15eda866b5d2d0d5975a Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Wed, 3 Apr 2019 22:55:36 +0200 Subject: Use serde lowercase for SortingMethods enum --- src/listing.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 64eb575..de84dc6 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -24,17 +24,15 @@ struct QueryParameters { /// Available sorting methods #[derive(Debug, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] pub enum SortingMethod { /// Sort by name - #[serde(alias = "name")] Name, /// Sort by size - #[serde(alias = "size")] Size, /// Sort by last modification date (natural sort: follows alphanumerical order) - #[serde(alias = "date")] Date, } -- cgit v1.2.3 From 2d717e4d0e78bd2132886a3339804f05f816f67a Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sun, 7 Apr 2019 13:15:48 +0200 Subject: Use strum on Enums to reduce boilerplate --- src/listing.rs | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) (limited to 'src/listing.rs') 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, order: Option, @@ -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 { -- cgit v1.2.3 From 8a98a8dfa4479fcbfad364343b727891b3e41a11 Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sun, 7 Apr 2019 13:39:26 +0200 Subject: Use serialize_all --- src/listing.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index 5096c73..fee81a2 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -26,17 +26,15 @@ struct QueryParameters { /// Available sorting methods #[derive(Deserialize, Clone, EnumString, Display)] #[serde(rename_all = "lowercase")] +#[strum(serialize_all = "snake_case")] 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, } -- cgit v1.2.3 From fb12cde5a0682cddf731929a5536c9d895b518ec Mon Sep 17 00:00:00 2001 From: boasting-squirrel Date: Sun, 7 Apr 2019 14:05:16 +0200 Subject: Fixed serde's rename strategy for CompressionMethods enum --- src/listing.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/listing.rs') diff --git a/src/listing.rs b/src/listing.rs index fee81a2..fdabc48 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -25,7 +25,7 @@ struct QueryParameters { /// Available sorting methods #[derive(Deserialize, Clone, EnumString, Display)] -#[serde(rename_all = "lowercase")] +#[serde(rename_all = "snake_case")] #[strum(serialize_all = "snake_case")] pub enum SortingMethod { /// Sort by name -- cgit v1.2.3