icu: Make Stringprep private and add helper functions

Emmanuel Gil Peyrot created

This makes the API easier to use.

Change summary

icu/src/lib.rs        | 53 ++++++++++++++++++++++++++++----------------
icu/src/stringprep.rs |  4 +-
2 files changed, 35 insertions(+), 22 deletions(-)

Detailed changes

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<String, Error> {
+        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<String, Error> {
+        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<String, Error> {
+        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<String, Error> {
+        self.saslprep.stringprep(string, strict)
+    }
 }
 
 #[cfg(test)]

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<String, Error> {
+    pub(crate) fn stringprep(&self, input: &str, strict: Strict) -> Result<String, Error> {
         if input.len() > 1023 {
             return Err(Error::TooLong);
         }