134 lines
3.9 KiB
Rust
134 lines
3.9 KiB
Rust
|
|
use crate::sharry::{AliasID, FileID, ShareID, Uri};
|
||
|
|
|
||
|
|
const VALID_URI: &'static str = "scheme://valid.uri";
|
||
|
|
const VALID_ALIAS: &'static str = "valid-alias";
|
||
|
|
const VALID_SHARE: &'static str = "valid-share";
|
||
|
|
const VALID_FILE: &'static str = "valid-file";
|
||
|
|
|
||
|
|
pub fn check_uri(uri: &Uri) -> crate::Result<&Uri> {
|
||
|
|
if uri.to_string() != VALID_URI {
|
||
|
|
Err(uri.into())
|
||
|
|
} else {
|
||
|
|
Ok(uri)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn check_alias<'t>(uri: &Uri, alias_id: &'t AliasID) -> crate::Result<&'t AliasID> {
|
||
|
|
check_uri(uri)?;
|
||
|
|
|
||
|
|
if alias_id.as_ref() != VALID_ALIAS {
|
||
|
|
Err(alias_id.into())
|
||
|
|
} else {
|
||
|
|
Ok(alias_id)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn check_share(share_id: &ShareID) -> crate::Result<&ShareID> {
|
||
|
|
if share_id.to_string() != VALID_SHARE {
|
||
|
|
Err(share_id.into())
|
||
|
|
} else {
|
||
|
|
Ok(share_id)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn check_file<'t>(share_id: &ShareID, file_id: &'t FileID) -> crate::Result<&'t FileID> {
|
||
|
|
check_share(share_id)?;
|
||
|
|
|
||
|
|
if file_id.to_string() != VALID_FILE {
|
||
|
|
Err(file_id.into())
|
||
|
|
} else {
|
||
|
|
Ok(file_id)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn make_invalid(valid: &str) -> String {
|
||
|
|
let invalid = valid.replace("valid", "invalid");
|
||
|
|
assert_ne!(valid, invalid);
|
||
|
|
|
||
|
|
invalid
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<bool> for Uri {
|
||
|
|
fn from(value: bool) -> Self {
|
||
|
|
match value {
|
||
|
|
true => Self::from(VALID_URI.to_string()),
|
||
|
|
false => Self::from(make_invalid(VALID_URI)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<bool> for AliasID {
|
||
|
|
fn from(value: bool) -> Self {
|
||
|
|
match value {
|
||
|
|
true => Self::from(VALID_ALIAS.to_string()),
|
||
|
|
false => Self::from(make_invalid(VALID_ALIAS)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<bool> for ShareID {
|
||
|
|
fn from(value: bool) -> Self {
|
||
|
|
match value {
|
||
|
|
true => Self::from(VALID_SHARE.to_string()),
|
||
|
|
false => Self::from(make_invalid(VALID_SHARE)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<bool> for FileID {
|
||
|
|
fn from(value: bool) -> Self {
|
||
|
|
match value {
|
||
|
|
true => Self::new_test(VALID_FILE),
|
||
|
|
false => Self::new_test(make_invalid(VALID_FILE)),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use crate::Parameter;
|
||
|
|
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn true_makes_valids() {
|
||
|
|
assert!(check_uri(&true.into()).is_ok());
|
||
|
|
assert!(check_alias(&true.into(), &true.into()).is_ok());
|
||
|
|
assert!(check_share(&true.into()).is_ok());
|
||
|
|
assert!(check_file(&true.into(), &true.into()).is_ok());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn false_makes_invalids() {
|
||
|
|
// invalid Uri
|
||
|
|
let check = check_uri(&false.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::Uri(_)));
|
||
|
|
|
||
|
|
// invalid Uri, valid alias
|
||
|
|
let check = check_alias(&false.into(), &true.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::Uri(_)));
|
||
|
|
|
||
|
|
// valid Uri, invalid alias
|
||
|
|
let check = check_alias(&true.into(), &false.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::AliasID(_)));
|
||
|
|
|
||
|
|
// invalid share
|
||
|
|
let check = check_share(&false.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::ShareID(_)));
|
||
|
|
|
||
|
|
// invalid share, valid file
|
||
|
|
let check = check_file(&false.into(), &true.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::ShareID(_)));
|
||
|
|
|
||
|
|
// valid share, invalid file
|
||
|
|
let check = check_file(&true.into(), &false.into()).expect_err("should be invalid");
|
||
|
|
let p = check.get_invalid_param().expect("should be InvalidParam");
|
||
|
|
assert!(matches!(p, Parameter::FileID(_)));
|
||
|
|
}
|
||
|
|
}
|