12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
+ use crate :: analysis:: analysed_type:: {
16
+ bool, chr, f32, f64, s16, s32, s64, s8, str, u16, u32, u64, u8,
17
+ } ;
15
18
use crate :: analysis:: AnalysisResult ;
16
19
use crate :: component:: { ComponentExternalKind , PrimitiveValueType } ;
17
20
18
21
#[ derive( Debug , Clone , PartialEq , Hash , Eq ) ]
19
22
#[ cfg_attr( feature = "json" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
23
+ #[ cfg_attr( feature = "json" , serde( tag = "type" ) ) ]
20
24
#[ cfg_attr( feature = "bincode" , derive( bincode:: Encode , bincode:: Decode ) ) ]
21
25
#[ cfg_attr( feature = "poem_openapi" , derive( poem_openapi:: Union ) ) ]
22
26
#[ cfg_attr(
@@ -281,6 +285,144 @@ pub enum AnalysedType {
281
285
Handle ( TypeHandle ) ,
282
286
}
283
287
288
+ pub mod analysed_type {
289
+ use crate :: analysis:: * ;
290
+
291
+ pub fn field ( name : & str , typ : AnalysedType ) -> NameTypePair {
292
+ NameTypePair {
293
+ name : name. to_string ( ) ,
294
+ typ,
295
+ }
296
+ }
297
+
298
+ pub fn case ( name : & str , typ : AnalysedType ) -> NameOptionTypePair {
299
+ NameOptionTypePair {
300
+ name : name. to_string ( ) ,
301
+ typ : Some ( typ) ,
302
+ }
303
+ }
304
+
305
+ pub fn unit_case ( name : & str ) -> NameOptionTypePair {
306
+ NameOptionTypePair {
307
+ name : name. to_string ( ) ,
308
+ typ : None ,
309
+ }
310
+ }
311
+
312
+ pub fn bool ( ) -> AnalysedType {
313
+ AnalysedType :: Bool ( TypeBool )
314
+ }
315
+
316
+ pub fn s8 ( ) -> AnalysedType {
317
+ AnalysedType :: S8 ( TypeS8 )
318
+ }
319
+
320
+ pub fn s16 ( ) -> AnalysedType {
321
+ AnalysedType :: S16 ( TypeS16 )
322
+ }
323
+
324
+ pub fn s32 ( ) -> AnalysedType {
325
+ AnalysedType :: S32 ( TypeS32 )
326
+ }
327
+
328
+ pub fn s64 ( ) -> AnalysedType {
329
+ AnalysedType :: S64 ( TypeS64 )
330
+ }
331
+
332
+ pub fn u8 ( ) -> AnalysedType {
333
+ AnalysedType :: U8 ( TypeU8 )
334
+ }
335
+
336
+ pub fn u16 ( ) -> AnalysedType {
337
+ AnalysedType :: U16 ( TypeU16 )
338
+ }
339
+
340
+ pub fn u32 ( ) -> AnalysedType {
341
+ AnalysedType :: U32 ( TypeU32 )
342
+ }
343
+
344
+ pub fn u64 ( ) -> AnalysedType {
345
+ AnalysedType :: U64 ( TypeU64 )
346
+ }
347
+
348
+ pub fn f32 ( ) -> AnalysedType {
349
+ AnalysedType :: F32 ( TypeF32 )
350
+ }
351
+
352
+ pub fn f64 ( ) -> AnalysedType {
353
+ AnalysedType :: F64 ( TypeF64 )
354
+ }
355
+
356
+ pub fn chr ( ) -> AnalysedType {
357
+ AnalysedType :: Chr ( TypeChr )
358
+ }
359
+
360
+ pub fn str ( ) -> AnalysedType {
361
+ AnalysedType :: Str ( TypeStr )
362
+ }
363
+
364
+ pub fn list ( inner : AnalysedType ) -> AnalysedType {
365
+ AnalysedType :: List ( TypeList {
366
+ inner : Box :: new ( inner) ,
367
+ } )
368
+ }
369
+
370
+ pub fn option ( inner : AnalysedType ) -> AnalysedType {
371
+ AnalysedType :: Option ( TypeOption {
372
+ inner : Box :: new ( inner) ,
373
+ } )
374
+ }
375
+
376
+ pub fn flags ( names : & [ & str ] ) -> AnalysedType {
377
+ AnalysedType :: Flags ( TypeFlags {
378
+ names : names. iter ( ) . map ( |n| n. to_string ( ) ) . collect ( ) ,
379
+ } )
380
+ }
381
+
382
+ pub fn r#enum ( cases : & [ & str ] ) -> AnalysedType {
383
+ AnalysedType :: Enum ( TypeEnum {
384
+ cases : cases. iter ( ) . map ( |n| n. to_string ( ) ) . collect ( ) ,
385
+ } )
386
+ }
387
+
388
+ pub fn tuple ( items : Vec < AnalysedType > ) -> AnalysedType {
389
+ AnalysedType :: Tuple ( TypeTuple { items } )
390
+ }
391
+
392
+ pub fn result ( ok : AnalysedType , err : AnalysedType ) -> AnalysedType {
393
+ AnalysedType :: Result ( TypeResult {
394
+ ok : Some ( Box :: new ( ok) ) ,
395
+ err : Some ( Box :: new ( err) ) ,
396
+ } )
397
+ }
398
+
399
+ pub fn result_ok ( ok : AnalysedType ) -> AnalysedType {
400
+ AnalysedType :: Result ( TypeResult {
401
+ ok : Some ( Box :: new ( ok) ) ,
402
+ err : None ,
403
+ } )
404
+ }
405
+
406
+ pub fn result_err ( err : AnalysedType ) -> AnalysedType {
407
+ AnalysedType :: Result ( TypeResult {
408
+ ok : None ,
409
+ err : Some ( Box :: new ( err) ) ,
410
+ } )
411
+ }
412
+
413
+ pub fn record ( fields : Vec < NameTypePair > ) -> AnalysedType {
414
+ AnalysedType :: Record ( TypeRecord { fields } )
415
+ }
416
+
417
+ pub fn variant ( cases : Vec < NameOptionTypePair > ) -> AnalysedType {
418
+ AnalysedType :: Variant ( TypeVariant { cases } )
419
+ }
420
+
421
+ pub fn handle ( resource_id : AnalysedResourceId , mode : AnalysedResourceMode ) -> AnalysedType {
422
+ AnalysedType :: Handle ( TypeHandle { resource_id, mode } )
423
+ }
424
+ }
425
+
284
426
#[ derive( Debug , Clone , PartialEq , Hash , Eq ) ]
285
427
#[ cfg_attr( feature = "json" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
286
428
#[ cfg_attr( feature = "bincode" , derive( bincode:: Encode , bincode:: Decode ) ) ]
@@ -299,19 +441,19 @@ pub struct AnalysedResourceId(pub u64);
299
441
impl From < & PrimitiveValueType > for AnalysedType {
300
442
fn from ( value : & PrimitiveValueType ) -> Self {
301
443
match value {
302
- PrimitiveValueType :: Bool => AnalysedType :: Bool ( TypeBool ) ,
303
- PrimitiveValueType :: S8 => AnalysedType :: S8 ( TypeS8 ) ,
304
- PrimitiveValueType :: U8 => AnalysedType :: U8 ( TypeU8 ) ,
305
- PrimitiveValueType :: S16 => AnalysedType :: S16 ( TypeS16 ) ,
306
- PrimitiveValueType :: U16 => AnalysedType :: U16 ( TypeU16 ) ,
307
- PrimitiveValueType :: S32 => AnalysedType :: S32 ( TypeS32 ) ,
308
- PrimitiveValueType :: U32 => AnalysedType :: U32 ( TypeU32 ) ,
309
- PrimitiveValueType :: S64 => AnalysedType :: S64 ( TypeS64 ) ,
310
- PrimitiveValueType :: U64 => AnalysedType :: U64 ( TypeU64 ) ,
311
- PrimitiveValueType :: F32 => AnalysedType :: F32 ( TypeF32 ) ,
312
- PrimitiveValueType :: F64 => AnalysedType :: F64 ( TypeF64 ) ,
313
- PrimitiveValueType :: Chr => AnalysedType :: Chr ( TypeChr ) ,
314
- PrimitiveValueType :: Str => AnalysedType :: Str ( TypeStr ) ,
444
+ PrimitiveValueType :: Bool => bool ( ) ,
445
+ PrimitiveValueType :: S8 => s8 ( ) ,
446
+ PrimitiveValueType :: U8 => u8 ( ) ,
447
+ PrimitiveValueType :: S16 => s16 ( ) ,
448
+ PrimitiveValueType :: U16 => u16 ( ) ,
449
+ PrimitiveValueType :: S32 => s32 ( ) ,
450
+ PrimitiveValueType :: U32 => u32 ( ) ,
451
+ PrimitiveValueType :: S64 => s64 ( ) ,
452
+ PrimitiveValueType :: U64 => u64 ( ) ,
453
+ PrimitiveValueType :: F32 => f32 ( ) ,
454
+ PrimitiveValueType :: F64 => f64 ( ) ,
455
+ PrimitiveValueType :: Chr => chr ( ) ,
456
+ PrimitiveValueType :: Str => str ( ) ,
315
457
}
316
458
}
317
459
}
@@ -381,3 +523,38 @@ impl AnalysisFailure {
381
523
}
382
524
}
383
525
}
526
+
527
+ #[ cfg( test) ]
528
+ mod tests {
529
+ use crate :: analysis:: analysed_type:: { bool, list, str} ;
530
+ use crate :: analysis:: {
531
+ AnalysedExport , AnalysedFunction , AnalysedFunctionParameter , AnalysedFunctionResult ,
532
+ AnalysedInstance ,
533
+ } ;
534
+ use poem_openapi:: types:: ToJSON ;
535
+ use pretty_assertions:: assert_eq;
536
+
537
+ #[ cfg( feature = "poem_openapi" ) ]
538
+ #[ cfg( feature = "json" ) ]
539
+ #[ test]
540
+ fn analysed_export_poem_and_serde_are_compatible ( ) {
541
+ let export1 = AnalysedExport :: Instance ( AnalysedInstance {
542
+ name : "inst1" . to_string ( ) ,
543
+ functions : vec ! [ AnalysedFunction {
544
+ name: "func1" . to_string( ) ,
545
+ parameters: vec![ AnalysedFunctionParameter {
546
+ name: "param1" . to_string( ) ,
547
+ typ: bool ( ) ,
548
+ } ] ,
549
+ results: vec![ AnalysedFunctionResult {
550
+ name: None ,
551
+ typ: list( str ( ) ) ,
552
+ } ] ,
553
+ } ] ,
554
+ } ) ;
555
+ let poem_serialized = export1. to_json_string ( ) ;
556
+ let serde_deserialized: AnalysedExport = serde_json:: from_str ( & poem_serialized) . unwrap ( ) ;
557
+
558
+ assert_eq ! ( export1, serde_deserialized) ;
559
+ }
560
+ }
0 commit comments