aboutsummaryrefslogtreecommitdiffstats
path: root/src/args.rs
diff options
context:
space:
mode:
authorSven-Hendrik Haase <svenstaro@gmail.com>2019-04-26 02:42:52 +0000
committerGitHub <noreply@github.com>2019-04-26 02:42:52 +0000
commit442ff6b1142ab03f8abc221896d0cb7228678407 (patch)
treea6de481eaf136eb9d5c739e99c14cae349c5c01a /src/args.rs
parentMerge pull request #82 from svenstaro/dependabot/cargo/pretty_assertions-0.6.1 (diff)
parentMerge branch 'master' into no_panics (diff)
downloadminiserve-442ff6b1142ab03f8abc221896d0cb7228678407.tar.gz
miniserve-442ff6b1142ab03f8abc221896d0cb7228678407.zip
Merge pull request #80 from boastful-squirrel/no_panics
Revamp errors + avoid panics in main()
Diffstat (limited to 'src/args.rs')
-rw-r--r--src/args.rs15
1 files changed, 7 insertions, 8 deletions
diff --git a/src/args.rs b/src/args.rs
index 825a4ac..8d2e105 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -3,6 +3,7 @@ use std::path::PathBuf;
use structopt::StructOpt;
use crate::auth;
+use crate::errors::{ContextualError, ContextualErrorKind};
use crate::themes;
/// Possible characters for random routes
@@ -76,15 +77,13 @@ fn parse_interface(src: &str) -> Result<IpAddr, std::net::AddrParseError> {
}
/// Checks wether the auth string is valid, i.e. it follows the syntax username:password
-fn parse_auth(src: &str) -> Result<(String, String), String> {
+fn parse_auth(src: &str) -> Result<(String, String), ContextualError> {
let mut split = src.splitn(2, ':');
let username = match split.next() {
Some(username) => username,
None => {
- return Err(
- "Invalid credentials string, expected format is username:password".to_owned(),
- )
+ return Err(ContextualError::new(ContextualErrorKind::InvalidAuthFormat));
}
};
@@ -92,9 +91,7 @@ fn parse_auth(src: &str) -> Result<(String, String), String> {
// This allows empty passwords, as the spec does not forbid it
Some(password) => password,
None => {
- return Err(
- "Invalid credentials string, expected format is username:password".to_owned(),
- )
+ return Err(ContextualError::new(ContextualErrorKind::InvalidAuthFormat));
}
};
@@ -102,7 +99,9 @@ fn parse_auth(src: &str) -> Result<(String, String), String> {
// After 255 characters, Windows will truncate the value.
// As for the username, the spec does not mention a limit in length
if password.len() > 255 {
- return Err("Password length cannot exceed 255 characters".to_owned());
+ return Err(ContextualError::new(
+ ContextualErrorKind::PasswordTooLongError,
+ ));
}
Ok((username.to_owned(), password.to_owned()))