44//! Validation follows [NATS subject naming](https://docs.nats.io/nats-concepts/subjects#characters-allowed-and-recommended-for-subject-names):
55//! ASCII only (recommended), rejecting `.` `*` `>` and whitespace (forbidden). Validity is
66//! guaranteed at construction.
7- //!
8- //! TODO: Consider extracting to `trogon-nats` as a generic `NatsSubject` (or `NatsToken`) type
9- //! so prefix, session_id, and other subject tokens share the same validation.
107
8+ use crate :: nats_token_policies:: SingleTokenPolicy ;
119use crate :: subject_token_violation:: SubjectTokenViolation ;
12-
13- const MAX_SESSION_ID_LENGTH : usize = 128 ;
10+ use trogon_nats:: NatsToken ;
1411
1512/// Error returned when [`AcpSessionId`] validation fails.
1613#[ derive( Debug , Clone , PartialEq ) ]
@@ -37,32 +34,15 @@ impl std::error::Error for SessionIdError {}
3734/// Follows [NATS subject naming](https://docs.nats.io/nats-concepts/subjects#characters-allowed-and-recommended-for-subject-names):
3835/// ASCII only; rejects `.`, `*`, `>`, and whitespace. Max 128 characters.
3936#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
40- pub struct AcpSessionId ( std :: sync :: Arc < str > ) ;
37+ pub struct AcpSessionId ( NatsToken < SingleTokenPolicy > ) ;
4138
4239impl AcpSessionId {
4340 pub fn new ( s : impl AsRef < str > ) -> Result < Self , SessionIdError > {
44- let s = s. as_ref ( ) ;
45- if s. is_empty ( ) {
46- return Err ( SessionIdError ( SubjectTokenViolation :: Empty ) ) ;
47- }
48- let mut char_count = 0 ;
49- for ch in s. chars ( ) {
50- char_count += 1 ;
51- if char_count > MAX_SESSION_ID_LENGTH {
52- return Err ( SessionIdError ( SubjectTokenViolation :: TooLong ( char_count) ) ) ;
53- }
54- if !ch. is_ascii ( ) {
55- return Err ( SessionIdError ( SubjectTokenViolation :: InvalidCharacter ( ch) ) ) ;
56- }
57- if ch == '.' || ch == '*' || ch == '>' || ch. is_whitespace ( ) {
58- return Err ( SessionIdError ( SubjectTokenViolation :: InvalidCharacter ( ch) ) ) ;
59- }
60- }
61- Ok ( Self ( s. into ( ) ) )
41+ NatsToken :: new ( s) . map ( Self ) . map_err ( SessionIdError )
6242 }
6343
6444 pub fn as_str ( & self ) -> & str {
65- & self . 0
45+ self . 0 . as_str ( )
6646 }
6747}
6848
0 commit comments