Skip to content

Commit 2344f91

Browse files
committed
Use helper function for common url parsing steps
1 parent d077f56 commit 2344f91

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

gix-url/src/parse/mod.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl From<Infallible> for Error {
3030
}
3131

3232
///
33-
#[derive(Debug)]
33+
#[derive(Debug, Clone)]
3434
pub enum UrlKind {
3535
///
3636
Url,
@@ -93,16 +93,7 @@ pub fn parse(input: &BStr) -> Result<crate::Url, Error> {
9393
}
9494

9595
fn parse_url(input: &BStr) -> Result<crate::Url, Error> {
96-
let input = std::str::from_utf8(input).map_err(|source| Error::Utf8 {
97-
url: input.to_owned(),
98-
kind: UrlKind::Url,
99-
source,
100-
})?;
101-
let url = url::Url::parse(input).map_err(|source| Error::Url {
102-
url: input.to_owned(),
103-
kind: UrlKind::Url,
104-
source,
105-
})?;
96+
let (input, url) = input_to_utf8_and_url(input, UrlKind::Url)?;
10697

10798
let scheme = url.scheme().into();
10899

@@ -133,11 +124,7 @@ fn parse_url(input: &BStr) -> Result<crate::Url, Error> {
133124
}
134125

135126
fn parse_scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
136-
let input = std::str::from_utf8(input).map_err(|source| Error::Utf8 {
137-
url: input.to_owned(),
138-
kind: UrlKind::Scp,
139-
source,
140-
})?;
127+
let input = input_to_utf8(input, UrlKind::Scp)?;
141128

142129
// TODO: this incorrectly splits at IPv6 addresses, check for `[]` before splitting
143130
let (host, path) = input.split_at(colon);
@@ -176,16 +163,7 @@ fn parse_scp(input: &BStr, colon: usize) -> Result<crate::Url, Error> {
176163
}
177164

178165
fn parse_file_url(input: &BStr, protocol_colon: usize) -> Result<crate::Url, Error> {
179-
let input = std::str::from_utf8(input).map_err(|source| Error::Utf8 {
180-
url: input.to_owned(),
181-
kind: UrlKind::Url,
182-
source,
183-
})?;
184-
let url = url::Url::parse(input).map_err(|source| Error::Url {
185-
url: input.to_owned(),
186-
kind: UrlKind::Url,
187-
source,
188-
})?;
166+
let (input, url) = input_to_utf8_and_url(input, UrlKind::Url)?;
189167

190168
if !input[protocol_colon + 3..].contains('/') {
191169
return Err(Error::MissingRepositoryPath {
@@ -215,3 +193,26 @@ fn parse_local(input: &BStr, was_in_url_format: bool) -> Result<crate::Url, Erro
215193
path: input.into(),
216194
})
217195
}
196+
197+
/// Helper function to turn a BStr into an str. The kind is only used for the construction of the
198+
/// error variant.
199+
fn input_to_utf8(input: &BStr, kind: UrlKind) -> Result<&str, Error> {
200+
std::str::from_utf8(input).map_err(|source| Error::Utf8 {
201+
url: input.to_owned(),
202+
kind,
203+
source,
204+
})
205+
}
206+
207+
/// Helper function to turn a BStr into an Url. The kind is only used for the construction of the
208+
/// error variant.
209+
fn input_to_utf8_and_url(input: &BStr, kind: UrlKind) -> Result<(&str, url::Url), Error> {
210+
let input = input_to_utf8(input, kind.clone())?;
211+
url::Url::parse(input)
212+
.map(|url| (input, url))
213+
.map_err(|source| Error::Url {
214+
url: input.to_owned(),
215+
kind,
216+
source,
217+
})
218+
}

0 commit comments

Comments
 (0)