-
Notifications
You must be signed in to change notification settings - Fork 99
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
Showing
8 changed files
with
114 additions
and
1 deletion.
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
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
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,68 @@ | ||
use quote::quote; | ||
use syn::{Result, Type}; | ||
|
||
use crate::{ | ||
attr::{EnumAttr, StructAttr}, | ||
deps::Dependencies, | ||
DerivedTS, | ||
}; | ||
|
||
pub(crate) fn type_as_struct(attr: &StructAttr, name: &str, type_as: &Type) -> Result<DerivedTS> { | ||
if attr.rename_all.is_some() { | ||
syn_err!("`rename_all` is not compatible with `as`"); | ||
} | ||
if attr.tag.is_some() { | ||
syn_err!("`tag` is not compatible with `as`"); | ||
} | ||
|
||
let crate_rename = attr.crate_rename(); | ||
|
||
Ok(DerivedTS { | ||
crate_rename: crate_rename.clone(), | ||
inline: quote!(#type_as::inline()), | ||
inline_flattened: None, | ||
docs: attr.docs.clone(), | ||
dependencies: Dependencies::new(crate_rename), | ||
export: attr.export, | ||
export_to: attr.export_to.clone(), | ||
ts_name: name.to_owned(), | ||
concrete: attr.concrete.clone(), | ||
bound: attr.bound.clone(), | ||
}) | ||
} | ||
|
||
pub(crate) fn type_as_enum(attr: &EnumAttr, name: &str, type_as: &Type) -> Result<DerivedTS> { | ||
if attr.rename_all.is_some() { | ||
syn_err!("`rename_all` is not compatible with `as`"); | ||
} | ||
if attr.rename_all_fields.is_some() { | ||
syn_err!("`rename_all_fields` is not compatible with `as`"); | ||
} | ||
if attr.tag.is_some() { | ||
syn_err!("`tag` is not compatible with `as`"); | ||
} | ||
if attr.content.is_some() { | ||
syn_err!("`content` is not compatible with `as`"); | ||
} | ||
if attr.untagged { | ||
syn_err!("`untagged` is not compatible with `as`"); | ||
} | ||
if attr.type_override.is_some() { | ||
syn_err!("`type` is not compatible with `as`"); | ||
} | ||
|
||
let crate_rename = attr.crate_rename(); | ||
|
||
Ok(DerivedTS { | ||
crate_rename: crate_rename.clone(), | ||
inline: quote!(#type_as::inline()), | ||
inline_flattened: None, | ||
docs: attr.docs.clone(), | ||
dependencies: Dependencies::new(crate_rename), | ||
export: attr.export, | ||
export_to: attr.export_to.clone(), | ||
ts_name: name.to_owned(), | ||
concrete: attr.concrete.clone(), | ||
bound: attr.bound.clone(), | ||
}) | ||
} |
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,22 @@ | ||
use ts_rs::TS; | ||
|
||
#[derive(TS)] | ||
#[ts(as = "T")] | ||
pub enum UntaggedEnum<T: TS> { | ||
Left(T), | ||
Right(T), | ||
} | ||
|
||
#[test] | ||
pub fn top_level_type_as_enum() { | ||
assert_eq!(UntaggedEnum::<String>::inline(), r#"string"#) | ||
} | ||
|
||
#[derive(TS)] | ||
#[ts(as = "T")] | ||
pub struct Wrapper<T: TS>(T); | ||
|
||
#[test] | ||
pub fn top_level_type_as_struct() { | ||
assert_eq!(Wrapper::<String>::inline(), r#"string"#) | ||
} |