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