use crate::Error;
use crate::Error::InvalidFragment;
use crate::Fragment;
pub fn check_fragment(fragment: &str) -> Result<(), Error> {
if fragment.is_empty() || Fragment::is_valid(fragment) {
Ok(())
} else {
Err(InvalidFragment)
}
}
#[cfg(test)]
mod tests {
use crate::Error;
use crate::Error::InvalidFragment;
use crate::parse::check_fragment;
#[test]
fn fn_check_fragment() {
let test_cases: &[(&str, Result<(), Error>)] = &[
("", Ok(())),
("fragment", Err(InvalidFragment)),
("#", Ok(())),
("#fragment", Ok(())),
("#\x00", Err(InvalidFragment)),
("#你好", Err(InvalidFragment)),
("#fragment ", Err(InvalidFragment)),
];
for (fragment, expected) in test_cases {
let result: Result<(), Error> = check_fragment(fragment);
assert_eq!(result, *expected, "fragment={}", fragment);
}
}
}