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
11- use crate :: constants :: MAX_SESSION_ID_LENGTH ;
12- use crate :: subject_token_violation :: SubjectTokenViolation ;
8+ use trogon_nats :: SubjectTokenViolation ;
9+ use trogon_nats :: NatsToken ;
1310
1411/// Error returned when [`AcpSessionId`] validation fails.
1512#[ derive( Debug , Clone , PartialEq ) ]
@@ -36,32 +33,15 @@ impl std::error::Error for SessionIdError {}
3633/// Follows [NATS subject naming](https://docs.nats.io/nats-concepts/subjects#characters-allowed-and-recommended-for-subject-names):
3734/// ASCII only; rejects `.`, `*`, `>`, and whitespace. Max 128 characters.
3835#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
39- pub struct AcpSessionId ( std :: sync :: Arc < str > ) ;
36+ pub struct AcpSessionId ( NatsToken ) ;
4037
4138impl AcpSessionId {
4239 pub fn new ( s : impl AsRef < str > ) -> Result < Self , SessionIdError > {
43- let s = s. as_ref ( ) ;
44- if s. is_empty ( ) {
45- return Err ( SessionIdError ( SubjectTokenViolation :: Empty ) ) ;
46- }
47- let mut char_count = 0 ;
48- for ch in s. chars ( ) {
49- char_count += 1 ;
50- if char_count > MAX_SESSION_ID_LENGTH {
51- return Err ( SessionIdError ( SubjectTokenViolation :: TooLong ( char_count) ) ) ;
52- }
53- if !ch. is_ascii ( ) {
54- return Err ( SessionIdError ( SubjectTokenViolation :: InvalidCharacter ( ch) ) ) ;
55- }
56- if ch == '.' || ch == '*' || ch == '>' || ch. is_whitespace ( ) {
57- return Err ( SessionIdError ( SubjectTokenViolation :: InvalidCharacter ( ch) ) ) ;
58- }
59- }
60- Ok ( Self ( s. into ( ) ) )
40+ NatsToken :: new ( s) . map ( Self ) . map_err ( SessionIdError )
6141 }
6242
6343 pub fn as_str ( & self ) -> & str {
64- & self . 0
44+ self . 0 . as_str ( )
6545 }
6646}
6747
0 commit comments