Skip to content
Permalink
Browse files

Format component of url

  • Loading branch information...
chansuke committed Sep 5, 2018
1 parent 9070d4b commit 812d4a479ac4ea68e9e345c3e869987802446072
Showing with 25 additions and 15 deletions.
  1. +18 −12 components/url/lib.rs
  2. +7 −3 components/url/origin.rs
@@ -3,13 +3,15 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![deny(unsafe_code)]

#![crate_name = "servo_url"]
#![crate_type = "rlib"]

#[macro_use] extern crate malloc_size_of;
#[macro_use] extern crate malloc_size_of_derive;
#[macro_use] extern crate serde;
#[macro_use]
extern crate malloc_size_of;
#[macro_use]
extern crate malloc_size_of_derive;
#[macro_use]
extern crate serde;
extern crate servo_rand;
extern crate url;
extern crate url_serde;
@@ -29,22 +31,24 @@ use url::{Url, Position};
pub use url::Host;

#[derive(Clone, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd)]
pub struct ServoUrl(
#[ignore_malloc_size_of = "Arc"]
Arc<Url>
);
pub struct ServoUrl(#[ignore_malloc_size_of = "Arc"] Arc<Url>);

impl ServoUrl {
pub fn from_url(url: Url) -> Self {
ServoUrl(Arc::new(url))
}

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 {
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 {
@@ -209,15 +213,17 @@ impl From<Url> for ServoUrl {

impl serde::Serialize for ServoUrl {
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)
}
}

impl<'de> serde::Deserialize<'de> for ServoUrl {
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)
}
@@ -18,10 +18,13 @@ pub enum ImmutableOrigin {
/// Consists of the URL's scheme, host and port
Tuple(
String,
#[serde(deserialize_with = "url_serde::deserialize", serialize_with = "url_serde::serialize")]
#[serde(
deserialize_with = "url_serde::deserialize",
serialize_with = "url_serde::serialize"
)]
Host,
u16,
)
),
}

impl ImmutableOrigin {
@@ -160,7 +163,8 @@ impl MutableOrigin {
}

pub fn effective_domain(&self) -> Option<Host> {
self.immutable().host()
self.immutable()
.host()
.map(|host| self.domain().unwrap_or_else(|| host.clone()))
}
}

0 comments on commit 812d4a4

Please sign in to comment.
You can’t perform that action at this time.