icu: Make Stringprep private and add helper functions

This makes the API easier to use.
This commit is contained in:
Emmanuel Gil Peyrot 2022-09-20 19:13:44 +02:00
parent 8d4ed296d0
commit 4bfd85556d
2 changed files with 35 additions and 22 deletions

View file

@ -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)]

View file

@ -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 doesnt 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);
}