aboutsummaryrefslogtreecommitdiffstats
path: root/src/args.rs
diff options
context:
space:
mode:
authorboastful-squirrel <boastful.squirrel@gmail.com>2019-04-21 14:27:34 +0000
committerboastful-squirrel <boastful.squirrel@gmail.com>2019-04-21 14:27:34 +0000
commit8af3ff10e2347da70c35eb45046f8a04843f7256 (patch)
tree88b48970bfd3223c30eb820c21bf5b7df8d3511f /src/args.rs
parentcargo fmt (diff)
downloadminiserve-8af3ff10e2347da70c35eb45046f8a04843f7256.tar.gz
miniserve-8af3ff10e2347da70c35eb45046f8a04843f7256.zip
Rework error system + 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()))