Skip to content

Commit

Permalink
named_datatype (#122)
Browse files Browse the repository at this point in the history
* named_datatype

* GenericType

* docs better

---------

Co-authored-by: Oscar Beaumont <[email protected]>
  • Loading branch information
Brendonovich and oscartbeaumont authored Aug 31, 2023
1 parent 841afee commit d020af6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 111 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
43 changes: 41 additions & 2 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 @@ -109,6 +112,32 @@ pub enum NamedDataTypeItem {
Tuple(TupleType),
}

impl NamedDataTypeItem {
/// Converts a [`NamedDataTypeItem`] into a [`DataType`]
pub fn datatype(self) -> DataType {
match self {
Self::Object(o) => o.into(),
Self::Enum(e) => e.into(),
Self::Tuple(t) => t.into(),
}
}

/// Returns the generics arguments for the type
pub fn generics(&self) -> Vec<GenericType> {
match self {
// Named struct
Self::Object(ObjectType { generics, .. }) => generics.clone(),
// Enum
Self::Enum(e) => e.generics().clone(),
// Struct with unnamed fields
Self::Tuple(tuple) => match tuple {
TupleType::Unnamed => vec![],
TupleType::Named { generics, .. } => generics.clone(),
},
}
}
}

/// A reference to a [`DataType`] that can be used before a type is resolved in order to
/// support recursive types without causing an infinite loop.
///
Expand All @@ -126,11 +155,21 @@ pub struct DataTypeReference {
pub generics: Vec<DataType>,
}

/// A generic parameter to another type.
/// A generic ("placeholder") argument to a Specta-enabled type.
///
/// A generic does not hold a specific type instead it acts as a slot where a type can be provided when referencing this type.
///
/// A `GenericType` holds the identifier of the generic. Eg. Given a generic type `struct A<T>(T);` the generics will be represented as `vec![GenericType("A".into())]`
#[derive(Debug, Clone, PartialEq)]
#[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
Loading

0 comments on commit d020af6

Please sign in to comment.