diff options
author | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-04 23:02:36 +0000 |
---|---|---|
committer | boasting-squirrel <boasting.squirrel@gmail.com> | 2019-03-04 23:02:36 +0000 |
commit | 08ec4f6a35b83c37a6a3e557aade09c7a0bb4e73 (patch) | |
tree | e4fc0febefffda315388f399872bd71d5529d779 | |
parent | Sort in alphanumeric order by default (diff) | |
download | miniserve-08ec4f6a35b83c37a6a3e557aade09c7a0bb4e73.tar.gz miniserve-08ec4f6a35b83c37a6a3e557aade09c7a0bb4e73.zip |
Use actix::Query
-rw-r--r-- | Cargo.lock | 25 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/listing.rs | 48 |
3 files changed, 46 insertions, 30 deletions
@@ -87,7 +87,7 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)", "serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -764,6 +764,7 @@ dependencies = [ "maud 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)", "nanoid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "simplelog 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "yansi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1203,8 +1204,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.87" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "serde_json" @@ -1213,7 +1227,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1223,7 +1237,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1970,7 +1984,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.87 (registry+https://github.com/rust-lang/crates.io-index)" = "2e20fde37801e83c891a2dc4ebd3b81f0da4d1fb67a9e0a2a3b921e2536a58ee" +"checksum serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560" +"checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c" "checksum serde_json 1.0.38 (registry+https://github.com/rust-lang/crates.io-index)" = "27dce848e7467aa0e2fcaf0a413641499c0b745452aaca1194d24dedde9e13c9" "checksum serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d48f9f99cd749a2de71d29da5f948de7f2764cc5a9d7f3c97e3514d4ee6eabf2" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" @@ -32,4 +32,5 @@ alphanumeric-sort = "1.0.6" structopt = "0.2.14" chrono = "0.4.6" chrono-humanize = "0.0.11" -maud = { version = "0.20.0", features = ["actix-web"] }
\ No newline at end of file +maud = { version = "0.20.0", features = ["actix-web"] } +serde = { version = "1.0.89", features = ["derive"] }
\ No newline at end of file diff --git a/src/listing.rs b/src/listing.rs index 45d7af3..c820a80 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -1,36 +1,38 @@ -use actix_web::{fs, HttpRequest, HttpResponse, Result}; +use actix_web::{fs, FromRequest, HttpRequest, HttpResponse, Query, Result}; use bytesize::ByteSize; use htmlescape::encode_minimal as escape_html_entity; use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET}; +use serde::Deserialize; use std::io; use std::path::Path; use std::time::SystemTime; use crate::renderer; +/// Query parameters +#[derive(Debug, Deserialize)] +struct SortingQueryParameters { + sort: Option<SortingMethod>, + order: Option<SortingOrder>, +} + /// Available sorting methods -#[derive(Debug)] +#[derive(Debug, Deserialize, Clone)] 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, } impl SortingMethod { - fn from_str(src: &str) -> Self { - match src { - "name" => SortingMethod::Name, - "size" => SortingMethod::Size, - "date" => SortingMethod::Date, - _ => SortingMethod::Name, - } - } - pub fn to_string(&self) -> String { match &self { SortingMethod::Name => "name", @@ -42,23 +44,18 @@ impl SortingMethod { } /// Available sorting orders -#[derive(Debug)] +#[derive(Debug, Deserialize, Clone)] pub enum SortingOrder { /// Ascending order + #[serde(alias = "asc")] Ascending, /// Descending order + #[serde(alias = "desc")] Descending, } impl SortingOrder { - fn from_str(src: &str) -> Self { - match src { - "desc" => SortingOrder::Descending, - _ => SortingOrder::Ascending, - } - } - pub fn to_string(&self) -> String { match &self { SortingOrder::Ascending => "asc", @@ -137,9 +134,13 @@ pub fn directory_listing<S>( let is_root = base.parent().is_none() || req.path() == random_route; let page_parent = base.parent().map(|p| p.display().to_string()); - let query = req.query(); - let sort_method = query.get("sort").map(|e| SortingMethod::from_str(e)); - let sort_order = query.get("order").map(|e| SortingOrder::from_str(e)); + let mut sort_method: Option<SortingMethod> = None; + let mut sort_order: Option<SortingOrder> = None; + + if let Ok(query) = Query::<SortingQueryParameters>::extract(req) { + sort_method = query.sort.clone(); + sort_order = query.order.clone(); + } let mut entries: Vec<Entry> = Vec::new(); @@ -210,8 +211,7 @@ pub fn directory_listing<S>( }; } else { // Sort in alphanumeric order by default - entries - .sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())) + entries.sort_by(|e1, e2| alphanumeric_sort::compare_str(e1.name.clone(), e2.name.clone())) } if let Some(sorting_order) = &sort_order { |