gix_url/
impls.rs

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::path::{Path, PathBuf};

use bstr::BStr;

use crate::{parse, Scheme, Url};

impl Default for Url {
    fn default() -> Self {
        Url {
            serialize_alternative_form: false,
            scheme: Scheme::Ssh,
            user: None,
            password: None,
            host: None,
            port: None,
            path: bstr::BString::default(),
        }
    }
}

impl TryFrom<&str> for Url {
    type Error = parse::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_bytes(value.into())
    }
}

impl TryFrom<String> for Url {
    type Error = parse::Error;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_bytes(value.as_str().into())
    }
}

impl TryFrom<PathBuf> for Url {
    type Error = parse::Error;

    fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
        gix_path::into_bstr(value).try_into()
    }
}

impl TryFrom<&Path> for Url {
    type Error = parse::Error;

    fn try_from(value: &Path) -> Result<Self, Self::Error> {
        gix_path::into_bstr(value).try_into()
    }
}

impl TryFrom<&std::ffi::OsStr> for Url {
    type Error = parse::Error;

    fn try_from(value: &std::ffi::OsStr) -> Result<Self, Self::Error> {
        gix_path::os_str_into_bstr(value)
            .expect("no illformed UTF-8 on Windows")
            .try_into()
    }
}

impl TryFrom<&BStr> for Url {
    type Error = parse::Error;

    fn try_from(value: &BStr) -> Result<Self, Self::Error> {
        Self::from_bytes(value)
    }
}

impl<'a> TryFrom<std::borrow::Cow<'a, BStr>> for Url {
    type Error = parse::Error;

    fn try_from(value: std::borrow::Cow<'a, BStr>) -> Result<Self, Self::Error> {
        Self::try_from(&*value)
    }
}

impl std::fmt::Display for Url {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut storage;
        let to_print = if self.password.is_some() {
            storage = self.clone();
            storage.password = Some("<redacted>".into());
            &storage
        } else {
            self
        };
        to_print.to_bstring().fmt(f)
    }
}