Skip to content

Commit

Permalink
GenericType
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendonovich committed Aug 31, 2023
1 parent b014aba commit d462f4a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/datatype/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -16,11 +16,11 @@ use crate::{
pub enum EnumType {
Untagged {
variants: Vec<EnumVariant>,
generics: Vec<Cow<'static, str>>,
generics: Vec<GenericType>,
},
Tagged {
variants: Vec<(Cow<'static, str>, EnumVariant)>,
generics: Vec<Cow<'static, str>>,
generics: Vec<GenericType>,
repr: EnumRepr,
},
}
Expand All @@ -32,7 +32,7 @@ impl From<EnumType> for DataType {
}

impl EnumType {
pub(crate) fn generics(&self) -> &Vec<Cow<'static, str>> {
pub(crate) fn generics(&self) -> &Vec<GenericType> {
match self {
Self::Untagged { generics, .. } => generics,
Self::Tagged { generics, .. } => generics,
Expand Down
17 changes: 12 additions & 5 deletions src/datatype/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{borrow::Cow, collections::BTreeMap};
use std::{
borrow::{Borrow, Cow},
collections::BTreeMap,
};

mod r#enum;
mod literal;
Expand Down Expand Up @@ -120,14 +123,12 @@ impl NamedDataTypeItem {
}

/// Returns the generics arguments for the type
pub fn generics(&self) -> Vec<Cow<'static, str>> {
pub fn generics(&self) -> Vec<GenericType> {
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![],
Expand Down Expand Up @@ -159,6 +160,12 @@ pub struct DataTypeReference {
#[allow(missing_docs)]
pub struct GenericType(pub Cow<'static, str>);

impl Borrow<str> for GenericType {
fn borrow(&self) -> &str {
&self.0
}
}

impl From<GenericType> for DataType {
fn from(t: GenericType) -> Self {
Self::Generic(t)
Expand Down
4 changes: 2 additions & 2 deletions src/datatype/object.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<Cow<'static, str>>,
pub generics: Vec<GenericType>,

Check warning on line 19 in src/datatype/object.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct field

warning: missing documentation for a struct field --> src/datatype/object.rs:19:5 | 19 | pub generics: Vec<GenericType>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: the lint level is defined here --> src/lib.rs:58:58 | 58 | #![warn(clippy::all, clippy::unwrap_used, clippy::panic, missing_docs)] | ^^^^^^^^^^^^
pub fields: Vec<ObjectField>,

Check warning on line 20 in src/datatype/object.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct field

warning: missing documentation for a struct field --> src/datatype/object.rs:20:5 | 20 | pub fields: Vec<ObjectField>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pub tag: Option<Cow<'static, str>>,

Check warning on line 21 in src/datatype/object.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct field

warning: missing documentation for a struct field --> src/datatype/object.rs:21:5 | 21 | pub tag: Option<Cow<'static, str>>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Expand Down
4 changes: 2 additions & 2 deletions src/datatype/tuple.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,7 +16,7 @@ pub enum TupleType {
/// Be aware `()` is treated specially as `null` in Typescript.
Named {
fields: Vec<DataType>,

Check warning on line 18 in src/datatype/tuple.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct field

warning: missing documentation for a struct field --> src/datatype/tuple.rs:18:9 | 18 | fields: Vec<DataType>, | ^^^^^^^^^^^^^^^^^^^^^
generics: Vec<Cow<'static, str>>,
generics: Vec<GenericType>,

Check warning on line 19 in src/datatype/tuple.rs

View workflow job for this annotation

GitHub Actions / clippy

missing documentation for a struct field

warning: missing documentation for a struct field --> src/datatype/tuple.rs:19:9 | 19 | generics: Vec<GenericType>, | ^^^^^^^^^^^^^^^^^^^^^^^^^^
},
}

Expand Down
5 changes: 3 additions & 2 deletions src/lang/ts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use reserved_terms::*;

use crate::*;

#[allow(missing_docs)]
pub type Result<T> = std::result::Result<T, TsExportError>;
pub type Output = Result<String>;
type Output = Result<String>;

/// Convert a type which implements [`Type`](crate::Type) to a TypeScript string with an export.
///
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d462f4a

Please sign in to comment.