icu: Make Stringprep private and add helper functions
This makes the API easier to use.
This commit is contained in:
parent
8d4ed296d0
commit
4bfd85556d
2 changed files with 35 additions and 22 deletions
|
|
@ -18,7 +18,7 @@ use crate::bindings::{
|
||||||
pub use crate::error::Error;
|
pub use crate::error::Error;
|
||||||
pub use crate::idna2008::Idna;
|
pub use crate::idna2008::Idna;
|
||||||
pub use crate::spoof::SpoofChecker;
|
pub use crate::spoof::SpoofChecker;
|
||||||
pub use crate::stringprep::Stringprep;
|
use crate::stringprep::Stringprep;
|
||||||
|
|
||||||
/// How unassigned codepoints should be handled.
|
/// How unassigned codepoints should be handled.
|
||||||
pub enum Strict {
|
pub enum Strict {
|
||||||
|
|
@ -31,25 +31,10 @@ pub enum Strict {
|
||||||
|
|
||||||
/// Main struct of this module, exposing the needed ICU functions to JID.
|
/// Main struct of this module, exposing the needed ICU functions to JID.
|
||||||
pub struct Icu {
|
pub struct Icu {
|
||||||
/// Perform stringprep using the Nameprep profile.
|
nameprep: Stringprep,
|
||||||
///
|
nodeprep: Stringprep,
|
||||||
/// See [RFC3491](https://tools.ietf.org/html/rfc3491).
|
resourceprep: Stringprep,
|
||||||
pub nameprep: Stringprep,
|
saslprep: 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,
|
|
||||||
|
|
||||||
/// IDNA2008 support.
|
/// IDNA2008 support.
|
||||||
///
|
///
|
||||||
|
|
@ -86,6 +71,34 @@ impl Icu {
|
||||||
spoofchecker,
|
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)]
|
#[cfg(test)]
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use crate::Strict;
|
||||||
use std::ptr::null_mut;
|
use std::ptr::null_mut;
|
||||||
|
|
||||||
/// Struct representing a given stringprep profile.
|
/// Struct representing a given stringprep profile.
|
||||||
pub struct Stringprep {
|
pub(crate) struct Stringprep {
|
||||||
inner: *mut UStringPrepProfile,
|
inner: *mut UStringPrepProfile,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,7 +30,7 @@ impl Stringprep {
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// Panics if ICU doesn’t return a valid UTF-16 string, which should never happen.
|
/// 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 {
|
if input.len() > 1023 {
|
||||||
return Err(Error::TooLong);
|
return Err(Error::TooLong);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue