From 4bfd85556d3a336201b5657260326407624adc98 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 20 Sep 2022 19:13:44 +0200 Subject: [PATCH] icu: Make Stringprep private and add helper functions This makes the API easier to use. --- icu/src/lib.rs | 53 +++++++++++++++++++++++++++---------------- icu/src/stringprep.rs | 4 ++-- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/icu/src/lib.rs b/icu/src/lib.rs index fa058b48..31afb5e0 100644 --- a/icu/src/lib.rs +++ b/icu/src/lib.rs @@ -18,7 +18,7 @@ use crate::bindings::{ pub use crate::error::Error; pub use crate::idna2008::Idna; pub use crate::spoof::SpoofChecker; -pub use crate::stringprep::Stringprep; +use crate::stringprep::Stringprep; /// How unassigned codepoints should be handled. pub enum Strict { @@ -31,25 +31,10 @@ pub enum Strict { /// Main struct of this module, exposing the needed ICU functions to JID. pub struct Icu { - /// Perform stringprep using the Nameprep profile. - /// - /// See [RFC3491](https://tools.ietf.org/html/rfc3491). - pub nameprep: Stringprep, - - /// Perform stringprep using the Nodeprep profile. - /// - /// See [RFC6122 appendix A](https://tools.ietf.org/html/rfc6122#appendix-A). - pub nodeprep: Stringprep, - - /// Perform stringprep using the Resourceprep profile. - /// - /// See [RFC6122 appendix A](https://tools.ietf.org/html/rfc6122#appendix-A). - pub resourceprep: Stringprep, - - /// Perform stringprep using the Saslprep profile. - /// - /// See [RFC4013](https://tools.ietf.org/html/rfc4013). - pub saslprep: Stringprep, + nameprep: Stringprep, + nodeprep: Stringprep, + resourceprep: Stringprep, + saslprep: Stringprep, /// IDNA2008 support. /// @@ -86,6 +71,34 @@ impl Icu { spoofchecker, }) } + + /// Perform stringprep using the Nameprep profile. + /// + /// See [RFC3491](https://tools.ietf.org/html/rfc3491). + pub fn nameprep(&self, string: &str, strict: Strict) -> Result { + self.nameprep.stringprep(string, strict) + } + + /// Perform stringprep using the Nodeprep profile. + /// + /// See [RFC6122 appendix A](https://tools.ietf.org/html/rfc6122#appendix-A). + pub fn nodeprep(&self, string: &str, strict: Strict) -> Result { + self.nodeprep.stringprep(string, strict) + } + + /// Perform stringprep using the Resourceprep profile. + /// + /// See [RFC6122 appendix A](https://tools.ietf.org/html/rfc6122#appendix-A). + pub fn resourceprep(&self, string: &str, strict: Strict) -> Result { + self.resourceprep.stringprep(string, strict) + } + + /// Perform stringprep using the Saslprep profile. + /// + /// See [RFC4013](https://tools.ietf.org/html/rfc4013). + pub fn saslprep(&self, string: &str, strict: Strict) -> Result { + self.saslprep.stringprep(string, strict) + } } #[cfg(test)] diff --git a/icu/src/stringprep.rs b/icu/src/stringprep.rs index c4436d76..ee6b150b 100644 --- a/icu/src/stringprep.rs +++ b/icu/src/stringprep.rs @@ -11,7 +11,7 @@ use crate::Strict; use std::ptr::null_mut; /// Struct representing a given stringprep profile. -pub struct Stringprep { +pub(crate) struct Stringprep { inner: *mut UStringPrepProfile, } @@ -30,7 +30,7 @@ impl Stringprep { /// /// # Panics /// Panics if ICU doesn’t return a valid UTF-16 string, which should never happen. - pub fn stringprep(&self, input: &str, strict: Strict) -> Result { + pub(crate) fn stringprep(&self, input: &str, strict: Strict) -> Result { if input.len() > 1023 { return Err(Error::TooLong); }