diff --git a/src/datatype/enum.rs b/src/datatype/enum.rs index 5b56416..25c6ff3 100644 --- a/src/datatype/enum.rs +++ b/src/datatype/enum.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use crate::{ datatype::{DataType, ObjectType, TupleType}, - ExportError, ImplLocation, + ExportError, GenericType, ImplLocation, }; /// Enum type which dictates how the enum is represented. @@ -16,11 +16,11 @@ use crate::{ pub enum EnumType { Untagged { variants: Vec, - generics: Vec>, + generics: Vec, }, Tagged { variants: Vec<(Cow<'static, str>, EnumVariant)>, - generics: Vec>, + generics: Vec, repr: EnumRepr, }, } @@ -32,7 +32,7 @@ impl From for DataType { } impl EnumType { - pub(crate) fn generics(&self) -> &Vec> { + pub(crate) fn generics(&self) -> &Vec { match self { Self::Untagged { generics, .. } => generics, Self::Tagged { generics, .. } => generics, diff --git a/src/datatype/mod.rs b/src/datatype/mod.rs index f2e82d5..f6c61a0 100644 --- a/src/datatype/mod.rs +++ b/src/datatype/mod.rs @@ -1,4 +1,7 @@ -use std::{borrow::Cow, collections::BTreeMap}; +use std::{ + borrow::{Borrow, Cow}, + collections::BTreeMap, +}; mod r#enum; mod literal; @@ -120,14 +123,12 @@ impl NamedDataTypeItem { } /// Returns the generics arguments for the type - pub fn generics(&self) -> Vec> { + pub fn generics(&self) -> Vec { match self { // Named struct Self::Object(ObjectType { generics, .. }) => generics.clone(), // Enum - Self::Enum(EnumType::Tagged { generics, .. } | EnumType::Untagged { generics, .. }) => { - generics.clone() - } + Self::Enum(e) => e.generics().clone(), // Struct with unnamed fields Self::Tuple(tuple) => match tuple { TupleType::Unnamed => vec![], @@ -159,6 +160,12 @@ pub struct DataTypeReference { #[allow(missing_docs)] pub struct GenericType(pub Cow<'static, str>); +impl Borrow for GenericType { + fn borrow(&self) -> &str { + &self.0 + } +} + impl From for DataType { fn from(t: GenericType) -> Self { Self::Generic(t) diff --git a/src/datatype/object.rs b/src/datatype/object.rs index 76595a8..3719738 100644 --- a/src/datatype/object.rs +++ b/src/datatype/object.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use crate::{DataType, NamedDataType, NamedDataTypeItem}; +use crate::{DataType, GenericType, NamedDataType, NamedDataTypeItem}; /// A field in an [`ObjectType`]. #[derive(Debug, Clone, PartialEq)] @@ -16,7 +16,7 @@ pub struct ObjectField { /// Could be from a struct or named enum variant. #[derive(Debug, Clone, PartialEq)] pub struct ObjectType { - pub generics: Vec>, + pub generics: Vec, pub fields: Vec, pub tag: Option>, } diff --git a/src/datatype/tuple.rs b/src/datatype/tuple.rs index 722a0d7..0f65603 100644 --- a/src/datatype/tuple.rs +++ b/src/datatype/tuple.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use crate::{DataType, NamedDataType, NamedDataTypeItem}; +use crate::{DataType, GenericType, NamedDataType, NamedDataTypeItem}; /// Type of a tuple. /// Could be from an actual tuple or unnamed struct. @@ -16,7 +16,7 @@ pub enum TupleType { /// Be aware `()` is treated specially as `null` in Typescript. Named { fields: Vec, - generics: Vec>, + generics: Vec, }, } diff --git a/src/lang/ts/mod.rs b/src/lang/ts/mod.rs index 2b4231d..0d9dd06 100644 --- a/src/lang/ts/mod.rs +++ b/src/lang/ts/mod.rs @@ -16,8 +16,9 @@ use reserved_terms::*; use crate::*; +#[allow(missing_docs)] pub type Result = std::result::Result; -pub type Output = Result; +type Output = Result; /// Convert a type which implements [`Type`](crate::Type) to a TypeScript string with an export. /// @@ -105,7 +106,7 @@ fn export_datatype_inner( let generics = Some(item.generics()) .filter(|generics| !generics.is_empty()) - .map(|generics| format!("<{}>", generics.to_vec().join(", "))) + .map(|generics| format!("<{}>", generics.join(", "))) .unwrap_or_default(); let comments = ctx