19
19
20
20
use std:: vec;
21
21
22
- use bend:: fun:: { Adt as BAdt , Book , Name , Num , Term as BTerm } ;
22
+ use bend:: fun:: { Book , Name , Num , Term as BTerm } ;
23
23
use bend:: imp:: { self } ;
24
24
use num_traits:: ToPrimitive ;
25
25
use pyo3:: types:: { PyAnyMethods , PyString , PyTuple } ;
@@ -228,14 +228,13 @@ pub fn from_term_into_adt(term: &BTerm, def_adts: &Ctrs) -> Option<TermParse> {
228
228
/// * `data` - The Python data associated with this ADT instance
229
229
/// * `book` - The Bend book containing ADT definitions
230
230
#[ derive( Debug , Clone ) ]
231
- pub struct UserAdt < ' py > {
232
- adt : BAdt ,
231
+ pub struct UserAdt < ' py , ' book > {
232
+ book : & ' book Book ,
233
233
full_name : Name ,
234
234
data : Bound < ' py , PyAny > ,
235
- book : Book ,
236
235
}
237
236
238
- impl < ' py > UserAdt < ' py > {
237
+ impl < ' py , ' book > UserAdt < ' py , ' book > {
239
238
/// Creates a new UserAdt instance
240
239
///
241
240
/// # Arguments
@@ -251,7 +250,7 @@ impl<'py> UserAdt<'py> {
251
250
///
252
251
/// This function attempts to create a UserAdt by matching the Python data's `__ctr_type__`
253
252
/// attribute with ADT definitions in the provided Bend book.
254
- pub fn new ( data : Bound < ' py , PyAny > , book : & Book ) -> Option < Self > {
253
+ pub fn new ( data : Bound < ' py , PyAny > , book : & ' book Book ) -> Option < Self > {
255
254
if data. is_none ( ) {
256
255
return None ;
257
256
}
@@ -264,18 +263,12 @@ impl<'py> UserAdt<'py> {
264
263
if let Ok ( binding) = data. getattr ( "__ctr_type__" ) {
265
264
for ( nam, _ctr) in & book. ctrs {
266
265
let new_nam = nam. to_string ( ) ;
267
- let two_names = new_nam. split_once ( '/' ) . unwrap ( ) ;
268
266
269
267
if nam. to_string ( ) == binding. to_string ( ) {
270
268
return Some ( Self {
271
- book : book . clone ( ) ,
269
+ book,
272
270
data,
273
271
full_name : Name :: new ( new_nam. clone ( ) ) ,
274
- adt : book
275
- . adts
276
- . get ( & Name :: new ( two_names. 0 . to_string ( ) ) )
277
- . unwrap ( )
278
- . clone ( ) ,
279
272
} ) ;
280
273
}
281
274
}
@@ -295,10 +288,16 @@ impl<'py> UserAdt<'py> {
295
288
///
296
289
/// This method recursively converts the UserAdt and its fields into a Bend expression,
297
290
/// handling nested ADTs and other field types.
298
- impl < ' py > BendType for UserAdt < ' py > {
291
+ impl < ' py , ' book > BendType for UserAdt < ' py , ' book > {
299
292
fn to_bend ( & self ) -> super :: BendResult {
300
- for ( nam, fields) in & self . adt . ctrs {
301
- if * nam == self . full_name {
293
+ let binding = self . full_name . to_string ( ) ;
294
+ let name = binding. split ( "/" ) . next ( ) . unwrap ( ) ;
295
+ let adt = self . book . adts . get ( & Name :: new ( name) ) . unwrap ( ) ;
296
+
297
+ dbg ! ( binding) ;
298
+
299
+ for ( nam, fields) in adt. ctrs . iter ( ) {
300
+ if nam. to_string ( ) == self . full_name . to_string ( ) {
302
301
let mut adt_fields: Vec < imp:: Expr > = vec ! [ ] ;
303
302
304
303
for field in fields {
@@ -313,7 +312,7 @@ impl<'py> BendType for UserAdt<'py> {
313
312
314
313
if let Some ( t) = extract_type_raw ( attr. clone ( ) ) {
315
314
adt_fields. push ( t. to_bend ( ) . unwrap ( ) ) ;
316
- } else if let Some ( adt) = UserAdt :: new ( attr, & self . book ) {
315
+ } else if let Some ( adt) = UserAdt :: new ( attr, self . book ) {
317
316
let new_adt = adt. to_bend ( ) ;
318
317
adt_fields. push ( new_adt. unwrap ( ) ) ;
319
318
} else {
0 commit comments