Merge pull request #14 from robatipoor/master

some code refactor
This commit is contained in:
Vincent Flyson 2019-08-23 05:21:27 -04:00 committed by GitHub
commit 1480b20cb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 10 deletions

View file

@ -13,7 +13,7 @@ pub fn is_url(path: &str) -> bool {
} }
pub fn resolve_url(from: &str, to: &str) -> Result<String, ParseError> { pub fn resolve_url(from: &str, to: &str) -> Result<String, ParseError> {
let mut result = "".to_string(); let mut result = String::new();
if is_url(to) { // (anything, http://site.com/css/main.css) if is_url(to) { // (anything, http://site.com/css/main.css)
result = to.to_string(); result = to.to_string();
@ -51,9 +51,9 @@ pub fn resolve_url(from: &str, to: &str) -> Result<String, ParseError> {
} }
pub fn url_is_data(url: &str) -> Result<bool, String> { pub fn url_is_data(url: &str) -> Result<bool, String> {
match Url::parse(&url) { match Url::parse(url) {
Ok(parsed_url) => Ok(parsed_url.scheme() == "data"), Ok(parsed_url) => Ok(parsed_url.scheme() == "data"),
Err(err) => return Err(format!("{}", err.to_string())), Err(err) => return Err(format!("{}", err)),
} }
} }

View file

@ -17,12 +17,7 @@ pub fn data_to_dataurl(mime: &str, data: &[u8]) -> String {
} }
fn detect_mimetype(data: &[u8]) -> String { fn detect_mimetype(data: &[u8]) -> String {
let detected_mimetype = data.sniff_mime_type(); data.sniff_mime_type().unwrap_or("").to_string()
if detected_mimetype != None {
detected_mimetype.unwrap().to_string()
} else {
"".to_string()
}
} }
#[cfg(test)] #[cfg(test)]
@ -34,6 +29,9 @@ mod tests {
let mime = "application/javascript"; let mime = "application/javascript";
let data = "var word = 'hello';\nalert(word);\n"; let data = "var word = 'hello';\nalert(word);\n";
let datauri = data_to_dataurl(mime, data.as_bytes()); let datauri = data_to_dataurl(mime, data.as_bytes());
assert_eq!(&datauri, "data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"); assert_eq!(
&datauri,
"data:application/javascript;base64,dmFyIHdvcmQgPSAnaGVsbG8nOwphbGVydCh3b3JkKTsK"
);
} }
} }