@@ -2,8 +2,8 @@ use core::fmt;
22use std:: borrow:: Cow ;
33
44use pgrx:: prelude:: * ;
5+ use serde:: { Deserialize , Serialize } ;
56use uuid:: Uuid ;
6- use serde:: { Serialize , Deserialize } ;
77
88use crate :: base32:: { decode_base32_uuid, encode_base32_uuid} ;
99
@@ -36,7 +36,7 @@ impl TypeIDPrefix {
3636 }
3737
3838 pub fn try_unsafe ( tag : & str ) -> Self {
39- Self ( tag. to_string ( ) )
39+ Self ( tag. to_string ( ) )
4040 }
4141
4242 fn try_from_type_prefix ( tag : & str ) -> Result < Self , Cow < ' static , str > > {
@@ -77,58 +77,62 @@ impl TypeIDPrefix {
7777pub struct TypeID ( TypeIDPrefix , Uuid ) ;
7878
7979impl TypeID {
80- pub fn new ( type_prefix : TypeIDPrefix , uuid : Uuid ) -> Self {
81- TypeID ( type_prefix, uuid)
82- }
83-
84- pub fn from_string ( id : & str ) -> Result < Self , Error > {
85- // Split the input string once at the first occurrence of '_'
86- let ( tag, id) = match id. rsplit_once ( '_' ) {
87- Some ( ( "" , _) ) => return Err ( Error :: InvalidType ) ,
88- Some ( ( tag, id) ) => ( tag, id) ,
89- None => ( "" , id) ,
90- } ;
91-
92- // Decode the UUID part and handle potential errors
93- let uuid = decode_base32_uuid ( id) . map_err ( |_| Error :: InvalidData ) ?;
94-
95- let prefix = TypeIDPrefix :: new ( tag) ?;
96-
97- // Create and return the TypeID
98- Ok ( TypeID ( prefix, uuid) )
99- }
100-
101- pub fn type_prefix ( & self ) -> & str {
102- & self . 0 . to_type_prefix ( )
103- }
104-
105- pub fn uuid ( & self ) -> & Uuid {
106- & self . 1
107- }
80+ pub fn new ( type_prefix : TypeIDPrefix , uuid : Uuid ) -> Self {
81+ TypeID ( type_prefix, uuid)
82+ }
83+
84+ pub fn from_string ( id : & str ) -> Result < Self , Error > {
85+ // Split the input string once at the first occurrence of '_'
86+ let ( tag, id) = match id. rsplit_once ( '_' ) {
87+ Some ( ( "" , _) ) => return Err ( Error :: InvalidType ) ,
88+ Some ( ( tag, id) ) => ( tag, id) ,
89+ None => ( "" , id) ,
90+ } ;
91+
92+ // Decode the UUID part and handle potential errors
93+ let uuid = decode_base32_uuid ( id) . map_err ( |_| Error :: InvalidData ) ?;
94+
95+ let prefix = TypeIDPrefix :: new ( tag) ?;
96+
97+ // Create and return the TypeID
98+ Ok ( TypeID ( prefix, uuid) )
99+ }
100+
101+ pub fn type_prefix ( & self ) -> & str {
102+ self . 0 . to_type_prefix ( )
103+ }
104+
105+ pub fn uuid ( & self ) -> & Uuid {
106+ & self . 1
107+ }
108108}
109109
110110impl fmt:: Display for TypeID {
111- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
112- if self . type_prefix ( ) . is_empty ( ) {
113- write ! ( f, "{}" , encode_base32_uuid( self . uuid( ) ) )
114- } else {
115- write ! ( f, "{}_{}" , self . type_prefix( ) , encode_base32_uuid( self . uuid( ) ) )
116- }
117- }
111+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
112+ if self . type_prefix ( ) . is_empty ( ) {
113+ write ! ( f, "{}" , encode_base32_uuid( self . uuid( ) ) )
114+ } else {
115+ write ! (
116+ f,
117+ "{}_{}" ,
118+ self . type_prefix( ) ,
119+ encode_base32_uuid( self . uuid( ) )
120+ )
121+ }
122+ }
118123}
119124
120-
121125impl InOutFuncs for TypeID {
122- fn input ( input : & core:: ffi:: CStr ) -> TypeID {
123- // Convert the input to a str and handle potential UTF-8 errors
124- let str_input = input. to_str ( ) . expect ( "text input is not valid UTF8" ) ;
125-
126- TypeID :: from_string ( str_input) . unwrap ( )
127- }
128-
129- fn output ( & self , buffer : & mut pgrx:: StringInfo ) {
130- // Use write! macro to directly push the string representation into the buffer
131- use std:: fmt:: Write ;
132- write ! ( buffer, "{}" , self ) . expect ( "Failed to write to buffer" ) ;
133- }
134- }
126+ fn input ( input : & core:: ffi:: CStr ) -> TypeID {
127+ // Convert the input to a str and handle potential UTF-8 errors
128+ let str_input = input. to_str ( ) . expect ( "text input is not valid UTF8" ) ;
129+
130+ TypeID :: from_string ( str_input) . unwrap ( )
131+ }
132+
133+ fn output ( & self , buffer : & mut pgrx:: StringInfo ) {
134+ // Use write! macro to directly push the string representation into the buffer
135+ use std:: fmt:: Write ;
136+ write ! ( buffer, "{}" , self ) . expect ( "Failed to write to buffer" ) ;
137+ }
138+ }
0 commit comments