aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboasting-squirrel <boasting.squirrel@gmail.com>2019-02-13 18:13:34 +0000
committerboasting-squirrel <boasting.squirrel@gmail.com>2019-02-13 18:13:34 +0000
commit3e2c3341ee4fd540126f822ef23aa442508cf4b6 (patch)
treec29d8f7f3d120b6a01b15c848af48fba356e307d
parentRemoved config.rs and put back actix config in main.rs (diff)
downloadminiserve-3e2c3341ee4fd540126f822ef23aa442508cf4b6.tar.gz
miniserve-3e2c3341ee4fd540126f822ef23aa442508cf4b6.zip
Added some docstrings
-rw-r--r--src/args.rs7
-rw-r--r--src/auth.rs2
-rw-r--r--src/listing.rs24
-rw-r--r--src/main.rs23
4 files changed, 54 insertions, 2 deletions
diff --git a/src/args.rs b/src/args.rs
index 64ff3e6..ae86108 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -4,10 +4,12 @@ use clap::{crate_authors, crate_description, crate_name, crate_version};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::path::PathBuf;
+/// Possible characters for random routes
const ROUTE_ALPHABET: [char; 16] = [
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f',
];
+/// Checks wether a path is valid, i.e. it exists on the system and points to a file/directory
fn is_valid_path(path: String) -> Result<(), String> {
let path_to_check = PathBuf::from(path);
if path_to_check.is_file() || path_to_check.is_dir() {
@@ -18,12 +20,15 @@ fn is_valid_path(path: String) -> Result<(), String> {
))
}
+/// Checks wether a port is valid
fn is_valid_port(port: String) -> Result<(), String> {
port.parse::<u16>()
.and(Ok(()))
.or_else(|e| Err(e.to_string()))
}
+
+/// Checks wether an interface is valid, i.e. it can be parsed into an IP address
fn is_valid_interface(interface: String) -> Result<(), String> {
interface
.parse::<IpAddr>()
@@ -31,12 +36,14 @@ fn is_valid_interface(interface: String) -> Result<(), String> {
.or_else(|e| Err(e.to_string()))
}
+/// Checks wether the auth string is valid, i.e. it follows the syntax username:password
fn is_valid_auth(auth: String) -> Result<(), String> {
auth.find(':')
.ok_or_else(|| "Correct format is username:password".to_owned())
.map(|_| ())
}
+/// Parses the command line arguments
pub fn parse_args() -> crate::MiniserveConfig {
use clap::{App, AppSettings, Arg};
diff --git a/src/auth.rs b/src/auth.rs
index e39305c..7b72b21 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -4,12 +4,14 @@ use actix_web::{HttpRequest, HttpResponse, Result};
pub struct Auth;
+/// HTTP Basic authentication errors
pub enum BasicAuthError {
Base64DecodeError,
InvalidUsernameFormat,
}
#[derive(Clone, Debug)]
+/// HTTP Basic authentication parameters
pub struct BasicAuthParams {
pub username: String,
pub password: String,
diff --git a/src/listing.rs b/src/listing.rs
index aeddd7d..f0662ef 100644
--- a/src/listing.rs
+++ b/src/listing.rs
@@ -9,15 +9,28 @@ use std::path::Path;
use std::str::FromStr;
#[derive(Clone, Copy, Debug)]
+/// Available sorting methods
pub enum SortingMethods {
+ /// Natural sorting method
+ /// 1 -> 2 -> 3 -> 11
Natural,
+
+ /// Pure alphabetical sorting method
+ /// 1 -> 11 -> 2 -> 3
Alpha,
+
+ /// Directories are listed first, alphabetical sorting is also applied
+ /// 1/ -> 2/ -> 3/ -> 11 -> 12
DirsFirst,
}
#[derive(PartialEq)]
+/// Possible entry types
enum EntryType {
+ /// Entry is a directory
Directory,
+
+ /// Entry is a file
File,
}
@@ -31,10 +44,18 @@ impl PartialOrd for EntryType {
}
}
+/// Entry
struct Entry {
+ /// Name of the entry
name: String,
+
+ /// Type of the entry
entry_type: EntryType,
+
+ /// URL of the entry
link: String,
+
+ /// Size in byte of the entry. Only available for EntryType::File
size: Option<bytesize::ByteSize>,
}
@@ -72,7 +93,8 @@ pub fn file_handler(req: &HttpRequest<crate::MiniserveConfig>) -> Result<fs::Nam
Ok(fs::NamedFile::open(path)?)
}
-// ↓ Adapted from https://docs.rs/actix-web/0.7.13/src/actix_web/fs.rs.html#564
+/// List a directory and renders a HTML file accordingly
+/// Adapted from https://docs.rs/actix-web/0.7.13/src/actix_web/fs.rs.html#564
pub fn directory_listing<S>(
dir: &fs::Directory,
req: &HttpRequest<S>,
diff --git a/src/main.rs b/src/main.rs
index 080124d..7c31976 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,16 +12,36 @@ mod auth;
mod listing;
#[derive(Clone, Debug)]
+/// Configuration of the Miniserve application
pub struct MiniserveConfig {
+ /// Enable verbose mode
pub verbose: bool,
+
+ /// Path to be served by miniserve
pub path: std::path::PathBuf,
+
+ /// Port on which miniserve will be listening
pub port: u16,
+
+ /// IP address(es) on which miniserve will be available
pub interfaces: Vec<IpAddr>,
+
+ /// Enable HTTP basic authentication
pub auth: Option<auth::BasicAuthParams>,
+
+ /// If false, miniserve will serve the current working directory
pub path_explicitly_chosen: bool,
+
+ /// Enable symlink resolution
pub no_symlinks: bool,
+
+ /// Enable random route generation
pub random_route: Option<String>,
+
+ /// Sort files/directories
pub sort_method: listing::SortingMethods,
+
+ /// Enable inverse sorting
pub reverse_sort: bool,
}
@@ -152,7 +172,8 @@ fn main() {
let _ = sys.run();
}
-pub fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
+/// Configures the Actix application
+fn configure_app(app: App<MiniserveConfig>) -> App<MiniserveConfig> {
let s = {
let path = &app.state().path;
let no_symlinks = app.state().no_symlinks;