Remove duplicate validation (#404)

* Remove duplicated validation of struct attributes

* Fix clippy warnings

* Revert MSRV break
This commit is contained in:
Gustavo Shigueo
2025-05-29 19:17:43 -03:00
committed by GitHub
parent 40b82771c8
commit 315bed8f3f
4 changed files with 15 additions and 30 deletions

View File

@@ -39,14 +39,14 @@ fn type_def(attr: &StructAttr, ts_name: Expr, fields: &Fields) -> Result<Derived
match fields {
Fields::Named(named) => 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)),
}
}

View File

@@ -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);

View File

@@ -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<DerivedTS> {
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<string, never>".to_owned()),
inline_flattened: None,
@@ -22,14 +21,13 @@ pub(crate) fn empty_object(attr: &StructAttr, ts_name: Expr) -> Result<DerivedTS
ts_name,
concrete: attr.concrete.clone(),
bound: attr.bound.clone(),
})
}
}
pub(crate) fn empty_array(attr: &StructAttr, ts_name: Expr) -> Result<DerivedTS> {
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<DerivedTS>
ts_name,
concrete: attr.concrete.clone(),
bound: attr.bound.clone(),
})
}
}
pub(crate) fn null(attr: &StructAttr, ts_name: Expr) -> Result<DerivedTS> {
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<DerivedTS> {
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(())
}

View File

@@ -21,7 +21,7 @@ mod path;
static EXPORT_PATHS: OnceLock<Mutex<HashMap<PathBuf, HashSet<String>>>> = OnceLock::new();
fn get_export_paths<'a>() -> &'a Mutex<HashMap<PathBuf, HashSet<String>>> {
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 { ")