Refactor: Use AsRef<str> instead of String as arg type

This commit is contained in:
Matthias Beyer 2018-04-24 20:56:07 +02:00
parent 488ce9fe91
commit 775508a6ed

View file

@ -23,27 +23,27 @@
use std::path::PathBuf; use std::path::PathBuf;
use boolinator::Boolinator; use boolinator::Boolinator;
pub fn is_existing_path(s: String) -> Result<(), String> { pub fn is_existing_path<A: AsRef<str>>(s: A) -> Result<(), String> {
PathBuf::from(s.clone()).exists().as_result((), format!("Not a File or Directory: {}", s)) PathBuf::from(s.as_ref()).exists().as_result((), format!("Not a File or Directory: {}", s.as_ref()))
} }
pub fn is_file(s: String) -> Result<(), String> { pub fn is_file<A: AsRef<str>>(s: A) -> Result<(), String> {
PathBuf::from(s.clone()).is_file().as_result((), format!("Not a File: {}", s)) PathBuf::from(s.as_ref()).is_file().as_result((), format!("Not a File: {}", s.as_ref()))
} }
pub fn is_directory(s: String) -> Result<(), String> { pub fn is_directory<A: AsRef<str>>(s: A) -> Result<(), String> {
PathBuf::from(s.clone()).is_dir().as_result((), format!("Not a Directory: {}", s)) PathBuf::from(s.as_ref()).is_dir().as_result((), format!("Not a Directory: {}", s.as_ref()))
} }
pub fn is_integer(s: String) -> Result<(), String> { pub fn is_integer<A: AsRef<str>>(s: A) -> Result<(), String> {
use std::str::FromStr; use std::str::FromStr;
let i : Result<i64, _> = FromStr::from_str(&s); let i : Result<i64, _> = FromStr::from_str(s.as_ref());
i.map(|_| ()).map_err(|_| format!("Not an integer: {}", s)) i.map(|_| ()).map_err(|_| format!("Not an integer: {}", s.as_ref()))
} }
pub fn is_url(s: String) -> Result<(), String> { pub fn is_url<A: AsRef<str>>(s: A) -> Result<(), String> {
use url::Url; use url::Url;
Url::parse(&s).map(|_| ()).map_err(|_| format!("Not a URL: {}", s)) Url::parse(s.as_ref()).map(|_| ()).map_err(|_| format!("Not a URL: {}", s.as_ref()))
} }