use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{type_name}: {kind}")]
pub struct ValueError {
pub type_name: &'static str,
pub kind: ValueErrorKind,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValueErrorKind {
Length {
min: usize,
max: usize,
actual: usize,
},
Pattern {
expected: &'static str,
},
Char {
index: usize,
codepoint: u32,
},
Sequence {
max: u64,
},
XmlChar {
index: usize,
codepoint: u32,
},
}
impl fmt::Display for ValueErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Length { min, max, actual } if min == max => {
write!(f, "length must be {min}, is {actual}")
}
Self::Length { min, max, actual } => {
write!(f, "length must be {min}..={max}, is {actual}")
}
Self::Pattern { expected } => write!(f, "must match {expected}"),
Self::Char { index, codepoint } => {
write!(f, "character U+{codepoint:04X} at position {index} is outside the WBCI character set")
}
Self::Sequence { max } => write!(f, "sequence number must be at most {max}"),
Self::XmlChar { index, codepoint } => {
write!(f, "character U+{codepoint:04X} at position {index} is not permitted in XML text")
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Charset {
Wbci,
WbciMultiline,
Xml,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ws {
Preserve,
Replace,
Collapse,
}
#[must_use]
pub fn in_wbci_charset(c: char, multiline: bool) -> bool {
matches!(c,
'\u{09}'
| '\u{20}'..='\u{5F}'
| '\u{61}'..='\u{7B}'
| '\u{7D}' | '\u{7F}' | '\u{A7}'
| '\u{B0}'..='\u{B3}'
| '\u{C0}'..='\u{D6}'
| '\u{D8}'..='\u{F6}'
| '\u{F8}'..='\u{FF}')
|| (multiline && matches!(c, '\n' | '\r'))
}
fn is_xml_char(c: char) -> bool {
matches!(c, '\u{9}' | '\u{A}' | '\u{D}' | '\u{20}'..='\u{D7FF}' | '\u{E000}'..='\u{FFFD}' | '\u{10000}'..)
}
pub type Shape = (fn(&str) -> bool, &'static str);
#[derive(Debug, Clone, Copy)]
pub struct TextSpec {
pub name: &'static str,
pub min: usize,
pub max: usize,
pub charset: Charset,
pub ws: Ws,
pub pattern: Option<Shape>,
}
impl TextSpec {
#[must_use]
pub const fn xml(name: &'static str, min: usize, max: usize) -> Self {
Self { name, min, max, charset: Charset::Xml, ws: Ws::Preserve, pattern: None }
}
pub fn parse(&self, input: &str) -> Result<String, ValueError> {
let value = match self.ws {
Ws::Preserve => input.to_owned(),
Ws::Replace => input.replace(['\t', '\n', '\r'], " "),
Ws::Collapse => {
input.split([' ', '\t', '\n', '\r']).filter(|s| !s.is_empty()).collect::<Vec<_>>().join(" ")
}
};
let err = |kind| ValueError { type_name: self.name, kind };
for (index, c) in value.chars().enumerate() {
let codepoint = u32::from(c);
match self.charset {
Charset::Wbci | Charset::WbciMultiline => {
if !in_wbci_charset(c, self.charset == Charset::WbciMultiline) {
return Err(err(ValueErrorKind::Char { index, codepoint }));
}
}
Charset::Xml => {
if !(is_xml_char(c) && !c.is_control() || matches!(c, '\t' | '\n' | '\r')) {
return Err(err(ValueErrorKind::XmlChar { index, codepoint }));
}
}
}
}
let len = value.chars().count();
if len < self.min || len > self.max {
return Err(err(ValueErrorKind::Length { min: self.min, max: self.max, actual: len }));
}
if let Some((check, expected)) = self.pattern
&& !check(&value)
{
return Err(err(ValueErrorKind::Pattern { expected }));
}
Ok(value)
}
}
#[must_use]
pub fn digits(s: &str, n: usize, m: usize) -> bool {
(n..=m).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_digit())
}
#[must_use]
pub fn upper_alnum(s: &str, n: usize, m: usize) -> bool {
(n..=m).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_digit() || b.is_ascii_uppercase())
}
#[macro_export]
macro_rules! text_type {
(
$(#[$meta:meta])*
$name:ident, $spec:expr, $redact:tt
) => {
$(#[$meta])*
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct $name(String);
impl $name {
pub(crate) const SPEC: $crate::text::TextSpec = $spec;
pub fn new(value: impl AsRef<str>) -> Result<Self, $crate::ValueError> {
Self::SPEC.parse(value.as_ref()).map(Self)
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl core::str::FromStr for $name {
type Err = $crate::ValueError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl TryFrom<&str> for $name {
type Error = $crate::ValueError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::new(s)
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
&self.0
}
}
impl core::fmt::Display for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
$crate::text_type!(@debug $name, $redact);
};
(@debug $name:ident, redact) => {
impl core::fmt::Debug for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, concat!(stringify!($name), "(<redacted, {} chars>)"), self.0.chars().count())
}
}
};
(@debug $name:ident, plain) => {
impl core::fmt::Debug for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, concat!(stringify!($name), "({:?})"), self.0)
}
}
};
}
#[macro_export]
macro_rules! code_enum {
($(#[$meta:meta])* $name:ident { $($(#[$vmeta:meta])* $variant:ident = $code:literal),+ $(,)? }) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum $name {
$($(#[$vmeta])* $variant),+
}
impl $name {
pub const ALL: &'static [$name] = &[$(Self::$variant),+];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self { $(Self::$variant => $code),+ }
}
#[must_use]
pub fn from_code(s: &str) -> Option<Self> {
match s { $($code => Some(Self::$variant),)+ _ => None }
}
}
impl core::fmt::Display for $name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
};
}
#[cfg(test)]
mod tests {
use super::*;
const NAME: TextSpec =
TextSpec { name: "Vorname", min: 1, max: 30, charset: Charset::Wbci, ws: Ws::Collapse, pattern: None };
#[test]
fn collapse_then_check() {
assert_eq!(NAME.parse(" Anna \t Marie ").as_deref(), Ok("Anna Marie"));
}
#[test]
fn umlauts_and_symbols_are_in_the_charset() {
assert!(NAME.parse("Jürgen-Øle § °").is_ok());
}
#[test]
fn control_character_is_refused_with_position() {
let err = NAME.parse("An\u{7}na").unwrap_err();
assert_eq!(err.kind, ValueErrorKind::Char { index: 2, codepoint: 7 });
assert_eq!(err.to_string(), "Vorname: character U+0007 at position 2 is outside the WBCI character set");
}
#[test]
fn characters_outside_latin1_and_excluded_ascii_are_refused() {
for bad in ["€", "`", "|", "~", "Ł", "\u{202E}"] {
assert!(NAME.parse(bad).is_err(), "{bad:?}");
}
}
#[test]
fn multiline_admits_line_breaks() {
let spec = TextSpec { charset: Charset::WbciMultiline, ws: Ws::Preserve, ..NAME };
assert!(spec.parse("a\r\nb").is_ok());
assert!(NAME.parse("a\u{0}b").is_err());
}
#[test]
fn length_is_counted_in_characters() {
let thirty = "ä".repeat(30);
assert!(NAME.parse(&thirty).is_ok());
let err = NAME.parse(&"ä".repeat(31)).unwrap_err();
assert_eq!(err.kind, ValueErrorKind::Length { min: 1, max: 30, actual: 31 });
assert!(NAME.parse(" ").is_err());
}
#[test]
fn xml_charset_admits_any_text_but_controls() {
let spec = TextSpec::xml("Freitext", 1, 30);
assert!(spec.parse("€ | ~ Ł 漢字\n").is_ok());
let err = spec.parse("a\u{1}").unwrap_err();
assert_eq!(err.kind, ValueErrorKind::XmlChar { index: 1, codepoint: 1 });
assert_eq!(err.to_string(), "Freitext: character U+0001 at position 1 is not permitted in XML text");
assert!(spec.parse("\u{FFFE}").is_err());
}
}