Bug 1151899 - rust-url-capi error code changes
MozReview-Commit-ID: AHwbkgGifmr
--- a/netwerk/base/rust-url-capi/src/error_mapping.rs
+++ b/netwerk/base/rust-url-capi/src/error_mapping.rs
@@ -1,57 +1,50 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
use url::ParseError;
pub trait ErrorCode {
fn error_code(&self) -> i32;
}
impl<T: ErrorCode> ErrorCode for Result<(), T> {
fn error_code(&self) -> i32 {
match *self {
Ok(_) => 0,
Err(ref error) => error.error_code(),
}
}
}
-impl ErrorCode for () {
- fn error_code(&self) -> i32 {
- return -1;
+impl ErrorCode for Result<(), ()> {
+ fn error_code(&self) -> i32 {
+ match *self {
+ Ok(_) => 0,
+ Err(_) => -255,
}
+ }
}
+
impl ErrorCode for ParseError {
fn error_code(&self) -> i32 {
- return -1;
-// match *self {
-// ParseError::EmptyHost => -1,
-// ParseError::InvalidScheme => -2,
-// ParseError::InvalidPort => -3,
-// ParseError::InvalidIpv6Address => -4,
-// ParseError::InvalidDomainCharacter => -5,
-// ParseError::InvalidCharacter => -6,
-// ParseError::InvalidBackslash => -7,
-// ParseError::InvalidPercentEncoded => -8,
-// ParseError::InvalidAtSymbolInUser => -9,
-// ParseError::ExpectedTwoSlashes => -10,
-// ParseError::ExpectedInitialSlash => -11,
-// ParseError::NonUrlCodePoint => -12,
-// ParseError::RelativeUrlWithScheme => -13,
-// ParseError::RelativeUrlWithoutBase => -14,
-// ParseError::RelativeUrlWithNonRelativeBase => -15,
-// ParseError::NonAsciiDomainsNotSupportedYet => -16,
-// ParseError::CannotSetJavascriptFragment => -17,
-// ParseError::CannotSetPortWithFileLikeScheme => -18,
-// ParseError::CannotSetUsernameWithNonRelativeScheme => -19,
-// ParseError::CannotSetPasswordWithNonRelativeScheme => -20,
-// ParseError::CannotSetHostPortWithNonRelativeScheme => -21,
-// ParseError::CannotSetHostWithNonRelativeScheme => -22,
-// ParseError::CannotSetPortWithNonRelativeScheme => -23,
-// ParseError::CannotSetPathWithNonRelativeScheme => -24,
-// }
+ match *self {
+ ParseError::EmptyHost => -1,
+ ParseError::InvalidPort => -2,
+ ParseError::InvalidIpv6Address => -3,
+ ParseError::InvalidDomainCharacter => -4,
+ ParseError::IdnaError => -5,
+ ParseError::InvalidIpv4Address => -6,
+ ParseError::RelativeUrlWithoutBase => -7,
+ ParseError::RelativeUrlWithCannotBeABaseBase => -8,
+ ParseError::SetHostOnCannotBeABaseUrl => -9,
+ ParseError::Overflow => -10,
+ }
}
}
pub enum NSError {
OK,
InvalidArg,
Failure,
}
--- a/netwerk/base/rust-url-capi/src/lib.rs
+++ b/netwerk/base/rust-url-capi/src/lib.rs
@@ -1,8 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
extern crate url;
use url::{Url, ParseError, ParseOptions};
use url::quirks;
extern crate libc;
use libc::size_t;
use std::mem;
@@ -315,49 +319,53 @@ pub unsafe extern "C" fn rusturl_set_pat
let mut url: &mut Url = mem::transmute(urlptr);
let slice = std::slice::from_raw_parts(path as *const libc::c_uchar, len as usize);
let path_ = match str::from_utf8(slice).ok() {
Some(p) => p,
None => return ParseError::InvalidDomainCharacter.error_code() // utf-8 failed
};
- quirks::set_pathname(url, path_).error_code()
+ quirks::set_pathname(url, path_);
+ NSError::OK.error_code()
+
}
#[no_mangle]
pub unsafe extern "C" fn rusturl_set_query(urlptr: rusturl_ptr, query: *mut libc::c_char, len: size_t) -> i32 {
if urlptr.is_null() {
return NSError::InvalidArg.error_code();
}
let mut url: &mut Url = mem::transmute(urlptr);
let slice = std::slice::from_raw_parts(query as *const libc::c_uchar, len as usize);
let query_ = match str::from_utf8(slice).ok() {
Some(p) => p,
None => return ParseError::InvalidDomainCharacter.error_code() // utf-8 failed
};
- quirks::set_search(url, query_).error_code()
+ quirks::set_search(url, query_);
+ NSError::OK.error_code()
}
#[no_mangle]
pub unsafe extern "C" fn rusturl_set_fragment(urlptr: rusturl_ptr, fragment: *mut libc::c_char, len: size_t) -> i32 {
if urlptr.is_null() {
return NSError::InvalidArg.error_code();
}
let mut url: &mut Url = mem::transmute(urlptr);
let slice = std::slice::from_raw_parts(fragment as *const libc::c_uchar, len as usize);
let fragment_ = match str::from_utf8(slice).ok() {
Some(p) => p,
None => return ParseError::InvalidDomainCharacter.error_code() // utf-8 failed
};
- quirks::set_hash(url, fragment_).error_code()
+ quirks::set_hash(url, fragment_);
+ NSError::OK.error_code()
}
#[no_mangle]
pub unsafe extern "C" fn rusturl_resolve(urlptr: rusturl_ptr, resolve: *mut libc::c_char, len: size_t, cont: *mut libc::c_void) -> i32 {
if urlptr.is_null() {
return NSError::InvalidArg.error_code();
}
let url: &mut Url = mem::transmute(urlptr);
@@ -470,8 +478,12 @@ pub unsafe extern "C" fn rusturl_relativ
}
for p2 in path2 {
buffer = buffer + p2 + "/";
}
return cont.assign(&buffer);
}
+#[no_mangle]
+pub unsafe extern "C" fn sizeof_rusturl() -> size_t {
+ mem::size_of::<Url>()
+}
--- a/netwerk/base/rust-url-capi/src/rust-url-capi.h
+++ b/netwerk/base/rust-url-capi/src/rust-url-capi.h
@@ -1,8 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
#ifndef __RUST_URL_CAPI
#define __RUST_URL_CAPI
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -33,13 +37,15 @@ int32_t rusturl_set_port_no(rusturl_ptr
int32_t rusturl_set_path(rusturl_ptr url, const char *path, size_t len);
int32_t rusturl_set_query(rusturl_ptr url, const char *path, size_t len);
int32_t rusturl_set_fragment(rusturl_ptr url, const char *path, size_t len);
int32_t rusturl_resolve(rusturl_ptr url, const char *relative, size_t len, void*);
int32_t rusturl_common_base_spec(rusturl_ptr url1, rusturl_ptr url2, void*);
int32_t rusturl_relative_spec(rusturl_ptr url1, rusturl_ptr url2, void*);
+size_t sizeof_rusturl();
+
#ifdef __cplusplus
}
#endif
-#endif // __RUST_URL_CAPI
\ No newline at end of file
+#endif // __RUST_URL_CAPI
--- a/netwerk/base/rust-url-capi/src/string_utils.rs
+++ b/netwerk/base/rust-url-capi/src/string_utils.rs
@@ -1,8 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
extern crate libc;
use libc::size_t;
extern crate std;
use std::ptr;
use error_mapping::*;
--- a/netwerk/base/rust-url-capi/test/test.cpp
+++ b/netwerk/base/rust-url-capi/test/test.cpp
@@ -1,8 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "../src/rust-url-capi.h"
class StringContainer
{
public: