Skip to content

Commit

Permalink
Prevent naming collisions with <T as crate::TS>
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo committed Mar 6, 2025
1 parent 594fa30 commit b2c023b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 88 deletions.
8 changes: 4 additions & 4 deletions macros/src/types/type_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub(crate) fn type_as_struct(attr: &StructAttr, name: &str, type_as: &Type) -> R
dependencies.append_from(type_as);

Ok(DerivedTS {
crate_rename,
inline: quote!(#type_as::inline()),
crate_rename: crate_rename.clone(),
inline: quote!(<#type_as as #crate_rename::TS>::inline()),
inline_flattened: None,
docs: attr.docs.clone(),
dependencies,
Expand All @@ -34,8 +34,8 @@ pub(crate) fn type_as_enum(attr: &EnumAttr, name: &str, type_as: &Type) -> Resul
dependencies.append_from(type_as);

Ok(DerivedTS {
crate_rename,
inline: quote!(#type_as::inline()),
crate_rename: crate_rename.clone(),
inline: quote!(<#type_as as #crate_rename::TS>::inline()),
inline_flattened: None,
docs: attr.docs.clone(),
dependencies,
Expand Down
18 changes: 9 additions & 9 deletions ts-rs/src/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ macro_rules! impl_dummy {

fn name() -> String { String::new() }
fn inline() -> String { String::new() }
fn inline_flattened() -> String { panic!("{} cannot be flattened", Self::name()) }
fn decl() -> String { panic!("{} cannot be declared", Self::name()) }
fn decl_concrete() -> String { panic!("{} cannot be declared", Self::name()) }
fn inline_flattened() -> String { panic!("{} cannot be flattened", <Self as $crate::TS>::name()) }
fn decl() -> String { panic!("{} cannot be declared", <Self as $crate::TS>::name()) }
fn decl_concrete() -> String { panic!("{} cannot be declared", <Self as $crate::TS>::name()) }
}
)*};
}
Expand All @@ -40,13 +40,13 @@ impl<T: TimeZone + 'static> TS for DateTime<T> {
"string".to_owned()
}
fn inline_flattened() -> String {
panic!("{} cannot be flattened", Self::name())
panic!("{} cannot be flattened", <Self as crate::TS>::name())
}
fn decl() -> String {
panic!("{} cannot be declared", Self::name())
panic!("{} cannot be declared", <Self as crate::TS>::name())
}
fn decl_concrete() -> String {
panic!("{} cannot be declared", Self::name())
panic!("{} cannot be declared", <Self as crate::TS>::name())
}
}

Expand All @@ -64,12 +64,12 @@ impl<T: TimeZone + 'static> TS for Date<T> {
"string".to_owned()
}
fn inline_flattened() -> String {
panic!("{} cannot be flattened", Self::name())
panic!("{} cannot be flattened", <Self as crate::TS>::name())
}
fn decl() -> String {
panic!("{} cannot be declared", Self::name())
panic!("{} cannot be declared", <Self as crate::TS>::name())
}
fn decl_concrete() -> String {
panic!("{} cannot be declared", Self::name())
panic!("{} cannot be declared", <Self as crate::TS>::name())
}
}
21 changes: 10 additions & 11 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use crate::TS;
mod error;
mod path;

static EXPORT_PATHS: OnceLock<Mutex<HashMap<PathBuf, HashSet<String>>>> =
OnceLock::new();
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())
Expand Down Expand Up @@ -53,7 +52,7 @@ mod recursive_export {
fn visit<T: TS + 'static + ?Sized>(&mut self) {
// if an error occurred previously, or the type cannot be exported (it's a primitive),
// we return
if self.error.is_some() || T::output_path().is_none() {
if self.error.is_some() || <T as crate::TS>::output_path().is_none() {
return;
}

Expand All @@ -78,7 +77,7 @@ mod recursive_export {
out_dir,
error: None,
};
T::visit_dependencies(&mut visitor);
<T as crate::TS>::visit_dependencies(&mut visitor);

if let Some(e) = visitor.error {
Err(e)
Expand All @@ -92,7 +91,7 @@ mod recursive_export {
pub(crate) fn export_into<T: TS + ?Sized + 'static>(
out_dir: impl AsRef<Path>,
) -> Result<(), ExportError> {
let path = T::output_path()
let path = <T as crate::TS>::output_path()
.ok_or_else(std::any::type_name::<T>)
.map_err(ExportError::CannotBeExported)?;
let path = out_dir.as_ref().join(path);
Expand All @@ -105,7 +104,7 @@ pub(crate) fn export_to<T: TS + ?Sized + 'static, P: AsRef<Path>>(
path: P,
) -> Result<(), ExportError> {
let path = path.as_ref().to_owned();
let type_name = T::ident();
let type_name = <T as crate::TS>::ident();

#[allow(unused_mut)]
let mut buffer = export_to_string::<T>()?;
Expand Down Expand Up @@ -264,7 +263,7 @@ fn merge(original_contents: String, new_contents: String) -> String {
pub(crate) fn export_to_string<T: TS + ?Sized + 'static>() -> Result<String, ExportError> {
let mut buffer = String::with_capacity(1024);
buffer.push_str(NOTE);
generate_imports::<T::WithoutGenerics>(&mut buffer, default_out_dir())?;
generate_imports::<<T as crate::TS>::WithoutGenerics>(&mut buffer, default_out_dir())?;
generate_decl::<T>(&mut buffer);
buffer.push('\n');
Ok(buffer)
Expand All @@ -280,27 +279,27 @@ pub(crate) fn default_out_dir() -> Cow<'static, Path> {
/// Push the declaration of `T`
fn generate_decl<T: TS + ?Sized>(out: &mut String) {
// Type Docs
let docs = &T::DOCS;
let docs = &<T as crate::TS>::DOCS;
if let Some(docs) = docs {
out.push_str(docs);
}

// Type Definition
out.push_str("export ");
out.push_str(&T::decl());
out.push_str(&<T as crate::TS>::decl());
}

/// Push an import statement for all dependencies of `T`.
fn generate_imports<T: TS + ?Sized + 'static>(
out: &mut String,
out_dir: impl AsRef<Path>,
) -> Result<(), ExportError> {
let path = T::output_path()
let path = <T as crate::TS>::output_path()
.ok_or_else(std::any::type_name::<T>)
.map(|x| out_dir.as_ref().join(x))
.map_err(ExportError::CannotBeExported)?;

let deps = T::dependencies();
let deps = <T as crate::TS>::dependencies();
let deduplicated_deps = deps
.iter()
.filter(|dep| dep.type_id != TypeId::of::<T>())
Expand Down
Loading

0 comments on commit b2c023b

Please sign in to comment.