git_url/
impls.rs

1use std::{
2    convert::TryFrom,
3    path::{Path, PathBuf},
4};
5
6use bstr::BStr;
7
8use crate::{parse, Scheme, Url};
9
10impl Default for Url {
11    fn default() -> Self {
12        Url {
13            serialize_alternative_form: false,
14            scheme: Scheme::Ssh,
15            user: None,
16            host: None,
17            port: None,
18            path: bstr::BString::default(),
19        }
20    }
21}
22
23impl TryFrom<&str> for Url {
24    type Error = parse::Error;
25
26    fn try_from(value: &str) -> Result<Self, Self::Error> {
27        Self::from_bytes(value.into())
28    }
29}
30
31impl TryFrom<String> for Url {
32    type Error = parse::Error;
33
34    fn try_from(value: String) -> Result<Self, Self::Error> {
35        Self::from_bytes(value.as_str().into())
36    }
37}
38
39impl TryFrom<PathBuf> for Url {
40    type Error = parse::Error;
41
42    fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
43        git_path::into_bstr(value).try_into()
44    }
45}
46
47impl TryFrom<&Path> for Url {
48    type Error = parse::Error;
49
50    fn try_from(value: &Path) -> Result<Self, Self::Error> {
51        git_path::into_bstr(value).try_into()
52    }
53}
54
55impl TryFrom<&std::ffi::OsStr> for Url {
56    type Error = parse::Error;
57
58    fn try_from(value: &std::ffi::OsStr) -> Result<Self, Self::Error> {
59        git_path::os_str_into_bstr(value)
60            .expect("no illformed UTF-8 on Windows")
61            .try_into()
62    }
63}
64
65impl TryFrom<&BStr> for Url {
66    type Error = parse::Error;
67
68    fn try_from(value: &BStr) -> Result<Self, Self::Error> {
69        Self::from_bytes(value)
70    }
71}
72
73impl<'a> TryFrom<std::borrow::Cow<'a, BStr>> for Url {
74    type Error = parse::Error;
75
76    fn try_from(value: std::borrow::Cow<'a, BStr>) -> Result<Self, Self::Error> {
77        Self::try_from(&*value)
78    }
79}