lib.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2016 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. //! This Rust crate implements IDNA
  9. //! [per the WHATWG URL Standard](https://url.spec.whatwg.org/#idna).
  10. //!
  11. //! It also exposes the underlying algorithms from [*Unicode IDNA Compatibility Processing*
  12. //! (Unicode Technical Standard #46)](http://www.unicode.org/reports/tr46/)
  13. //! and [Punycode (RFC 3492)](https://tools.ietf.org/html/rfc3492).
  14. //!
  15. //! Quoting from [UTS #46’s introduction](http://www.unicode.org/reports/tr46/#Introduction):
  16. //!
  17. //! > Initially, domain names were restricted to ASCII characters.
  18. //! > A system was introduced in 2003 for internationalized domain names (IDN).
  19. //! > This system is called Internationalizing Domain Names for Applications,
  20. //! > or IDNA2003 for short.
  21. //! > This mechanism supports IDNs by means of a client software transformation
  22. //! > into a format known as Punycode.
  23. //! > A revision of IDNA was approved in 2010 (IDNA2008).
  24. //! > This revision has a number of incompatibilities with IDNA2003.
  25. //! >
  26. //! > The incompatibilities force implementers of client software,
  27. //! > such as browsers and emailers,
  28. //! > to face difficult choices during the transition period
  29. //! > as registries shift from IDNA2003 to IDNA2008.
  30. //! > This document specifies a mechanism
  31. //! > that minimizes the impact of this transition for client software,
  32. //! > allowing client software to access domains that are valid under either system.
  33. #[macro_use]
  34. extern crate matches;
  35. extern crate unicode_bidi;
  36. extern crate unicode_normalization;
  37. pub mod punycode;
  38. mod uts46;
  39. pub use uts46::{Config, Errors};
  40. /// The [domain to ASCII](https://url.spec.whatwg.org/#concept-domain-to-ascii) algorithm.
  41. ///
  42. /// Return the ASCII representation a domain name,
  43. /// normalizing characters (upper-case to lower-case and other kinds of equivalence)
  44. /// and using Punycode as necessary.
  45. ///
  46. /// This process may fail.
  47. pub fn domain_to_ascii(domain: &str) -> Result<String, uts46::Errors> {
  48. uts46::Config::default().to_ascii(domain)
  49. }
  50. /// The [domain to Unicode](https://url.spec.whatwg.org/#concept-domain-to-unicode) algorithm.
  51. ///
  52. /// Return the Unicode representation of a domain name,
  53. /// normalizing characters (upper-case to lower-case and other kinds of equivalence)
  54. /// and decoding Punycode as necessary.
  55. ///
  56. /// This may indicate [syntax violations](https://url.spec.whatwg.org/#syntax-violation)
  57. /// but always returns a string for the mapped domain.
  58. pub fn domain_to_unicode(domain: &str) -> (String, Result<(), uts46::Errors>) {
  59. uts46::Config::default().to_unicode(domain)
  60. }