Skip to content

Commit 449c10a

Browse files
committed
Kill idna::uts46::Flags
1 parent fb3b957 commit 449c10a

File tree

2 files changed

+17
-54
lines changed

2 files changed

+17
-54
lines changed

idna/src/lib.rs

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

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

idna/src/uts46.rs

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

86-
fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec<Error>) {
86+
fn map_char(codepoint: char, config: Config, output: &mut String, errors: &mut Vec<Error>) {
8787
match *find_char(codepoint) {
8888
Mapping::Valid => output.push(codepoint),
8989
Mapping::Ignored => {},
9090
Mapping::Mapped(ref slice) => output.push_str(decode_slice(slice)),
9191
Mapping::Deviation(ref slice) => {
92-
if flags.transitional_processing {
92+
if config.transitional_processing {
9393
output.push_str(decode_slice(slice))
9494
} else {
9595
output.push(codepoint)
@@ -100,13 +100,13 @@ fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec
100100
output.push(codepoint);
101101
}
102102
Mapping::DisallowedStd3Valid => {
103-
if flags.use_std3_ascii_rules {
103+
if config.use_std3_ascii_rules {
104104
errors.push(Error::DissallowedByStd3AsciiRules);
105105
}
106106
output.push(codepoint)
107107
}
108108
Mapping::DisallowedStd3Mapped(ref slice) => {
109-
if flags.use_std3_ascii_rules {
109+
if config.use_std3_ascii_rules {
110110
errors.push(Error::DissallowedMappedInStd3);
111111
}
112112
output.push_str(decode_slice(slice))
@@ -271,8 +271,8 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
271271
// V6: Check against Mapping Table
272272
else if label.chars().any(|c| match *find_char(c) {
273273
Mapping::Valid => false,
274-
Mapping::Deviation(_) => config.flags.transitional_processing,
275-
Mapping::DisallowedStd3Valid => config.flags.use_std3_ascii_rules,
274+
Mapping::Deviation(_) => config.transitional_processing,
275+
Mapping::DisallowedStd3Valid => config.use_std3_ascii_rules,
276276
_ => true,
277277
}) {
278278
errors.push(Error::ValidityCriteria);
@@ -295,7 +295,7 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
295295
fn processing(domain: &str, config: Config, errors: &mut Vec<Error>) -> String {
296296
let mut mapped = String::with_capacity(domain.len());
297297
for c in domain.chars() {
298-
map_char(c, config.flags, &mut mapped, errors)
298+
map_char(c, config, &mut mapped, errors)
299299
}
300300
let mut normalized = String::with_capacity(mapped.len());
301301
normalized.extend(mapped.nfc());
@@ -351,35 +351,30 @@ fn processing(domain: &str, config: Config, errors: &mut Vec<Error>) -> String {
351351
validated
352352
}
353353

354-
#[derive(Clone, Copy)]
354+
#[derive(Clone, Copy, Default)]
355355
pub struct Config {
356-
flags: Flags,
356+
use_std3_ascii_rules: bool,
357+
transitional_processing: bool,
358+
verify_dns_length: bool,
357359
check_hyphens: bool,
358360
}
359361

360-
impl From<Flags> for Config {
361-
#[inline]
362-
fn from(flags: Flags) -> Self {
363-
Self { flags, check_hyphens: true }
364-
}
365-
}
366-
367362
impl Config {
368363
#[inline]
369364
pub fn use_std3_ascii_rules(mut self, value: bool) -> Self {
370-
self.flags.use_std3_ascii_rules = value;
365+
self.use_std3_ascii_rules = value;
371366
self
372367
}
373368

374369
#[inline]
375370
pub fn transitional_processing(mut self, value: bool) -> Self {
376-
self.flags.transitional_processing = value;
371+
self.transitional_processing = value;
377372
self
378373
}
379374

380375
#[inline]
381376
pub fn verify_dns_length(mut self, value: bool) -> Self {
382-
self.flags.verify_dns_length = value;
377+
self.verify_dns_length = value;
383378
self
384379
}
385380

@@ -412,7 +407,7 @@ impl Config {
412407
}
413408
}
414409

415-
if self.flags.verify_dns_length {
410+
if self.verify_dns_length {
416411
let domain = if result.ends_with(".") { &result[..result.len()-1] } else { &*result };
417412
if domain.len() < 1 || domain.split('.').any(|label| label.len() < 1) {
418413
errors.push(Error::TooShortForDns)
@@ -442,13 +437,6 @@ impl Config {
442437

443438
}
444439

445-
#[derive(Copy, Clone)]
446-
pub struct Flags {
447-
pub use_std3_ascii_rules: bool,
448-
pub transitional_processing: bool,
449-
pub verify_dns_length: bool,
450-
}
451-
452440
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
453441
enum Error {
454442
PunycodeError,
@@ -466,16 +454,3 @@ enum Error {
466454
/// More details may be exposed in the future.
467455
#[derive(Debug)]
468456
pub struct Errors(Vec<Error>);
469-
470-
/// http://www.unicode.org/reports/tr46/#ToASCII
471-
pub fn to_ascii(domain: &str, flags: Flags) -> Result<String, Errors> {
472-
Config::from(flags).to_ascii(domain)
473-
}
474-
475-
/// http://www.unicode.org/reports/tr46/#ToUnicode
476-
///
477-
/// Only `use_std3_ascii_rules` is used in `flags`.
478-
pub fn to_unicode(domain: &str, mut flags: Flags) -> (String, Result<(), Errors>) {
479-
flags.transitional_processing = false;
480-
Config::from(flags).to_unicode(domain)
481-
}

0 commit comments

Comments
 (0)