-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
587105f
commit a542d55
Showing
7 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use specta::{ts::ExportConfig, Type, TypeCollection}; | ||
|
||
#[derive(Type)] | ||
pub struct Hello { | ||
pub a: i32, | ||
pub b: bool, | ||
} | ||
|
||
#[derive(Type)] | ||
pub struct Test(Hello); | ||
|
||
fn main() { | ||
let code = TypeCollection::default() | ||
.register::<Hello>() | ||
.register::<Test>() | ||
.export_ts(&ExportConfig::default()) | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
code, | ||
"export type Hello = { a: number; b: boolean }\nexport type Test = Hello\n" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::collections::HashMap; | ||
|
||
use crate::{NamedDataType, NamedType, SpectaID, TypeMap}; | ||
|
||
/// Define a set of types which can be exported together | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct TypeCollection { | ||
types: HashMap<SpectaID, fn(&mut TypeMap) -> NamedDataType>, | ||
} | ||
|
||
impl Default for TypeCollection { | ||
fn default() -> Self { | ||
Self { | ||
types: HashMap::new(), | ||
} | ||
} | ||
} | ||
Check warning on line 17 in src/type_collection.rs GitHub Actions / clippythis `impl` can be derived
|
||
|
||
impl TypeCollection { | ||
/// Join another type collection into this one. | ||
pub fn join(&mut self, collection: TypeCollection) -> &mut Self { | ||
self.types.extend(collection.types); | ||
self | ||
} | ||
|
||
/// Register a type with the collection. | ||
pub fn register<T: NamedType>(&mut self) -> &mut Self { | ||
self.types | ||
.insert(T::sid(), |type_map| T::definition_named_data_type(type_map)); | ||
self | ||
} | ||
|
||
/// Export all the types in the collection. | ||
pub fn export(&mut self, mut type_map: &mut crate::TypeMap) { | ||
for (sid, export) in self.types.iter() { | ||
let dt = export(&mut type_map); | ||
Check warning on line 36 in src/type_collection.rs GitHub Actions / clippythis expression creates a reference which is immediately dereferenced by the compiler
|
||
type_map.insert(*sid, dt); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use specta::{Type, TypeCollection, TypeMap}; | ||
|
||
#[derive(Type)] | ||
struct A2(String); | ||
|
||
#[derive(Type)] | ||
struct A { | ||
a: A2, | ||
} | ||
|
||
#[derive(Type)] | ||
struct C { | ||
d: String, | ||
} | ||
|
||
#[derive(Type)] | ||
struct D(String); | ||
|
||
#[test] | ||
fn type_collection_export() { | ||
let mut type_map = TypeMap::default(); | ||
TypeCollection::default() | ||
.register::<A>() | ||
.export(&mut type_map); | ||
assert_eq!(type_map.len(), 2); | ||
} | ||
|
||
#[test] | ||
fn type_collection_merge() { | ||
let mut a = TypeCollection::default(); | ||
a.register::<A>(); | ||
let mut b = TypeCollection::default(); | ||
b.register::<C>(); | ||
|
||
let mut type_map = TypeMap::default(); | ||
TypeCollection::default() | ||
.register::<D>() | ||
.join(a) | ||
.join(b) | ||
.export(&mut type_map); | ||
assert_eq!(type_map.len(), 4); | ||
} | ||
|
||
#[test] | ||
fn type_collection_duplicate_register_ty() { | ||
let mut type_map = TypeMap::default(); | ||
TypeCollection::default() | ||
.register::<C>() | ||
.register::<C>() | ||
.export(&mut type_map); | ||
assert_eq!(type_map.len(), 1); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "typescript")] | ||
fn type_collection_ts() { | ||
let result = TypeCollection::default() | ||
.register::<A>() | ||
.register::<C>() | ||
.register::<D>() | ||
.export_ts(&Default::default()) | ||
.unwrap(); | ||
assert_eq!( | ||
result, | ||
"export type A = { a: A2 }\nexport type A2 = string\nexport type C = { d: string }\nexport type D = string\n" | ||
); | ||
} |