Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept, argument to export_to other than &'static str #347

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions macros/src/attr/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{parse_assign_from_str, parse_bound, Attr, ContainerAttr, Serde};
use crate::{
attr::{parse_assign_inflection, parse_assign_str, parse_concrete, Inflection},
utils::{parse_attrs, parse_docs},
path::CustomPath,
};

#[derive(Default)]
Expand All @@ -16,7 +17,7 @@ pub struct EnumAttr {
pub rename_all: Option<Inflection>,
pub rename_all_fields: Option<Inflection>,
pub rename: Option<String>,
pub export_to: Option<String>,
pub export_to: Option<CustomPath>,
pub export: bool,
pub docs: String,
pub concrete: HashMap<Ident, Type>,
Expand Down Expand Up @@ -212,7 +213,7 @@ impl_parse! {
"rename" => out.rename = Some(parse_assign_str(input)?),
"rename_all" => out.rename_all = Some(parse_assign_inflection(input)?),
"rename_all_fields" => out.rename_all_fields = Some(parse_assign_inflection(input)?),
"export_to" => out.export_to = Some(parse_assign_str(input)?),
"export_to" => out.export_to = Some(CustomPath::parse(input)?),
"export" => out.export = true,
"tag" => out.tag = Some(parse_assign_str(input)?),
"content" => out.content = Some(parse_assign_str(input)?),
Expand Down
5 changes: 3 additions & 2 deletions macros/src/attr/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
use crate::{
attr::{parse_assign_str, EnumAttr, Inflection, VariantAttr},
utils::{parse_attrs, parse_docs},
path::CustomPath,
};

#[derive(Default, Clone)]
Expand All @@ -18,7 +19,7 @@ pub struct StructAttr {
pub type_override: Option<String>,
pub rename_all: Option<Inflection>,
pub rename: Option<String>,
pub export_to: Option<String>,
pub export_to: Option<CustomPath>,
pub export: bool,
pub tag: Option<String>,
pub docs: String,
Expand Down Expand Up @@ -149,7 +150,7 @@ impl_parse! {
"rename_all" => out.rename_all = Some(parse_assign_inflection(input)?),
"tag" => out.tag = Some(parse_assign_str(input)?),
"export" => out.export = true,
"export_to" => out.export_to = Some(parse_assign_str(input)?),
"export_to" => out.export_to = Some(CustomPath::parse(input)?),
"concrete" => out.concrete = parse_concrete(input)?,
"bound" => out.bound = Some(parse_bound(input)?),
}
Expand Down
21 changes: 12 additions & 9 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use syn::{
WhereClause, WherePredicate,
};

use crate::{deps::Dependencies, utils::format_generics};
use crate::{deps::Dependencies, utils::format_generics, path::CustomPath};

#[macro_use]
mod utils;
mod attr;
mod deps;
mod types;
mod path;

struct DerivedTS {
crate_rename: Path,
Expand All @@ -30,26 +31,28 @@ struct DerivedTS {
bound: Option<Vec<WherePredicate>>,

export: bool,
export_to: Option<String>,
export_to: Option<CustomPath>,
}

impl DerivedTS {
fn into_impl(mut self, rust_ty: Ident, generics: Generics) -> TokenStream {
let export = self
.export
.then(|| self.generate_export_test(&rust_ty, &generics));

let output_path_fn = {
let path = match self.export_to.as_deref() {
Some(dirname) if dirname.ends_with('/') => {
format!("{}{}.ts", dirname, self.ts_name)
}
Some(filename) => filename.to_owned(),
None => format!("{}.ts", self.ts_name),
let (path,path_decl) =

if let Some(cust_path) = &self.export_to{
cust_path.get_path_and_some_decl(&self.ts_name)
} else {
let path = format!("{}.ts", self.ts_name);
(quote!( #path ), None)
};

quote! {
fn output_path() -> Option<&'static std::path::Path> {
#path_decl
Some(std::path::Path::new(#path))
}
}
Expand Down
244 changes: 244 additions & 0 deletions macros/src/path.rs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting feels weird, can you run cargo +nightly fmt?

Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
use quote::{format_ident, quote};
use proc_macro2::TokenStream;
use syn::{
parse::{Parse, ParseStream},
Error, Lit, Ident,LitStr, Token, Result,
};


#[derive(Clone,Debug)]
pub enum CustomPath{
Str(String),
Static(syn::Path),
Fn(syn::Path),
Env(syn::LitStr),
}

type FnOutputPathBody = ( TokenStream, Option<TokenStream> );

impl CustomPath {

pub fn get_path_and_some_decl(&self, ts_name: &String ) -> FnOutputPathBody {

match self {

Self::Str(input) => { Self::str_path(input,ts_name) },

Self::Static(input) => { Self::static_path(input,ts_name) },

Self::Fn(input) => { Self::fn_path(input,ts_name) },

Self::Env(input) => { Self::env_path(input,ts_name) },

}
}

fn str_path( input: &String, ts_name: &String ) -> FnOutputPathBody {

let path =
if input.ends_with('/') {
format!("{}{}.ts", input, ts_name)
} else {
input.to_owned()
};

return (quote!(#path),None);
}

fn static_path( input: &syn::Path, ts_name: &String ) -> FnOutputPathBody {

let path_ident = format_ident!("path");
let stat_path_ident = format_ident!("PATH");
let path_decl = quote! {

static #stat_path_ident: std::sync::OnceLock<String> = std::sync::OnceLock::new();

let #path_ident = #stat_path_ident.get_or_init( ||
{
if #input.ends_with('/') {
format!("{}{}.ts", #input, #ts_name)
} else {
format!("{}",#input)
}
}
);
};

( quote!(#path_ident), Some(path_decl) )
}

fn fn_path( input: &syn::Path, ts_name: &String ) -> FnOutputPathBody {

( quote!{#input (#ts_name)?}, None)
}

fn env_path( input: &LitStr, ts_name: &String ) -> FnOutputPathBody {

let path_ident = format_ident!("path");

let path_decl = quote!{

let #path_ident = if std::env!(#input).ends_with('/') {
std::concat!(std::env!(#input),#ts_name,".ts")
} else {
std::env!(#input)
};
};

( quote!(#path_ident), Some(path_decl) )
}

}

impl Parse for CustomPath {

fn parse(input: ParseStream) -> Result<CustomPath> {
input.parse::<Token![=]>()?;
let span = input.span();

let msg =
"expected arguments for 'export_to':

1) string literal
#[ts(export_to = \"my/path\")]

2) static or constant variable name

#[ts(export_to = MY_STATIC_PATH)]
#[ts(export_to = crate::MY_STATIC_PATH)]

Note: This option is available for Rust 1.7.0 and higher!
Copy link
Collaborator

@gustavo-shigueo gustavo-shigueo Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a typo, did you really mean 1.7.0? The MSRV for ts-rs is 1.63.0, so this note is not needed if you really did mean 1.7.0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a typo std::sync::OnceLock is stabilised in Rust 1.70.0, probably available 1.63. nightly only.

The idiomatic way is to check the Rust version, but #[cfg(version(..))]) isn't stabilised as well.

The worst-case scenario is a compile-time error, informing the user that std::sync::OnceLock is only available in nightly.

Additionally, since ts-rs currently depends on lazy_static, there's a good chance that this dependency will be dropped in favour of the standard library types increasing the MSRV and making this issue less relevant over time.

not sure what to do with it it's up to you really.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you meant 1.70.0 right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course 1.70 )))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the code says 1.7... you're missing a zero

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, it is a typo )))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, in that case, we can just change the MSRV to 1.70.0 and we don't have to worry about it


3) function name of a `Fn(&'static str) -> Option<&'static str>`

#[ts(export_to = get_path)]
#[ts(export_to = crate::get_path)]

Note: This option overrides the original `TS::output_path` logic`!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure if we should allow users to override TS::output_path, this may lead to way too many footguns

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

things that may go wrong with this option:

  • If the path is a directory:
    a) for existing directory, the test will panic with CannotBeExported.
    b) will write into a file with no extension.
  • Anytime the function returns None, the test will panic with CannotBeExported.

Also I've found a way to propagate the error the correct message
and exact span if the provided function is of a different than required type,
by asigning first as a function pointer and then use the pointer to invoke the function:

    fn output_path() -> Option<&'static std::path::Path> {
        let path: fn(&'static str) -> Option<&'static str> = crate::get_path_fn;
        Some(std::path::Path::new(path("ObjC")?))
    } 

it is precise!


4) environment variable name

#[ts(export_to = env(\"MY_ENV_VAR_PATH\"))]

Note: This option is for environment variables defined in the '.cargo/config.toml' file only, accessible through the `env!` macro!
";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message should be a module level const to avoid having a massive string literal inside the function. This makes the function more difficult to read

let get_path = |input: ParseStream| -> Result<(syn::Path,Option<LitStr>)>{
let mut tokens = TokenStream::new();
let mut env_var_str = None;

if input.peek(Token![self]) {
let token = input.parse::<Token![self]>()?;
tokens.extend(quote!(#token));
}
if input.peek(Token![super]) {
let token = input.parse::<Token![super]>()?;
tokens.extend(quote!(#token));
}
if input.peek(Token![crate]) {
let token = input.parse::<Token![crate]>()?;
tokens.extend(quote!(#token));
}
if input.peek(Ident) {
let ident = input.parse::<Ident>()?;
tokens.extend(quote!(#ident));
}

while input.peek(Token![::]) {
let token = input.parse::<Token![::]>()?;
tokens.extend(quote!(#token));

if input.peek(Ident){
let ident = input.parse::<Ident>()?;
tokens.extend(quote!(#ident));
} else { return Err(Error::new(input.span(),"expected ident")) }
}

if input.peek(syn::token::Paren){
let content;
syn::parenthesized!(content in input);
env_var_str = Some(content.parse::<LitStr>()?);
}

Ok((syn::parse2::<syn::Path>(tokens)?,env_var_str))
};


// string literal
if input.peek(LitStr){
if let Ok(lit) = Lit::parse(input){
match lit {
Lit::Str(string) => { return Ok(CustomPath::Str(string.value())); },
_ => { return Err(Error::new(span, msg)); },
}
}
}

match get_path(input) {

Ok((path,arg)) => {

if !path.segments.is_empty(){

if let Some( env_var_str ) = arg {

if path.is_ident("env") {
return Ok(CustomPath::Env(env_var_str));
}

} else {

let last = &path.segments.last().unwrap().ident;

// static or const
if is_screaming_snake_case(&last.to_string()) {
return Ok(CustomPath::Static(path));
}

// function
if is_snake_case(&last.to_string()) {
return Ok(CustomPath::Fn(path));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason I don't really like allowing a function path here is that there is no reliable way to differentiate between a function name and a const/static name. Sure, clippy will complain if a const isn't in SCREAMING_SNAKE_CASE, but it doesn't prevent the code from compiling

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've triyed this scenario on the new design (I've mentioned above, assignment to a function pointer ) and it looks like
it will not compile, which is not bad, this means that the macro is limited to
Rust naming conventions. As long as the mistake is excluded we are ok.

so for lower case static the error will be :

expected fn pointer `fn(&'static str) -> std::option::Option<&'static str>`
    found reference `&'static str`

}
}
return Err(Error::new(span, msg));
},

Err(e) => return Err(Error::new(e.span(), msg)),
}
}
}


// These functions mimic Rust's naming conventions for
// statics, constants, and function .
// To be replaced with proper, more robust validation.

fn is_screaming_snake_case(s: &str) -> bool {

if s.is_empty() || s.starts_with('_') || s.ends_with('_') || s.contains("__") {
return false;
}

for c in s.chars() {
if !c.is_ascii_uppercase() && c != '_' {
return false;
}
}

true
}

fn is_snake_case(s: &str) -> bool {

if s.is_empty() || s.starts_with('_') {
return false;
}

for c in s.chars() {
if !c.is_ascii_lowercase() && c != '_' {
return false;
}
}

true
}