22// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33// for complete details.
44
5+ use asn1:: PrintableString as Asn1PrintableString ;
56use pyo3:: types:: PyAnyMethods ;
67use pyo3:: { IntoPyObject , PyTypeInfo } ;
78
@@ -32,6 +33,9 @@ pub enum Type {
3233 /// `str` -> `UTF8String`
3334 #[ pyo3( constructor = ( ) ) ]
3435 PyStr ( ) ,
36+ /// PrintableString (`str`)
37+ #[ pyo3( constructor = ( ) ) ]
38+ PrintableString ( ) ,
3539}
3640
3741/// A type that we know how to encode/decode, along with any
@@ -70,6 +74,40 @@ impl Annotation {
7074 }
7175}
7276
77+ #[ derive( pyo3:: FromPyObject ) ]
78+ #[ pyo3:: pyclass( frozen, module = "cryptography.hazmat.bindings._rust.asn1" ) ]
79+ pub struct PrintableString {
80+ pub ( crate ) inner : pyo3:: Py < pyo3:: types:: PyString > ,
81+ }
82+
83+ #[ pyo3:: pymethods]
84+ impl PrintableString {
85+ #[ new]
86+ #[ pyo3( signature = ( inner, ) ) ]
87+ fn new ( py : pyo3:: Python < ' _ > , inner : pyo3:: Py < pyo3:: types:: PyString > ) -> pyo3:: PyResult < Self > {
88+ if Asn1PrintableString :: new ( & inner. to_cow ( py) ?) . is_none ( ) {
89+ return Err ( pyo3:: exceptions:: PyValueError :: new_err ( format ! (
90+ "invalid PrintableString: {inner}"
91+ ) ) ) ;
92+ }
93+
94+ Ok ( PrintableString { inner } )
95+ }
96+
97+ #[ pyo3( signature = ( ) ) ]
98+ pub fn as_str ( & self , py : pyo3:: Python < ' _ > ) -> pyo3:: PyResult < pyo3:: Py < pyo3:: types:: PyString > > {
99+ Ok ( self . inner . clone_ref ( py) )
100+ }
101+
102+ fn __eq__ ( & self , py : pyo3:: Python < ' _ > , other : pyo3:: PyRef < ' _ , Self > ) -> pyo3:: PyResult < bool > {
103+ ( * * self . inner . bind ( py) ) . eq ( other. inner . bind ( py) )
104+ }
105+
106+ pub fn __repr__ ( & self , py : pyo3:: Python < ' _ > ) -> pyo3:: PyResult < String > {
107+ Ok ( format ! ( "PrintableString({})" , self . inner. bind( py) . repr( ) ?) )
108+ }
109+ }
110+
73111/// Utility function for converting builtin Python types
74112/// to their Rust `Type` equivalent.
75113#[ pyo3:: pyfunction]
@@ -85,6 +123,8 @@ pub fn non_root_python_to_rust<'p>(
85123 Type :: PyStr ( ) . into_pyobject ( py)
86124 } else if class. is ( pyo3:: types:: PyBytes :: type_object ( py) ) {
87125 Type :: PyBytes ( ) . into_pyobject ( py)
126+ } else if class. is ( PrintableString :: type_object ( py) ) {
127+ Type :: PrintableString ( ) . into_pyobject ( py)
88128 } else {
89129 Err ( pyo3:: exceptions:: PyTypeError :: new_err ( format ! (
90130 "cannot handle type: {class:?}"
@@ -131,5 +171,5 @@ pub(crate) fn python_class_to_annotated<'p>(
131171#[ pyo3:: pymodule( gil_used = false ) ]
132172pub ( crate ) mod types {
133173 #[ pymodule_export]
134- use super :: { AnnotatedType , Annotation , Type } ;
174+ use super :: { AnnotatedType , Annotation , PrintableString , Type } ;
135175}
0 commit comments