strid_examples/
sso_wrapper.rs1#![allow(dead_code)]
12
13#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
14use std::{borrow::Cow, error, fmt};
15
16#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
17use smartstring::alias::String;
18#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
19use strid::braid;
20
21#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
30#[braid(serde, ref_doc = "A borrowed reference to a string slice wrapper")]
31pub struct SmartUsernameBuf;
32
33#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
39#[braid(serde, no_expose)]
40pub struct CompactData(compact_str::CompactString);
41
42#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
56#[braid(
57 serde,
58 no_expose,
59 normalizer,
60 ref_doc = "A borrowed reference to a non-empty, lowercase string"
61)]
62pub struct LowerCompactString(compact_str::CompactString);
63
64#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
65impl strid::Validator for LowerCompactString {
66 type Error = InvalidString;
67
68 fn validate(raw: &str) -> Result<(), Self::Error> {
69 if raw.is_empty() {
70 Err(InvalidString::EmptyString)
71 } else if raw.chars().any(char::is_uppercase) {
72 Err(InvalidString::InvalidCharacter)
73 } else {
74 Ok(())
75 }
76 }
77}
78
79#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
80impl strid::Normalizer for LowerCompactString {
81 fn normalize(s: &str) -> Result<Cow<str>, Self::Error> {
82 if s.is_empty() {
83 Err(InvalidString::EmptyString)
84 } else if s.contains(char::is_uppercase) {
85 Ok(Cow::Owned(s.to_lowercase()))
86 } else {
87 Ok(Cow::Borrowed(s))
88 }
89 }
90}
91
92#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
94#[derive(Debug)]
95pub enum InvalidString {
96 EmptyString,
97 InvalidCharacter,
98}
99
100#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
101impl fmt::Display for InvalidString {
102 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103 match self {
104 Self::EmptyString => f.write_str("string cannot be empty"),
105 Self::InvalidCharacter => f.write_str("string contains invalid uppercase character"),
106 }
107 }
108}
109
110#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
111impl error::Error for InvalidString {}
112
113#[cfg(all(feature = "smartstring-facet", feature = "compact_str-facet"))]
114strid::from_infallible!(InvalidString);