#![doc(html_root_url="https://docs.rs/stringprep/0.1.2")]
#![warn(missing_docs)]
extern crate unicode_bidi;
extern crate unicode_normalization;
use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::error;
use std::fmt;
use unicode_normalization::UnicodeNormalization;
mod rfc3454;
pub mod tables;
#[derive(Debug)]
enum ErrorCause {
ProhibitedCharacter(char),
ProhibitedBidirectionalText,
}
#[derive(Debug)]
pub struct Error(ErrorCause);
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
ErrorCause::ProhibitedCharacter(c) => write!(fmt, "prohibited character `{}`", c),
ErrorCause::ProhibitedBidirectionalText => write!(fmt, "prohibited bidirectional text"),
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
"error performing stringprep algorithm"
}
}
pub fn saslprep<'a>(s: &'a str) -> Result<Cow<'a, str>, Error> {
if s.chars()
.all(|c| c.is_ascii() && !tables::ascii_control_character(c)) {
return Ok(Cow::Borrowed(s));
}
let mapped = s.chars()
.map(|c| if tables::non_ascii_space_character(c) {
' '
} else {
c
})
.filter(|&c| !tables::commonly_mapped_to_nothing(c));
let normalized = mapped.nfkc().collect::<String>();
let prohibited = normalized
.chars()
.find(|&c| {
tables::non_ascii_space_character(c) ||
tables::ascii_control_character(c) ||
tables::non_ascii_control_character(c) ||
tables::private_use(c) ||
tables::non_character_code_point(c) ||
tables::surrogate_code(c) ||
tables::inappropriate_for_plain_text(c) ||
tables::inappropriate_for_canonical_representation(c) ||
tables::change_display_properties_or_deprecated(c) ||
tables::tagging_character(c) });
if let Some(c) = prohibited {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
if is_prohibited_bidirectional_text(&normalized) {
return Err(Error(ErrorCause::ProhibitedBidirectionalText));
}
let unassigned = normalized
.chars()
.find(|&c| tables::unassigned_code_point(c));
if let Some(c) = unassigned {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
Ok(Cow::Owned(normalized))
}
fn is_prohibited_bidirectional_text(s: &str) -> bool {
if s.contains(tables::bidi_r_or_al) {
if s.contains(tables::bidi_l) {
return true;
}
if !tables::bidi_r_or_al(s.chars().next().unwrap()) ||
!tables::bidi_r_or_al(s.chars().next_back().unwrap()) {
return true;
}
}
false
}
pub fn nameprep<'a>(s: &'a str) -> Result<Cow<'a, str>, Error> {
let mapped = s.chars()
.filter(|&c| !tables::commonly_mapped_to_nothing(c))
.flat_map(tables::case_fold_for_nfkc);
let normalized = mapped.nfkc().collect::<String>();
let prohibited = normalized
.chars()
.find(|&c| {
tables::non_ascii_space_character(c) ||
tables::non_ascii_control_character(c) ||
tables::private_use(c) ||
tables::non_character_code_point(c) ||
tables::surrogate_code(c) ||
tables::inappropriate_for_plain_text(c) ||
tables::inappropriate_for_canonical_representation(c) ||
tables::change_display_properties_or_deprecated(c) ||
tables::tagging_character(c) });
if let Some(c) = prohibited {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
if is_prohibited_bidirectional_text(&normalized) {
return Err(Error(ErrorCause::ProhibitedBidirectionalText));
}
let unassigned = normalized
.chars()
.find(|&c| tables::unassigned_code_point(c));
if let Some(c) = unassigned {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
Ok(Cow::Owned(normalized))
}
pub fn nodeprep<'a>(s: &'a str) -> Result<Cow<'a, str>, Error> {
let mapped = s.chars()
.filter(|&c| !tables::commonly_mapped_to_nothing(c))
.flat_map(tables::case_fold_for_nfkc);
let normalized = mapped.nfkc().collect::<String>();
let prohibited = normalized
.chars()
.find(|&c| {
tables::ascii_space_character(c) ||
tables::non_ascii_space_character(c) ||
tables::ascii_control_character(c) ||
tables::non_ascii_control_character(c) ||
tables::private_use(c) ||
tables::non_character_code_point(c) ||
tables::surrogate_code(c) ||
tables::inappropriate_for_plain_text(c) ||
tables::inappropriate_for_canonical_representation(c) ||
tables::change_display_properties_or_deprecated(c) ||
tables::tagging_character(c) ||
prohibited_node_character(c)
});
if let Some(c) = prohibited {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
if is_prohibited_bidirectional_text(&normalized) {
return Err(Error(ErrorCause::ProhibitedBidirectionalText));
}
let unassigned = normalized
.chars()
.find(|&c| tables::unassigned_code_point(c));
if let Some(c) = unassigned {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
Ok(Cow::Owned(normalized))
}
fn prohibited_node_character(c: char) -> bool {
match c {
'"' | '&' | '\'' | '/' | ':' | '<' | '>' | '@' => true,
_ => false
}
}
pub fn resourceprep<'a>(s: &'a str) -> Result<Cow<'a, str>, Error> {
let mapped = s.chars()
.filter(|&c| !tables::commonly_mapped_to_nothing(c))
.collect::<String>();
let normalized = mapped.nfkc().collect::<String>();
let prohibited = normalized
.chars()
.find(|&c| {
tables::non_ascii_space_character(c) ||
tables::ascii_control_character(c) ||
tables::non_ascii_control_character(c) ||
tables::private_use(c) ||
tables::non_character_code_point(c) ||
tables::surrogate_code(c) ||
tables::inappropriate_for_plain_text(c) ||
tables::inappropriate_for_canonical_representation(c) ||
tables::change_display_properties_or_deprecated(c) ||
tables::tagging_character(c) });
if let Some(c) = prohibited {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
if is_prohibited_bidirectional_text(&normalized) {
return Err(Error(ErrorCause::ProhibitedBidirectionalText));
}
let unassigned = normalized
.chars()
.find(|&c| tables::unassigned_code_point(c));
if let Some(c) = unassigned {
return Err(Error(ErrorCause::ProhibitedCharacter(c)));
}
Ok(Cow::Owned(normalized))
}
#[cfg(test)]
mod test {
use super::*;
fn assert_prohibited_character<T>(result: Result<T, Error>) {
match result {
Err(Error(ErrorCause::ProhibitedCharacter(_))) => (),
_ => assert!(false)
}
}
#[test]
fn saslprep_examples() {
assert_prohibited_character(saslprep("\u{0007}"));
}
#[test]
fn nodeprep_examples() {
assert_prohibited_character(nodeprep(" "));
assert_prohibited_character(nodeprep("\u{00a0}"));
assert_prohibited_character(nodeprep("foo@bar"));
}
#[test]
fn resourceprep_examples() {
assert_eq!("foo@bar", resourceprep("foo@bar").unwrap());
}
}