Skip to content

Commit b2b70f5

Browse files
committed
Merge commit 'refs/pull/484/head' of github.com:servo/rust-url into 2.0
2 parents da2b1af + 38cfea4 commit b2b70f5

File tree

4 files changed

+19
-56
lines changed

4 files changed

+19
-56
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ serde_json = "1.0"
3737
bencher = "0.1"
3838

3939
[dependencies]
40-
idna = { version = "0.1.0", path = "./idna" }
40+
idna = { version = "0.2.0", path = "./idna" }
4141
matches = "0.1"
4242
percent-encoding = { version = "1.0.0", path = "./percent_encoding" }
4343
serde = {version = "1.0", optional = true}

idna/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "idna"
3-
version = "0.1.6"
3+
version = "0.2.0"
44
authors = ["The rust-url developers"]
55
description = "IDNA (Internationalizing Domain Names in Applications) and Punycode."
66
repository = "https://github.com/servo/rust-url/"

idna/src/lib.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ pub mod uts46;
4848
///
4949
/// This process may fail.
5050
pub fn domain_to_ascii(domain: &str) -> Result<String, uts46::Errors> {
51-
let flags = uts46::Flags {
52-
use_std3_ascii_rules: false,
53-
transitional_processing: false,
54-
verify_dns_length: false,
55-
};
56-
uts46::Config::from(flags).check_hyphens(false).to_ascii(domain)
51+
uts46::Config::default().to_ascii(domain)
5752
}
5853

5954
/// The [domain to Unicode](https://url.spec.whatwg.org/#concept-domain-to-unicode) algorithm.
@@ -65,12 +60,5 @@ pub fn domain_to_ascii(domain: &str) -> Result<String, uts46::Errors> {
6560
/// This may indicate [syntax violations](https://url.spec.whatwg.org/#syntax-violation)
6661
/// but always returns a string for the mapped domain.
6762
pub fn domain_to_unicode(domain: &str) -> (String, Result<(), uts46::Errors>) {
68-
let flags = uts46::Flags {
69-
use_std3_ascii_rules: false,
70-
71-
// Unused:
72-
transitional_processing: false,
73-
verify_dns_length: false,
74-
};
75-
uts46::Config::from(flags).check_hyphens(false).to_unicode(domain)
63+
uts46::Config::default().to_unicode(domain)
7664
}

idna/src/uts46.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ fn find_char(codepoint: char) -> &'static Mapping {
8181
.unwrap()
8282
}
8383

84-
fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec<Error>) {
84+
fn map_char(codepoint: char, config: Config, output: &mut String, errors: &mut Vec<Error>) {
8585
match *find_char(codepoint) {
8686
Mapping::Valid => output.push(codepoint),
8787
Mapping::Ignored => {}
8888
Mapping::Mapped(ref slice) => output.push_str(decode_slice(slice)),
8989
Mapping::Deviation(ref slice) => {
90-
if flags.transitional_processing {
90+
if config.transitional_processing {
9191
output.push_str(decode_slice(slice))
9292
} else {
9393
output.push(codepoint)
@@ -98,13 +98,13 @@ fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec
9898
output.push(codepoint);
9999
}
100100
Mapping::DisallowedStd3Valid => {
101-
if flags.use_std3_ascii_rules {
101+
if config.use_std3_ascii_rules {
102102
errors.push(Error::DissallowedByStd3AsciiRules);
103103
}
104104
output.push(codepoint)
105105
}
106106
Mapping::DisallowedStd3Mapped(ref slice) => {
107-
if flags.use_std3_ascii_rules {
107+
if config.use_std3_ascii_rules {
108108
errors.push(Error::DissallowedMappedInStd3);
109109
}
110110
output.push_str(decode_slice(slice))
@@ -293,8 +293,8 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
293293
// V6: Check against Mapping Table
294294
else if label.chars().any(|c| match *find_char(c) {
295295
Mapping::Valid => false,
296-
Mapping::Deviation(_) => config.flags.transitional_processing,
297-
Mapping::DisallowedStd3Valid => config.flags.use_std3_ascii_rules,
296+
Mapping::Deviation(_) => config.transitional_processing,
297+
Mapping::DisallowedStd3Valid => config.use_std3_ascii_rules,
298298
_ => true,
299299
}) {
300300
errors.push(Error::ValidityCriteria);
@@ -315,7 +315,7 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
315315
fn processing(domain: &str, config: Config, errors: &mut Vec<Error>) -> String {
316316
let mut mapped = String::with_capacity(domain.len());
317317
for c in domain.chars() {
318-
map_char(c, config.flags, &mut mapped, errors)
318+
map_char(c, config, &mut mapped, errors)
319319
}
320320
let mut normalized = String::with_capacity(mapped.len());
321321
normalized.extend(mapped.nfc());
@@ -371,35 +371,30 @@ fn processing(domain: &str, config: Config, errors: &mut Vec<Error>) -> String {
371371
validated
372372
}
373373

374-
#[derive(Clone, Copy)]
374+
#[derive(Clone, Copy, Default)]
375375
pub struct Config {
376-
flags: Flags,
376+
use_std3_ascii_rules: bool,
377+
transitional_processing: bool,
378+
verify_dns_length: bool,
377379
check_hyphens: bool,
378380
}
379381

380-
impl From<Flags> for Config {
381-
#[inline]
382-
fn from(flags: Flags) -> Self {
383-
Self { flags, check_hyphens: true }
384-
}
385-
}
386-
387382
impl Config {
388383
#[inline]
389384
pub fn use_std3_ascii_rules(mut self, value: bool) -> Self {
390-
self.flags.use_std3_ascii_rules = value;
385+
self.use_std3_ascii_rules = value;
391386
self
392387
}
393388

394389
#[inline]
395390
pub fn transitional_processing(mut self, value: bool) -> Self {
396-
self.flags.transitional_processing = value;
391+
self.transitional_processing = value;
397392
self
398393
}
399394

400395
#[inline]
401396
pub fn verify_dns_length(mut self, value: bool) -> Self {
402-
self.flags.verify_dns_length = value;
397+
self.verify_dns_length = value;
403398
self
404399
}
405400

@@ -432,7 +427,7 @@ impl Config {
432427
}
433428
}
434429

435-
if self.flags.verify_dns_length {
430+
if self.verify_dns_length {
436431
let domain = if result.ends_with(".") { &result[..result.len()-1] } else { &*result };
437432
if domain.len() < 1 || domain.split('.').any(|label| label.len() < 1) {
438433
errors.push(Error::TooShortForDns)
@@ -462,13 +457,6 @@ impl Config {
462457

463458
}
464459

465-
#[derive(Copy, Clone)]
466-
pub struct Flags {
467-
pub use_std3_ascii_rules: bool,
468-
pub transitional_processing: bool,
469-
pub verify_dns_length: bool,
470-
}
471-
472460
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
473461
enum Error {
474462
PunycodeError,
@@ -486,16 +474,3 @@ enum Error {
486474
/// More details may be exposed in the future.
487475
#[derive(Debug)]
488476
pub struct Errors(Vec<Error>);
489-
490-
/// http://www.unicode.org/reports/tr46/#ToASCII
491-
pub fn to_ascii(domain: &str, flags: Flags) -> Result<String, Errors> {
492-
Config::from(flags).to_ascii(domain)
493-
}
494-
495-
/// http://www.unicode.org/reports/tr46/#ToUnicode
496-
///
497-
/// Only `use_std3_ascii_rules` is used in `flags`.
498-
pub fn to_unicode(domain: &str, mut flags: Flags) -> (String, Result<(), Errors>) {
499-
flags.transitional_processing = false;
500-
Config::from(flags).to_unicode(domain)
501-
}

0 commit comments

Comments
 (0)