@@ -83,13 +83,13 @@ fn find_char(codepoint: char) -> &'static Mapping {
83
83
} ) . unwrap ( )
84
84
}
85
85
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 > ) {
87
87
match * find_char ( codepoint) {
88
88
Mapping :: Valid => output. push ( codepoint) ,
89
89
Mapping :: Ignored => { } ,
90
90
Mapping :: Mapped ( ref slice) => output. push_str ( decode_slice ( slice) ) ,
91
91
Mapping :: Deviation ( ref slice) => {
92
- if flags . transitional_processing {
92
+ if config . transitional_processing {
93
93
output. push_str ( decode_slice ( slice) )
94
94
} else {
95
95
output. push ( codepoint)
@@ -100,13 +100,13 @@ fn map_char(codepoint: char, flags: Flags, output: &mut String, errors: &mut Vec
100
100
output. push ( codepoint) ;
101
101
}
102
102
Mapping :: DisallowedStd3Valid => {
103
- if flags . use_std3_ascii_rules {
103
+ if config . use_std3_ascii_rules {
104
104
errors. push ( Error :: DissallowedByStd3AsciiRules ) ;
105
105
}
106
106
output. push ( codepoint)
107
107
}
108
108
Mapping :: DisallowedStd3Mapped ( ref slice) => {
109
- if flags . use_std3_ascii_rules {
109
+ if config . use_std3_ascii_rules {
110
110
errors. push ( Error :: DissallowedMappedInStd3 ) ;
111
111
}
112
112
output. push_str ( decode_slice ( slice) )
@@ -271,8 +271,8 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
271
271
// V6: Check against Mapping Table
272
272
else if label. chars ( ) . any ( |c| match * find_char ( c) {
273
273
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 ,
276
276
_ => true ,
277
277
} ) {
278
278
errors. push ( Error :: ValidityCriteria ) ;
@@ -295,7 +295,7 @@ fn validate(label: &str, is_bidi_domain: bool, config: Config, errors: &mut Vec<
295
295
fn processing ( domain : & str , config : Config , errors : & mut Vec < Error > ) -> String {
296
296
let mut mapped = String :: with_capacity ( domain. len ( ) ) ;
297
297
for c in domain. chars ( ) {
298
- map_char ( c, config. flags , & mut mapped, errors)
298
+ map_char ( c, config, & mut mapped, errors)
299
299
}
300
300
let mut normalized = String :: with_capacity ( mapped. len ( ) ) ;
301
301
normalized. extend ( mapped. nfc ( ) ) ;
@@ -351,35 +351,30 @@ fn processing(domain: &str, config: Config, errors: &mut Vec<Error>) -> String {
351
351
validated
352
352
}
353
353
354
- #[ derive( Clone , Copy ) ]
354
+ #[ derive( Clone , Copy , Default ) ]
355
355
pub struct Config {
356
- flags : Flags ,
356
+ use_std3_ascii_rules : bool ,
357
+ transitional_processing : bool ,
358
+ verify_dns_length : bool ,
357
359
check_hyphens : bool ,
358
360
}
359
361
360
- impl From < Flags > for Config {
361
- #[ inline]
362
- fn from ( flags : Flags ) -> Self {
363
- Self { flags, check_hyphens : true }
364
- }
365
- }
366
-
367
362
impl Config {
368
363
#[ inline]
369
364
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;
371
366
self
372
367
}
373
368
374
369
#[ inline]
375
370
pub fn transitional_processing ( mut self , value : bool ) -> Self {
376
- self . flags . transitional_processing = value;
371
+ self . transitional_processing = value;
377
372
self
378
373
}
379
374
380
375
#[ inline]
381
376
pub fn verify_dns_length ( mut self , value : bool ) -> Self {
382
- self . flags . verify_dns_length = value;
377
+ self . verify_dns_length = value;
383
378
self
384
379
}
385
380
@@ -412,7 +407,7 @@ impl Config {
412
407
}
413
408
}
414
409
415
- if self . flags . verify_dns_length {
410
+ if self . verify_dns_length {
416
411
let domain = if result. ends_with ( "." ) { & result[ ..result. len ( ) -1 ] } else { & * result } ;
417
412
if domain. len ( ) < 1 || domain. split ( '.' ) . any ( |label| label. len ( ) < 1 ) {
418
413
errors. push ( Error :: TooShortForDns )
@@ -442,13 +437,6 @@ impl Config {
442
437
443
438
}
444
439
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
-
452
440
#[ derive( PartialEq , Eq , Clone , Copy , Debug ) ]
453
441
enum Error {
454
442
PunycodeError ,
@@ -466,16 +454,3 @@ enum Error {
466
454
/// More details may be exposed in the future.
467
455
#[ derive( Debug ) ]
468
456
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