ommui_string_patterns 0.1.2

String pattern checkers for OMMUI identifiers
Documentation
//! For (de-)serializing ommui id strings.
//!
//! This module can be used for (de-)serializing ommui id strings.
//! The ommui id strings are defined as text which starts with an
//! upper- or lowercase basic latin letters followed by any number
//! of further latin letters, numeric digits and underline characters.
//!
//! Examples of valid id strings are `"AnIdString"`, `"AnotherIdString"`,
//! `"anId2"`, `"anId2"`, `"anId2_"`, `"anId_2"` or `"an_Id2"`.

use idstring_maybe_empty;
use serde::de;
use serde::ser;

pub fn deserialize<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: de::Deserializer<'de>,
{
    let s = idstring_maybe_empty::deserialize::<'de, D>(deserializer)?;

    if s.is_empty() {
        Err(de::Error::custom("must not be empty"))
    } else {
        Ok(s)
    }
}

pub fn serialize<S>(s: &str, serializer: S) -> Result<S::Ok, S::Error>
where
    S: ser::Serializer,
{
    if !s.is_empty() {
        Ok(idstring_maybe_empty::serialize::<S>(
            s, serializer,
        )?)
    } else {
        Err(ser::Error::custom("must not be empty"))
    }
}