From 529fed3dcafe6616e02590129572e8269440f26a Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 19 Jul 2014 15:35:31 +0100 Subject: [PATCH] Add {RelativeSchemeData,Url}::to_file_path() methods. --- src/url.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/url.rs b/src/url.rs index 51b2b9a..86810f6 100644 --- a/src/url.rs +++ b/src/url.rs @@ -189,6 +189,14 @@ impl Url { UrlParser::new().parse(input) } + #[inline] + pub fn to_file_path(&self) -> Result { + match self.scheme_data { + RelativeSchemeData(ref scheme_data) => scheme_data.to_file_path(), + OtherSchemeData(..) => Err(()), + } + } + pub fn serialize(&self) -> String { let mut result = self.serialize_no_fragment(); match self.fragment { @@ -313,6 +321,27 @@ impl Url { impl RelativeSchemeData { + // FIXME: Figure out what to do on Windows. + #[cfg(unix)] + pub fn to_file_path(&self) -> Result { + // FIXME: Figure out what to do w.r.t host. + match self.domain() { + Some("") => { + if self.path.is_empty() { + Ok(Path::new("/")) + } else { + let mut bytes = Vec::new(); + for path_part in self.path.iter() { + bytes.push(b'/'); + percent_decode(path_part.as_bytes(), &mut bytes); + } + Ok(Path::new(bytes)) + } + } + _ => Err(()) + } + } + #[inline] pub fn domain<'a>(&'a self) -> Option<&'a str> { match self.host {