11use crate :: cross:: clrepr:: CLReprObject ;
2- use crate :: cross:: { CLRepr , StringType } ;
2+ use crate :: cross:: { CLRepr , CLReprObjectKind , StringType } ;
33use pyo3:: exceptions:: { PyNotImplementedError , PyTypeError } ;
44use pyo3:: types:: {
55 PyBool , PyComplex , PyDate , PyDict , PyFloat , PyFrame , PyFunction , PyInt , PyList , PySequence ,
@@ -16,15 +16,9 @@ pub enum PythonRef {
1616 PyExternalFunction ( Py < PyFunction > ) ,
1717}
1818
19- pub trait CLReprPython : Sized {
20- fn from_python_ref ( v : & PyAny ) -> Result < Self , PyErr > ;
21- fn into_py_impl ( from : CLRepr , py : Python ) -> Result < PyObject , PyErr > ;
22- fn into_py ( self , py : Python ) -> Result < PyObject , PyErr > ;
23- }
24-
25- impl CLReprPython for CLRepr {
19+ impl CLRepr {
2620 /// Convert python value to CLRepr
27- fn from_python_ref ( v : & PyAny ) -> Result < Self , PyErr > {
21+ pub fn from_python_ref ( v : & PyAny ) -> Result < Self , PyErr > {
2822 if v. is_none ( ) {
2923 return Ok ( Self :: Null ) ;
3024 }
@@ -47,7 +41,7 @@ impl CLReprPython for CLRepr {
4741 Self :: Int ( i)
4842 } else if v. get_type ( ) . is_subclass_of :: < PyDict > ( ) ? {
4943 let d = v. downcast :: < PyDict > ( ) ?;
50- let mut obj = CLReprObject :: new ( ) ;
44+ let mut obj = CLReprObject :: new ( CLReprObjectKind :: Object ) ;
5145
5246 for ( k, v) in d. iter ( ) {
5347 if k. get_type ( ) . is_subclass_of :: < PyString > ( ) ? {
@@ -126,6 +120,16 @@ impl CLReprPython for CLRepr {
126120 } )
127121 }
128122
123+ fn into_py_dict_impl ( obj : CLReprObject , py : Python ) -> Result < & PyDict , PyErr > {
124+ let r = PyDict :: new ( py) ;
125+
126+ for ( k, v) in obj. into_iter ( ) {
127+ r. set_item ( k, Self :: into_py_impl ( v, py) ?) ?;
128+ }
129+
130+ Ok ( r)
131+ }
132+
129133 fn into_py_impl ( from : CLRepr , py : Python ) -> Result < PyObject , PyErr > {
130134 Ok ( match from {
131135 CLRepr :: String ( v, _) => PyString :: new ( py, & v) . to_object ( py) ,
@@ -155,11 +159,7 @@ impl CLReprPython for CLRepr {
155159 PyTuple :: new ( py, elements) . to_object ( py)
156160 }
157161 CLRepr :: Object ( obj) => {
158- let r = PyDict :: new ( py) ;
159-
160- for ( k, v) in obj. into_iter ( ) {
161- r. set_item ( k, Self :: into_py_impl ( v, py) ?) ?;
162- }
162+ let r = Self :: into_py_dict_impl ( obj, py) ?;
163163
164164 r. to_object ( py)
165165 }
@@ -189,7 +189,19 @@ impl CLReprPython for CLRepr {
189189 } )
190190 }
191191
192- fn into_py ( self , py : Python ) -> Result < PyObject , PyErr > {
192+ pub fn into_py_dict ( self , py : Python ) -> Result < & PyDict , PyErr > {
193+ Ok ( match self {
194+ CLRepr :: Object ( obj) => Self :: into_py_dict_impl ( obj, py) ?,
195+ other => {
196+ return Err ( PyErr :: new :: < PyNotImplementedError , _ > ( format ! (
197+ "Unable to convert {:?} into PyDict" ,
198+ other. kind( )
199+ ) ) )
200+ }
201+ } )
202+ }
203+
204+ pub fn into_py ( self , py : Python ) -> Result < PyObject , PyErr > {
193205 Self :: into_py_impl ( self , py)
194206 }
195207}
0 commit comments