1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::str::FromStr;

use crate::{InputType, InputValueError};

pub fn url<T: AsRef<str> + InputType>(value: &T) -> Result<(), InputValueError<T>> {
    if let Ok(true) = http::uri::Uri::from_str(value.as_ref())
        .map(|uri| uri.scheme().is_some() && uri.authority().is_some())
    {
        Ok(())
    } else {
        Err("invalid url".into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_url() {
        assert!(url(&"http".to_string()).is_err());
        assert!(url(&"https://google.com".to_string()).is_ok());
        assert!(url(&"http://localhost:80".to_string()).is_ok());
        assert!(url(&"ftp://localhost:80".to_string()).is_ok());
    }
}