1
- use serde:: Serialize ;
1
+ use serde:: { Deserialize , Serialize } ;
2
2
3
3
/// A schema type representing a variably encoded integer
4
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize ) ]
4
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
5
5
pub enum Varint {
6
6
/// A variably encoded i16
7
7
I16 ,
@@ -26,7 +26,7 @@ pub enum Varint {
26
26
}
27
27
28
28
/// Serde Data Model Types (and friends)
29
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize ) ]
29
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
30
30
pub enum SdmTy {
31
31
/// The `bool` Serde Data Model Type
32
32
Bool ,
@@ -104,7 +104,7 @@ pub enum SdmTy {
104
104
}
105
105
106
106
/// A data type with a name - e.g. a field of a Struct
107
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize ) ]
107
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
108
108
pub struct NamedValue {
109
109
/// The name of this value
110
110
pub name : & ' static str ,
@@ -113,7 +113,7 @@ pub struct NamedValue {
113
113
}
114
114
115
115
/// A data type - e.g. a custom `struct Foo{ ... }` type
116
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize ) ]
116
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
117
117
pub struct NamedType {
118
118
/// The name of this type
119
119
pub name : & ' static str ,
@@ -122,7 +122,7 @@ pub struct NamedType {
122
122
}
123
123
124
124
/// An enum variant with a name, e.g. `T::Bar(...)`
125
- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize ) ]
125
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize ) ]
126
126
pub struct NamedVariant {
127
127
/// The name of this variant
128
128
pub name : & ' static str ,
@@ -278,3 +278,189 @@ impl Schema for alloc::string::String {
278
278
ty : & SdmTy :: String ,
279
279
} ;
280
280
}
281
+
282
+ #[ cfg( any( feature = "use-std" , feature = "alloc" ) ) ]
283
+ pub ( crate ) mod owned {
284
+ use super :: * ;
285
+
286
+ #[ cfg( feature = "use-std" ) ]
287
+ use std:: { boxed:: Box , string:: String , vec:: Vec } ;
288
+
289
+ #[ cfg( all( not( feature = "use-std" ) , feature = "alloc" ) ) ]
290
+ use alloc:: {
291
+ boxed:: Box ,
292
+ string:: { String , ToString } ,
293
+ vec:: Vec ,
294
+ } ;
295
+
296
+ /// Serde Data Model Types (and friends)
297
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
298
+ pub enum OwnedSdmTy {
299
+ /// The `bool` Serde Data Model Type
300
+ Bool ,
301
+
302
+ /// The `i8` Serde Data Model Type
303
+ I8 ,
304
+
305
+ /// The `u8` Serde Data Model Type
306
+ U8 ,
307
+
308
+ /// The Serde Data Model Type for variably length encoded integers
309
+ Varint ( Varint ) ,
310
+
311
+ /// The `f32` Serde Data Model Type
312
+ F32 ,
313
+
314
+ /// The `f64 Serde Data Model Type
315
+ F64 ,
316
+
317
+ /// The `char` Serde Data Model Type
318
+ Char ,
319
+
320
+ /// The `String` Serde Data Model Type
321
+ String ,
322
+
323
+ /// The `[u8; N]` Serde Data Model Type
324
+ ByteArray ,
325
+
326
+ /// The `Option<T>` Serde Data Model Type
327
+ Option ( Box < OwnedNamedType > ) ,
328
+
329
+ /// The `()` Serde Data Model Type
330
+ Unit ,
331
+
332
+ /// The "unit struct" Serde Data Model Type
333
+ UnitStruct ,
334
+
335
+ /// The "unit variant" Serde Data Model Type
336
+ UnitVariant ,
337
+
338
+ /// The "newtype struct" Serde Data Model Type
339
+ NewtypeStruct ( Box < OwnedNamedType > ) ,
340
+
341
+ /// The "newtype variant" Serde Data Model Type
342
+ NewtypeVariant ( Box < OwnedNamedType > ) ,
343
+
344
+ /// The "Sequence" Serde Data Model Type
345
+ Seq ( Box < OwnedNamedType > ) ,
346
+
347
+ /// The "Tuple" Serde Data Model Type
348
+ Tuple ( Vec < OwnedNamedType > ) ,
349
+
350
+ /// The "Tuple Struct" Serde Data Model Type
351
+ TupleStruct ( Vec < OwnedNamedType > ) ,
352
+
353
+ /// The "Tuple Variant" Serde Data Model Type
354
+ TupleVariant ( Vec < OwnedNamedType > ) ,
355
+
356
+ /// The "Map" Serde Data Model Type
357
+ Map {
358
+ /// The map "Key" type
359
+ key : Box < OwnedNamedType > ,
360
+ /// The map "Value" type
361
+ val : Box < OwnedNamedType > ,
362
+ } ,
363
+
364
+ /// The "Struct" Serde Data Model Type
365
+ Struct ( Vec < OwnedNamedValue > ) ,
366
+
367
+ /// The "Struct Variant" Serde Data Model Type
368
+ StructVariant ( Vec < OwnedNamedValue > ) ,
369
+
370
+ /// The "Enum" Serde Data Model Type (which contains any of the "Variant" types)
371
+ Enum ( Vec < OwnedNamedVariant > ) ,
372
+ }
373
+
374
+ impl From < & SdmTy > for OwnedSdmTy {
375
+ fn from ( other : & SdmTy ) -> Self {
376
+ match other {
377
+ SdmTy :: Bool => Self :: Bool ,
378
+ SdmTy :: I8 => Self :: I8 ,
379
+ SdmTy :: U8 => Self :: U8 ,
380
+ SdmTy :: Varint ( v) => Self :: Varint ( * v) ,
381
+ SdmTy :: F32 => Self :: F32 ,
382
+ SdmTy :: F64 => Self :: F64 ,
383
+ SdmTy :: Char => Self :: Char ,
384
+ SdmTy :: String => Self :: String ,
385
+ SdmTy :: ByteArray => Self :: ByteArray ,
386
+ SdmTy :: Option ( o) => Self :: Option ( Box :: new ( ( * o) . into ( ) ) ) ,
387
+ SdmTy :: Unit => Self :: Unit ,
388
+ SdmTy :: UnitStruct => Self :: UnitStruct ,
389
+ SdmTy :: UnitVariant => Self :: UnitVariant ,
390
+ SdmTy :: NewtypeStruct ( nts) => Self :: NewtypeStruct ( Box :: new ( ( * nts) . into ( ) ) ) ,
391
+ SdmTy :: NewtypeVariant ( ntv) => Self :: NewtypeVariant ( Box :: new ( ( * ntv) . into ( ) ) ) ,
392
+ SdmTy :: Seq ( s) => Self :: Seq ( Box :: new ( ( * s) . into ( ) ) ) ,
393
+ SdmTy :: Tuple ( t) => Self :: Tuple ( t. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) ) ,
394
+ SdmTy :: TupleStruct ( ts) => {
395
+ Self :: TupleStruct ( ts. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) )
396
+ }
397
+ SdmTy :: TupleVariant ( tv) => {
398
+ Self :: TupleVariant ( tv. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) )
399
+ }
400
+ SdmTy :: Map { key, val } => Self :: Map {
401
+ key : Box :: new ( ( * key) . into ( ) ) ,
402
+ val : Box :: new ( ( * val) . into ( ) ) ,
403
+ } ,
404
+ SdmTy :: Struct ( s) => Self :: Struct ( s. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) ) ,
405
+ SdmTy :: StructVariant ( sv) => {
406
+ Self :: StructVariant ( sv. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) )
407
+ }
408
+ SdmTy :: Enum ( e) => Self :: Enum ( e. iter ( ) . map ( |i| ( * i) . into ( ) ) . collect ( ) ) ,
409
+ }
410
+ }
411
+ }
412
+
413
+ /// A data type with a name - e.g. a field of a Struct
414
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
415
+ pub struct OwnedNamedValue {
416
+ /// The name of this value
417
+ pub name : String ,
418
+ /// The type of this value
419
+ pub ty : OwnedNamedType ,
420
+ }
421
+
422
+ impl From < & NamedValue > for OwnedNamedValue {
423
+ fn from ( value : & NamedValue ) -> Self {
424
+ Self {
425
+ name : value. name . to_string ( ) ,
426
+ ty : value. ty . into ( ) ,
427
+ }
428
+ }
429
+ }
430
+
431
+ /// A data type - e.g. a custom `struct Foo{ ... }` type
432
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
433
+ pub struct OwnedNamedType {
434
+ /// The name of this type
435
+ pub name : String ,
436
+ /// The type
437
+ pub ty : OwnedSdmTy ,
438
+ }
439
+
440
+ impl From < & NamedType > for OwnedNamedType {
441
+ fn from ( value : & NamedType ) -> Self {
442
+ Self {
443
+ name : value. name . to_string ( ) ,
444
+ ty : value. ty . into ( ) ,
445
+ }
446
+ }
447
+ }
448
+
449
+ /// An enum variant with a name, e.g. `T::Bar(...)`
450
+ #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
451
+ pub struct OwnedNamedVariant {
452
+ /// The name of this variant
453
+ pub name : String ,
454
+ /// The type of this variant
455
+ pub ty : OwnedSdmTy ,
456
+ }
457
+
458
+ impl From < & NamedVariant > for OwnedNamedVariant {
459
+ fn from ( value : & NamedVariant ) -> Self {
460
+ Self {
461
+ name : value. name . to_string ( ) ,
462
+ ty : value. ty . into ( ) ,
463
+ }
464
+ }
465
+ }
466
+ }
0 commit comments