Skip to content

Commit

Permalink
rename TypeMap to TypeCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Beaumont authored and Oscar Beaumont committed Dec 4, 2024
1 parent 93bade7 commit 1220b8f
Show file tree
Hide file tree
Showing 33 changed files with 235 additions and 187 deletions.
7 changes: 5 additions & 2 deletions specta-go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
html_favicon_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png"
)]

use specta::{datatype::DataType, Generics, Type, TypeMap};
use specta::{datatype::DataType, Generics, Type, TypeCollection};

/// TODO
pub fn export<T: Type>() -> Result<String, String> {
datatype(&T::inline(&mut TypeMap::default(), Generics::Definition))
datatype(&T::inline(
&mut TypeCollection::default(),
Generics::Definition,
))
}

fn datatype(t: &DataType) -> Result<String, String> {
Expand Down
4 changes: 2 additions & 2 deletions specta-jsdoc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use std::{borrow::Cow, path::Path};

use specta::{Language, TypeMap};
use specta::{Language, TypeCollection};
use specta_typescript::{BigIntExportBehavior, CommentFormatterFn, FormatterFn};

// TODO: Ensure this is up to our `Typescript` exporters standards.
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Language for JSDoc {
type Error = specta_typescript::ExportError; // TODO: Custom error type

// TODO: Make this properly export JSDoc
fn export(&self, _type_map: &TypeMap) -> Result<String, Self::Error> {
fn export(&self, _type_map: &TypeCollection) -> Result<String, Self::Error> {
todo!("Coming soon...");
// let mut out = self.0.header.to_string();
// if !self.0.remove_default_header {
Expand Down
7 changes: 5 additions & 2 deletions specta-kotlin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

use specta::{
datatype::{DataType, PrimitiveType},
Generics, Type, TypeMap,
Generics, Type, TypeCollection,
};

/// TODO
pub fn export<T: Type>() -> Result<String, String> {
datatype(&T::inline(&mut TypeMap::default(), Generics::Definition))
datatype(&T::inline(
&mut TypeCollection::default(),
Generics::Definition,
))
}

fn datatype(t: &DataType) -> Result<String, String> {
Expand Down
2 changes: 1 addition & 1 deletion specta-macros/src/specta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn attribute(item: proc_macro::TokenStream) -> syn::Result<proc_macro::Token
macro_rules! #wrapper {
// We take in `$function` from the invocation so we have `fn_name::<concrete_generics_types>`
(@export_fn; $function:path) => {{
fn export(type_map: &mut #crate_ref::TypeMap) -> #crate_ref::datatype::Function {
fn export(type_map: &mut #crate_ref::TypeCollection) -> #crate_ref::datatype::Function {
#crate_ref::internal::get_fn_datatype(
$function as fn(#(#arg_signatures),*) -> _,
#function_asyncness,
Expand Down
8 changes: 4 additions & 4 deletions specta-macros/src/type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt

#[automatically_derived]
#type_impl_heading {
fn inline(type_map: &mut #crate_ref::TypeMap, generics: #crate_ref::Generics) -> #crate_ref::datatype::DataType {
fn inline(type_map: &mut #crate_ref::TypeCollection, generics: #crate_ref::Generics) -> #crate_ref::datatype::DataType {
let generics = match generics {
#crate_ref::Generics::Definition => DEFINITION_GENERICS,
#crate_ref::Generics::Provided(generics) => generics,
Expand All @@ -147,7 +147,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt
#inlines
}

fn reference(type_map: &mut #crate_ref::TypeMap, generics: &[#crate_ref::datatype::DataType]) -> #crate_ref::datatype::reference::Reference {
fn reference(type_map: &mut #crate_ref::TypeCollection, generics: &[#crate_ref::datatype::DataType]) -> #crate_ref::datatype::reference::Reference {
#reference
}
}
Expand All @@ -158,7 +158,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt
#sid
}

fn named_data_type(type_map: &mut #crate_ref::TypeMap, generics: &[#crate_ref::datatype::DataType]) -> #crate_ref::datatype::NamedDataType {
fn named_data_type(type_map: &mut #crate_ref::TypeCollection, generics: &[#crate_ref::datatype::DataType]) -> #crate_ref::datatype::NamedDataType {
#crate_ref::internal::construct::named_data_type(
#name.into(),
#comments.into(),
Expand All @@ -169,7 +169,7 @@ pub fn derive(input: proc_macro::TokenStream) -> syn::Result<proc_macro::TokenSt
)
}

fn definition_named_data_type(type_map: &mut #crate_ref::TypeMap) -> #crate_ref::datatype::NamedDataType {
fn definition_named_data_type(type_map: &mut #crate_ref::TypeCollection) -> #crate_ref::datatype::NamedDataType {
#crate_ref::internal::construct::named_data_type(
#name.into(),
#comments.into(),
Expand Down
7 changes: 5 additions & 2 deletions specta-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
html_favicon_url = "https://github.com/oscartbeaumont/specta/raw/main/.github/logo-128.png"
)]

use specta::{datatype::DataType, Generics, Type, TypeMap};
use specta::{datatype::DataType, Generics, Type, TypeCollection};

/// TODO
pub fn export<T: Type>() -> Result<String, String> {
datatype(&T::inline(&mut TypeMap::default(), Generics::Definition))
datatype(&T::inline(
&mut TypeCollection::default(),
Generics::Definition,
))
}

fn datatype(t: &DataType) -> Result<String, String> {
Expand Down
14 changes: 7 additions & 7 deletions specta-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use specta::{
DataType, EnumRepr, EnumType, EnumVariants, LiteralType, PrimitiveType, StructFields,
},
internal::{resolve_generics, skip_fields, skip_fields_named},
SpectaID, TypeMap,
SpectaID, TypeCollection,
};

// TODO: The error should show a path to the type causing the issue like the BigInt error reporting.
Expand All @@ -32,13 +32,13 @@ pub enum SerdeError {
/// Check that a [DataType] is a valid for Serde.
///
/// This can be used by exporters which wanna do export-time checks that all types are compatible with Serde formats.
pub fn is_valid_ty(dt: &DataType, type_map: &TypeMap) -> Result<(), SerdeError> {
pub fn is_valid_ty(dt: &DataType, type_map: &TypeCollection) -> Result<(), SerdeError> {
is_valid_ty_internal(dt, type_map, &mut Default::default())
}

fn is_valid_ty_internal(
dt: &DataType,
type_map: &TypeMap,
type_map: &TypeCollection,
checked_references: &mut HashSet<SpectaID>,
) -> Result<(), SerdeError> {
match dt {
Expand Down Expand Up @@ -106,7 +106,7 @@ fn is_valid_ty_internal(
}

// Typescript: Must be assignable to `string | number | symbol` says Typescript.
fn is_valid_map_key(key_ty: &DataType, type_map: &TypeMap) -> Result<(), SerdeError> {
fn is_valid_map_key(key_ty: &DataType, type_map: &TypeCollection) -> Result<(), SerdeError> {
match key_ty {
DataType::Any => Ok(()),
DataType::Primitive(ty) => match ty {
Expand Down Expand Up @@ -171,7 +171,7 @@ fn is_valid_map_key(key_ty: &DataType, type_map: &TypeMap) -> Result<(), SerdeEr
}

// Serde does not allow serializing a variant of certain types of enum's.
fn validate_enum(e: &EnumType, type_map: &TypeMap) -> Result<(), SerdeError> {
fn validate_enum(e: &EnumType, type_map: &TypeCollection) -> Result<(), SerdeError> {
// You can't `#[serde(skip)]` your way to an empty enum.
let valid_variants = e.variants().iter().filter(|(_, v)| !v.skip()).count();
if valid_variants == 0 && !e.variants().is_empty() {
Expand All @@ -187,7 +187,7 @@ fn validate_enum(e: &EnumType, type_map: &TypeMap) -> Result<(), SerdeError> {
}

// Checks for specially internally tagged enums.
fn validate_internally_tag_enum(e: &EnumType, type_map: &TypeMap) -> Result<(), SerdeError> {
fn validate_internally_tag_enum(e: &EnumType, type_map: &TypeCollection) -> Result<(), SerdeError> {
for (_variant_name, variant) in e.variants() {
match &variant.inner() {
EnumVariants::Unit => {}
Expand Down Expand Up @@ -215,7 +215,7 @@ fn validate_internally_tag_enum(e: &EnumType, type_map: &TypeMap) -> Result<(),
// Which makes sense when you can't represent `{ "type": "A" } & string` in a single JSON value.
fn validate_internally_tag_enum_datatype(
ty: &DataType,
type_map: &TypeMap,
type_map: &TypeCollection,
) -> Result<(), SerdeError> {
match ty {
// `serde_json::Any` can be *technically* be either valid or invalid based on the actual data but we are being strict and reject it.
Expand Down
7 changes: 5 additions & 2 deletions specta-swift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

use specta::{
datatype::{DataType, PrimitiveType},
Generics, Type, TypeMap,
Generics, Type, TypeCollection,
};

/// TODO
pub fn export<T: Type>() -> Result<String, String> {
datatype(&T::inline(&mut TypeMap::default(), Generics::Definition))
datatype(&T::inline(
&mut TypeCollection::default(),
Generics::Definition,
))
}

fn datatype(t: &DataType) -> Result<String, String> {
Expand Down
10 changes: 7 additions & 3 deletions specta-typescript/src/js_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use std::borrow::Borrow;

use specta::{
datatype::{DeprecatedType, GenericType},
TypeMap,
TypeCollection,
};
use typescript::CommentFormatterArgs;

use super::*;

pub fn typedef_named_datatype(cfg: &Typescript, typ: &NamedDataType, type_map: &TypeMap) -> Output {
pub fn typedef_named_datatype(
cfg: &Typescript,
typ: &NamedDataType,
type_map: &TypeCollection,
) -> Output {
typedef_named_datatype_inner(
&ExportContext {
cfg,
Expand All @@ -24,7 +28,7 @@ pub fn typedef_named_datatype(cfg: &Typescript, typ: &NamedDataType, type_map: &
fn typedef_named_datatype_inner(
ctx: &ExportContext,
typ: &NamedDataType,
type_map: &TypeMap,
type_map: &TypeCollection,
) -> Output {
let name = typ.name();
let docs = typ.docs();
Expand Down
42 changes: 27 additions & 15 deletions specta-typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use specta::datatype::{
};
use specta::{
internal::{detect_duplicate_type_names, skip_fields, skip_fields_named, NonSkipField},
Generics, NamedType, Type, TypeMap,
Generics, NamedType, Type, TypeCollection,
};
use specta_serde::is_valid_ty;

Expand All @@ -47,7 +47,7 @@ pub fn export_ref<T: NamedType>(_: &T, conf: &Typescript) -> Output {
///
/// Eg. `export type Foo = { demo: string; };`
pub fn export<T: NamedType>(conf: &Typescript) -> Output {
let mut type_map = TypeMap::default();
let mut type_map = TypeCollection::default();
let named_data_type = T::definition_named_data_type(&mut type_map);
is_valid_ty(&named_data_type.inner, &type_map)?;
let result = export_named_datatype(conf, &named_data_type, &type_map);
Expand All @@ -70,7 +70,7 @@ pub fn inline_ref<T: Type>(_: &T, conf: &Typescript) -> Output {
///
/// Eg. `{ demo: string; };`
pub fn inline<T: Type>(conf: &Typescript) -> Output {
let mut type_map = TypeMap::default();
let mut type_map = TypeCollection::default();
let ty = T::inline(&mut type_map, Generics::NONE);
is_valid_ty(&ty, &type_map)?;
let result = datatype(conf, &FunctionResultVariant::Value(ty.clone()), &type_map);
Expand All @@ -85,7 +85,11 @@ pub fn inline<T: Type>(conf: &Typescript) -> Output {
/// Convert a DataType to a TypeScript string
///
/// Eg. `export Name = { demo: string; }`
pub fn export_named_datatype(conf: &Typescript, typ: &NamedDataType, type_map: &TypeMap) -> Output {
pub fn export_named_datatype(
conf: &Typescript,
typ: &NamedDataType,
type_map: &TypeCollection,
) -> Output {
// TODO: Duplicate type name detection?

is_valid_ty(&typ.inner, type_map)?;
Expand Down Expand Up @@ -126,7 +130,11 @@ fn inner_comments(
format!("{prefix}{comments}{other}")
}

fn export_datatype_inner(ctx: ExportContext, typ: &NamedDataType, type_map: &TypeMap) -> Output {
fn export_datatype_inner(
ctx: ExportContext,
typ: &NamedDataType,
type_map: &TypeCollection,
) -> Output {
let name = typ.name();
let docs = typ.docs();
let ext = typ.ext();
Expand Down Expand Up @@ -166,7 +174,11 @@ fn export_datatype_inner(ctx: ExportContext, typ: &NamedDataType, type_map: &Typ
/// Convert a DataType to a TypeScript string
///
/// Eg. `{ demo: string; }`
pub fn datatype(conf: &Typescript, typ: &FunctionResultVariant, type_map: &TypeMap) -> Output {
pub fn datatype(
conf: &Typescript,
typ: &FunctionResultVariant,
type_map: &TypeCollection,
) -> Output {
// TODO: Duplicate type name detection?

let mut s = String::new();
Expand All @@ -192,7 +204,7 @@ macro_rules! primitive_def {
pub(crate) fn datatype_inner(
ctx: ExportContext,
typ: &FunctionResultVariant,
type_map: &TypeMap,
type_map: &TypeCollection,
s: &mut String,
) -> Result<()> {
let typ = match typ {
Expand Down Expand Up @@ -390,7 +402,7 @@ pub(crate) fn datatype_inner(
fn unnamed_fields_datatype(
ctx: ExportContext,
fields: &[NonSkipField],
type_map: &TypeMap,
type_map: &TypeCollection,
s: &mut String,
) -> Result<()> {
Ok(match fields {
Expand Down Expand Up @@ -439,7 +451,7 @@ fn unnamed_fields_datatype(
})
}

fn tuple_datatype(ctx: ExportContext, tuple: &TupleType, type_map: &TypeMap) -> Output {
fn tuple_datatype(ctx: ExportContext, tuple: &TupleType, type_map: &TypeCollection) -> Output {
match &tuple.elements()[..] {
[] => Ok(NULL.to_string()),
tys => Ok(format!(
Expand All @@ -465,7 +477,7 @@ fn struct_datatype(
ctx: ExportContext,
key: &str,
strct: &StructType,
type_map: &TypeMap,
type_map: &TypeCollection,
s: &mut String,
) -> Result<()> {
Ok(match &strct.fields() {
Expand Down Expand Up @@ -550,7 +562,7 @@ fn struct_datatype(

fn enum_variant_datatype(
ctx: ExportContext,
type_map: &TypeMap,
type_map: &TypeCollection,
name: Cow<'static, str>,
variant: &EnumVariant,
) -> Result<Option<String>> {
Expand Down Expand Up @@ -630,7 +642,7 @@ fn enum_variant_datatype(
fn enum_datatype(
ctx: ExportContext,
e: &EnumType,
type_map: &TypeMap,
type_map: &TypeCollection,
s: &mut String,
) -> Result<()> {
if e.variants().is_empty() {
Expand Down Expand Up @@ -804,7 +816,7 @@ fn object_field_to_ts(
ctx: ExportContext,
key: Cow<'static, str>,
(field, ty): NonSkipField,
type_map: &TypeMap,
type_map: &TypeCollection,
s: &mut String,
) -> Result<()> {
let field_name_safe = sanitise_key(key, false);
Expand Down Expand Up @@ -876,7 +888,7 @@ pub(crate) fn sanitise_type_name(ctx: ExportContext, loc: NamedLocation, ident:
fn validate_type_for_tagged_intersection(
ctx: ExportContext,
ty: DataType,
type_map: &TypeMap,
type_map: &TypeCollection,
) -> Result<bool> {
match ty {
DataType::Any
Expand Down Expand Up @@ -934,7 +946,7 @@ fn validate_type_for_tagged_intersection(
ctx,
type_map
.get(r.sid())
.expect("TypeMap should have been populated by now")
.expect("TypeCollection should have been populated by now")
.inner
.clone(),
type_map,
Expand Down
4 changes: 2 additions & 2 deletions specta-typescript/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
path::{Path, PathBuf},
};

use specta::{datatype::DeprecatedType, Language, TypeMap};
use specta::{datatype::DeprecatedType, Language, TypeCollection};
use specta_serde::is_valid_ty;

use crate::{comments, detect_duplicate_type_names, export_named_datatype, ExportError};
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Typescript {
impl Language for Typescript {
type Error = ExportError;

fn export(&self, type_map: &TypeMap) -> Result<String, Self::Error> {
fn export(&self, type_map: &TypeCollection) -> Result<String, Self::Error> {
let mut out = self.header.to_string();
if !self.remove_default_header {
out += "// This file has been generated by Specta. DO NOT EDIT.\n\n";
Expand Down
Loading

0 comments on commit 1220b8f

Please sign in to comment.