diff --git a/macros/src/types/mod.rs b/macros/src/types/mod.rs index 417ccc2..f50f784 100644 --- a/macros/src/types/mod.rs +++ b/macros/src/types/mod.rs @@ -39,14 +39,14 @@ fn type_def(attr: &StructAttr, ts_name: Expr, fields: &Fields) -> Result match named.named.len() { - 0 if attr.tag.is_none() => unit::empty_object(attr, ts_name), + 0 if attr.tag.is_none() => Ok(unit::empty_object(attr, ts_name)), _ => named::named(attr, ts_name, named), }, Fields::Unnamed(unnamed) => match unnamed.unnamed.len() { - 0 => unit::empty_array(attr, ts_name), + 0 => Ok(unit::empty_array(attr, ts_name)), 1 => newtype::newtype(attr, ts_name, unnamed), _ => tuple::tuple(attr, ts_name, unnamed), }, - Fields::Unit => unit::null(attr, ts_name), + Fields::Unit => Ok(unit::null(attr, ts_name)), } } diff --git a/macros/src/types/newtype.rs b/macros/src/types/newtype.rs index f2863b0..722dde6 100644 --- a/macros/src/types/newtype.rs +++ b/macros/src/types/newtype.rs @@ -20,7 +20,7 @@ pub(crate) fn newtype( let crate_rename = attr.crate_rename(); if field_attr.skip { - return super::unit::null(attr, ts_name); + return Ok(super::unit::null(attr, ts_name)); } let inner_ty = field_attr.type_as(&inner.ty); diff --git a/macros/src/types/unit.rs b/macros/src/types/unit.rs index 0136635..740e5fb 100644 --- a/macros/src/types/unit.rs +++ b/macros/src/types/unit.rs @@ -1,5 +1,5 @@ use quote::quote; -use syn::{Expr, Result}; +use syn::Expr; use crate::{ attr::{ContainerAttr, StructAttr}, @@ -7,11 +7,10 @@ use crate::{ DerivedTS, }; -pub(crate) fn empty_object(attr: &StructAttr, ts_name: Expr) -> Result { - check_attributes(attr)?; +pub(crate) fn empty_object(attr: &StructAttr, ts_name: Expr) -> DerivedTS { let crate_rename = attr.crate_rename(); - Ok(DerivedTS { + DerivedTS { crate_rename: crate_rename.clone(), inline: quote!("Record".to_owned()), inline_flattened: None, @@ -22,14 +21,13 @@ pub(crate) fn empty_object(attr: &StructAttr, ts_name: Expr) -> Result Result { - check_attributes(attr)?; +pub(crate) fn empty_array(attr: &StructAttr, ts_name: Expr) -> DerivedTS { let crate_rename = attr.crate_rename(); - Ok(DerivedTS { + DerivedTS { crate_rename: crate_rename.clone(), inline: quote!("never[]".to_owned()), inline_flattened: None, @@ -40,14 +38,13 @@ pub(crate) fn empty_array(attr: &StructAttr, ts_name: Expr) -> Result ts_name, concrete: attr.concrete.clone(), bound: attr.bound.clone(), - }) + } } -pub(crate) fn null(attr: &StructAttr, ts_name: Expr) -> Result { - check_attributes(attr)?; +pub(crate) fn null(attr: &StructAttr, ts_name: Expr) -> DerivedTS { let crate_rename = attr.crate_rename(); - Ok(DerivedTS { + DerivedTS { crate_rename: crate_rename.clone(), inline: quote!("null".to_owned()), inline_flattened: None, @@ -58,17 +55,5 @@ pub(crate) fn null(attr: &StructAttr, ts_name: Expr) -> Result { ts_name, concrete: attr.concrete.clone(), bound: attr.bound.clone(), - }) -} - -fn check_attributes(attr: &StructAttr) -> Result<()> { - if attr.rename_all.is_some() { - syn_err!("`rename_all` is not applicable to unit structs"); } - - if attr.tag.is_some() { - syn_err!("`tag` is not applicable to unit structs"); - } - - Ok(()) } diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 420799d..9f3936c 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -21,7 +21,7 @@ mod path; static EXPORT_PATHS: OnceLock>>> = OnceLock::new(); fn get_export_paths<'a>() -> &'a Mutex>> { - EXPORT_PATHS.get_or_init(|| Default::default()) + EXPORT_PATHS.get_or_init(Default::default) } const NOTE: &str = "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n"; @@ -201,7 +201,7 @@ fn merge(original_contents: String, new_contents: String) -> String { .chain(new_header.lines().skip(1)) .map(|line| { let (import, from) = line.split_once(" from ").unwrap(); - let path = from.trim_start_matches('"').trim_end_matches(&['"', ';']); + let path = from.trim_start_matches('"').trim_end_matches(['"', ';']); let types = import .trim_start_matches("import type { ")