@@ -6,7 +6,7 @@ use std::time::Duration;
66// Converts from Python classes we define in pure python so we can use dataclasses
77// to represent the input types
88// TODO: Copy strings of these from egg-smol... Maybe actually wrap those isntead.
9- use pyo3:: { ffi :: PyDateTime_Delta , prelude:: * , types :: PyDelta } ;
9+ use pyo3:: prelude:: * ;
1010
1111// Execute the block and wrap the error in a type error
1212fn wrap_error < T > ( tp : & str , obj : & ' _ PyAny , block : impl FnOnce ( ) -> PyResult < T > ) -> PyResult < T > {
@@ -18,24 +18,55 @@ fn wrap_error<T>(tp: &str, obj: &'_ PyAny, block: impl FnOnce() -> PyResult<T>)
1818 } )
1919}
2020
21- // Wrapped version of Variant
22- pub struct WrappedVariant ( egg_smol:: ast:: Variant ) ;
21+ // A variant of a constructor
2322
24- impl FromPyObject < ' _ > for WrappedVariant {
25- fn extract ( obj : & ' _ PyAny ) -> PyResult < Self > {
26- wrap_error ( "Variant" , obj, || {
27- Ok ( WrappedVariant ( egg_smol:: ast:: Variant {
28- name : obj. getattr ( "name" ) ?. extract :: < String > ( ) ?. into ( ) ,
29- cost : obj. getattr ( "cost" ) ?. extract ( ) ?,
30- types : obj
31- . getattr ( "types" ) ?
32- . extract :: < Vec < String > > ( ) ?
33- . into_iter ( )
34- . map ( |x| x. into ( ) )
35- . collect ( ) ,
36- } ) )
23+ #[ pyclass( name = "Variant" ) ]
24+ #[ derive( Clone ) ]
25+ pub ( crate ) struct WrappedVariant ( egg_smol:: ast:: Variant ) ;
26+
27+ #[ pymethods]
28+ impl WrappedVariant {
29+ #[ new]
30+ fn new ( name : String , types : Vec < String > , cost : Option < usize > ) -> Self {
31+ Self ( egg_smol:: ast:: Variant {
32+ name : name. into ( ) ,
33+ types : types. into_iter ( ) . map ( |x| x. into ( ) ) . collect ( ) ,
34+ cost,
3735 } )
3836 }
37+ #[ getter]
38+ fn name ( & self ) -> & str {
39+ self . 0 . name . into ( )
40+ }
41+ #[ getter]
42+ fn types ( & self ) -> Vec < String > {
43+ self . 0 . types . iter ( ) . map ( |x| x. to_string ( ) ) . collect ( )
44+ }
45+ #[ getter]
46+ fn cost ( & self ) -> Option < usize > {
47+ self . 0 . cost
48+ }
49+
50+ fn __repr__ ( & self ) -> String {
51+ format ! (
52+ "Variant(name={}, types=[{}], cost={})" ,
53+ self . 0 . name. to_string( ) ,
54+ self . 0
55+ . types
56+ . iter( )
57+ . map( |x| x. to_string( ) )
58+ . collect:: <Vec <_>>( )
59+ . join( ", " ) ,
60+ match self . 0 . cost {
61+ Some ( x) => x. to_string( ) ,
62+ None => "None" . to_string( ) ,
63+ }
64+ )
65+ }
66+
67+ fn __str__ ( & self ) -> String {
68+ format ! ( "{:#?}" , self . 0 )
69+ }
3970}
4071
4172impl From < WrappedVariant > for egg_smol:: ast:: Variant {
@@ -44,6 +75,12 @@ impl From<WrappedVariant> for egg_smol::ast::Variant {
4475 }
4576}
4677
78+ impl From < egg_smol:: ast:: Variant > for WrappedVariant {
79+ fn from ( other : egg_smol:: ast:: Variant ) -> Self {
80+ WrappedVariant ( other)
81+ }
82+ }
83+
4784// Wrapped version of FunctionDecl
4885pub struct WrappedFunctionDecl ( egg_smol:: ast:: FunctionDecl ) ;
4986impl FromPyObject < ' _ > for WrappedFunctionDecl {
@@ -140,6 +177,12 @@ impl From<WrappedExpr> for egg_smol::ast::Expr {
140177 }
141178}
142179
180+ impl From < egg_smol:: ast:: Expr > for WrappedExpr {
181+ fn from ( other : egg_smol:: ast:: Expr ) -> Self {
182+ WrappedExpr ( other)
183+ }
184+ }
185+
143186// Wrapped version of Literal
144187pub struct WrappedLiteral ( egg_smol:: ast:: Literal ) ;
145188
@@ -253,7 +296,7 @@ impl From<Duration> for WrappedDuration {
253296impl IntoPy < PyObject > for WrappedDuration {
254297 fn into_py ( self , py : Python < ' _ > ) -> PyObject {
255298 let d = self . 0 ;
256- PyDelta :: new (
299+ pyo3 :: types :: PyDelta :: new (
257300 py,
258301 0 ,
259302 0 ,
0 commit comments