2019-09-06 10:55:00 +00:00
|
|
|
use crate::{ErrorKind, Result};
|
|
|
|
use trackable::error::ErrorKindExt;
|
|
|
|
|
2019-09-08 09:30:52 +00:00
|
|
|
pub(crate) fn parse_yes_or_no<T: AsRef<str>>(s: T) -> Result<bool> {
|
|
|
|
match s.as_ref() {
|
2019-09-06 10:55:00 +00:00
|
|
|
"YES" => Ok(true),
|
|
|
|
"NO" => Ok(false),
|
2019-09-08 09:30:52 +00:00
|
|
|
_ => track_panic!(
|
|
|
|
ErrorKind::InvalidInput,
|
|
|
|
"Unexpected value: {:?}",
|
|
|
|
s.as_ref()
|
|
|
|
),
|
2019-09-06 10:55:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 09:30:52 +00:00
|
|
|
pub(crate) fn parse_u64<T: AsRef<str>>(s: T) -> Result<u64> {
|
|
|
|
let n = track!(s
|
|
|
|
.as_ref()
|
|
|
|
.parse()
|
|
|
|
.map_err(|e| ErrorKind::InvalidInput.cause(e)))?;
|
2019-09-06 10:55:00 +00:00
|
|
|
Ok(n)
|
|
|
|
}
|
2019-09-08 09:30:52 +00:00
|
|
|
|
|
|
|
/// According to the documentation the following characters are forbidden
|
|
|
|
/// inside a quoted string:
|
|
|
|
/// - carriage return (`\r`)
|
|
|
|
/// - new line (`\n`)
|
|
|
|
/// - double quotes (`"`)
|
|
|
|
///
|
|
|
|
/// Therefore it is safe to simply remove any occurence of those characters.
|
|
|
|
/// [rfc8216#section-4.2](https://tools.ietf.org/html/rfc8216#section-4.2)
|
|
|
|
pub(crate) fn unquote<T: ToString>(value: T) -> String {
|
|
|
|
value
|
|
|
|
.to_string()
|
|
|
|
.replace("\"", "")
|
|
|
|
.replace("\n", "")
|
|
|
|
.replace("\r", "")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Puts a string inside quotes.
|
|
|
|
pub(crate) fn quote<T: ToString>(value: T) -> String {
|
|
|
|
// the replace is for the case, that quote is called on an already quoted string, which could
|
|
|
|
// cause problems!
|
|
|
|
format!("\"{}\"", value.to_string().replace("\"", ""))
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:05:20 +00:00
|
|
|
/// Checks, if the given tag is at the start of the input. If this is the case, it will remove it
|
|
|
|
/// return the rest of the input, otherwise it will return an error.
|
|
|
|
pub(crate) fn tag<T>(input: &str, tag: T) -> crate::Result<&str>
|
|
|
|
where
|
|
|
|
T: AsRef<str>,
|
|
|
|
{
|
|
|
|
if !input.starts_with(tag.as_ref()) {
|
|
|
|
Err(ErrorKind::InvalidInput)?; // TODO!
|
|
|
|
}
|
|
|
|
let result = input.split_at(tag.as_ref().len()).1;
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
2019-09-08 09:30:52 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_yes_or_no() {
|
|
|
|
assert!(parse_yes_or_no("YES").unwrap());
|
|
|
|
assert!(!parse_yes_or_no("NO").unwrap());
|
|
|
|
// TODO: test for error
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_u64() {
|
|
|
|
assert_eq!(parse_u64("1").unwrap(), 1);
|
|
|
|
assert_eq!(parse_u64("25").unwrap(), 25);
|
|
|
|
// TODO: test for error
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unquote() {
|
|
|
|
assert_eq!(unquote("\"TestValue\""), "TestValue".to_string());
|
|
|
|
assert_eq!(unquote("\"TestValue\n\""), "TestValue".to_string());
|
|
|
|
assert_eq!(unquote("\"TestValue\n\r\""), "TestValue".to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_quote() {
|
|
|
|
assert_eq!(quote("value"), "\"value\"".to_string());
|
|
|
|
assert_eq!(quote("\"value\""), "\"value\"".to_string());
|
|
|
|
}
|
2019-09-10 09:05:20 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tag() {
|
|
|
|
let input = "HelloMyFriendThisIsASampleString";
|
|
|
|
|
|
|
|
let input = tag(input, "Hello").unwrap();
|
|
|
|
assert_eq!(input, "MyFriendThisIsASampleString");
|
|
|
|
|
|
|
|
let input = tag(input, "My").unwrap();
|
|
|
|
assert_eq!(input, "FriendThisIsASampleString");
|
|
|
|
|
|
|
|
let input = tag(input, "FriendThisIs").unwrap();
|
|
|
|
assert_eq!(input, "ASampleString");
|
|
|
|
|
|
|
|
let input = tag(input, "A").unwrap();
|
|
|
|
assert_eq!(input, "SampleString");
|
|
|
|
}
|
2019-09-08 09:30:52 +00:00
|
|
|
}
|