Просмотр исходного кода

Add an Url::from_file_path() static method.

Simon Sapin 12 лет назад
Родитель
Сommit
1805b2f26f
1 измененных файлов с 35 добавлено и 1 удалено
  1. 35 1
      src/url.rs

+ 35 - 1
src/url.rs

@@ -18,11 +18,13 @@ extern crate serialize;
 
 
 use std::cmp;
 use std::cmp;
 use std::hash;
 use std::hash;
+use std::path;
+use std::path::Path;
 use std::ascii::OwnedStrAsciiExt;
 use std::ascii::OwnedStrAsciiExt;
 
 
 use encoding::EncodingRef;
 use encoding::EncodingRef;
 
 
-use encode_sets::{PASSWORD_ENCODE_SET, USERNAME_ENCODE_SET};
+use encode_sets::{PASSWORD_ENCODE_SET, USERNAME_ENCODE_SET, DEFAULT_ENCODE_SET};
 
 
 
 
 mod encode_sets;
 mod encode_sets;
@@ -189,6 +191,24 @@ impl Url {
         UrlParser::new().parse(input)
         UrlParser::new().parse(input)
     }
     }
 
 
+    // FIXME: Figure out what to do on Windows
+    #[cfg(unix)]
+    pub fn from_file_path(&self, path: &Path) -> Result<Url, ()> {
+        let path = try!(encode_file_path(path));
+        Ok(Url {
+            scheme: "file".to_string(),
+            scheme_data: RelativeSchemeData(RelativeSchemeData {
+                username: "".to_string(),
+                password: None,
+                port: "".to_string(),
+                host: Domain("".to_string()),
+                path: path,
+            }),
+            query: None,
+            fragment: None,
+        })
+    }
+
     #[inline]
     #[inline]
     pub fn to_file_path(&self) -> Result<Path, ()> {
     pub fn to_file_path(&self) -> Result<Path, ()> {
         match self.scheme_data {
         match self.scheme_data {
@@ -794,3 +814,17 @@ pub fn percent_decode(input: &[u8], output: &mut Vec<u8>) {
         i += 1;
         i += 1;
     }
     }
 }
 }
+
+// FIXME: Figure out what to do on Windows
+#[cfg(unix)]
+fn encode_file_path(path: &Path) -> Result<Vec<String>, ()> {
+    let path = path.as_vec();
+    if !path::is_sep_byte(&path[0]) {
+        return Err(())  // Not an absolute path
+    }
+    Ok(path.slice_from(1).split(path::is_sep_byte).map(|path_part| {
+        let mut encoded = String::new();
+        percent_encode(path_part, DEFAULT_ENCODE_SET, &mut encoded);
+        encoded
+    }).collect())
+}