mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Format component of url
This commit is contained in:
parent
9070d4bc65
commit
812d4a479a
2 changed files with 25 additions and 15 deletions
|
@ -3,13 +3,15 @@
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
|
|
||||||
#![crate_name = "servo_url"]
|
#![crate_name = "servo_url"]
|
||||||
#![crate_type = "rlib"]
|
#![crate_type = "rlib"]
|
||||||
|
|
||||||
#[macro_use] extern crate malloc_size_of;
|
#[macro_use]
|
||||||
#[macro_use] extern crate malloc_size_of_derive;
|
extern crate malloc_size_of;
|
||||||
#[macro_use] extern crate serde;
|
#[macro_use]
|
||||||
|
extern crate malloc_size_of_derive;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde;
|
||||||
extern crate servo_rand;
|
extern crate servo_rand;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
extern crate url_serde;
|
extern crate url_serde;
|
||||||
|
@ -29,10 +31,7 @@ use url::{Url, Position};
|
||||||
pub use url::Host;
|
pub use url::Host;
|
||||||
|
|
||||||
#[derive(Clone, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd)]
|
#[derive(Clone, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd)]
|
||||||
pub struct ServoUrl(
|
pub struct ServoUrl(#[ignore_malloc_size_of = "Arc"] Arc<Url>);
|
||||||
#[ignore_malloc_size_of = "Arc"]
|
|
||||||
Arc<Url>
|
|
||||||
);
|
|
||||||
|
|
||||||
impl ServoUrl {
|
impl ServoUrl {
|
||||||
pub fn from_url(url: Url) -> Self {
|
pub fn from_url(url: Url) -> Self {
|
||||||
|
@ -40,11 +39,16 @@ impl ServoUrl {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_with_base(base: Option<&Self>, input: &str) -> Result<Self, url::ParseError> {
|
pub fn parse_with_base(base: Option<&Self>, input: &str) -> Result<Self, url::ParseError> {
|
||||||
Url::options().base_url(base.map(|b| &*b.0)).parse(input).map(Self::from_url)
|
Url::options()
|
||||||
|
.base_url(base.map(|b| &*b.0))
|
||||||
|
.parse(input)
|
||||||
|
.map(Self::from_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_string(self) -> String {
|
pub fn into_string(self) -> String {
|
||||||
Arc::try_unwrap(self.0).unwrap_or_else(|s| (*s).clone()).into_string()
|
Arc::try_unwrap(self.0)
|
||||||
|
.unwrap_or_else(|s| (*s).clone())
|
||||||
|
.into_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_url(self) -> Url {
|
pub fn into_url(self) -> Url {
|
||||||
|
@ -209,7 +213,8 @@ impl From<Url> for ServoUrl {
|
||||||
|
|
||||||
impl serde::Serialize for ServoUrl {
|
impl serde::Serialize for ServoUrl {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
where S: serde::Serializer,
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
url_serde::serialize(&*self.0, serializer)
|
url_serde::serialize(&*self.0, serializer)
|
||||||
}
|
}
|
||||||
|
@ -217,7 +222,8 @@ impl serde::Serialize for ServoUrl {
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for ServoUrl {
|
impl<'de> serde::Deserialize<'de> for ServoUrl {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||||
where D: serde::Deserializer<'de>,
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
{
|
{
|
||||||
url_serde::deserialize(deserializer).map(Self::from_url)
|
url_serde::deserialize(deserializer).map(Self::from_url)
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,10 +18,13 @@ pub enum ImmutableOrigin {
|
||||||
/// Consists of the URL's scheme, host and port
|
/// Consists of the URL's scheme, host and port
|
||||||
Tuple(
|
Tuple(
|
||||||
String,
|
String,
|
||||||
#[serde(deserialize_with = "url_serde::deserialize", serialize_with = "url_serde::serialize")]
|
#[serde(
|
||||||
|
deserialize_with = "url_serde::deserialize",
|
||||||
|
serialize_with = "url_serde::serialize"
|
||||||
|
)]
|
||||||
Host,
|
Host,
|
||||||
u16,
|
u16,
|
||||||
)
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImmutableOrigin {
|
impl ImmutableOrigin {
|
||||||
|
@ -160,7 +163,8 @@ impl MutableOrigin {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn effective_domain(&self) -> Option<Host> {
|
pub fn effective_domain(&self) -> Option<Host> {
|
||||||
self.immutable().host()
|
self.immutable()
|
||||||
|
.host()
|
||||||
.map(|host| self.domain().unwrap_or_else(|| host.clone()))
|
.map(|host| self.domain().unwrap_or_else(|| host.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue