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