web-url 0.11.0

This library aids in processing web-based URLs.
Documentation
use crate::Error;
use crate::Error::InvalidFragment;
use crate::Fragment;

/// Checks the optional `fragment`.
///
/// The `fragment` must be a valid fragment or be empty.
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);
        }
    }
}