Code Monkey home page Code Monkey logo

Comments (4)

code-mart avatar code-mart commented on August 18, 2024 1

Thanks @Wulf , I achieved this in a different way in my project. In order to evaluate macros (including concatenation of static strings, which is a basic feature for string constants), tsync would need to be working with a compiled project, rather than working with the source code, like tsync does now. This is because there is not a good, stable way (that I have found) to evaluate macros before compilation.

Additionally, with tsync's approach of using attribute-like macros, #[tsync], I could not find an elegant way to run the tsync code as part of the compiled project, without, for example, adding a function call to main.

I ended up creating a separate "constant-definer" crate, where I could include the constants in my rust code, and I would run all my export!() function-like macros to export the constants to typescript. This is basically what it looks like:

// Declare a static string.
lazy_static! {
    static ref MY_STRING: String = "some_string".to_string();
    static ref CONCATENATED: String = format!("{}_is_concatenated", MY_STRING.as_str());
}

// Convert static string to a typescript declaration and write to file.
macro_rules! export {
    ($name:ident, $file:ident) => (
        write!($file, "export const {}: string = \"{}\";\n\n", stringify!($name), ($name).as_str());
    )
}


pub fn export_all() {
    let mut file = File::create(EXPORT_FILENAME.as_str()).unwrap();
    write!(file, "// DO NOT EDIT. THIS FILE WAS AUTOMATICALLY GENERATED.\n\n");

    export!(MY_STRING, file);
    export!(CONCATENATED, file);
}

fn main() {
    export_all();
}

There may be a way to integrate this feature into tsync by using tsync as a library, as you have already implemented. Please share any thoughts!

from tsync.

AravindPrabhs avatar AravindPrabhs commented on August 18, 2024

Ah I overlooked this completely since for my use case I actually had a #[cfg(false)] around the const I wanted to tsync. So the code never actually got compiled.
I don't think this would be possible with the current tsync implementation I think would have to be extended for expressions maybe (let).

from tsync.

Wulf avatar Wulf commented on August 18, 2024

Thanks @code-mart for reporting this and @AravindPrabhs for the details. I've added a little comment in the README linking to this issue so others can be aware. We may need to remove this if it's not achievable.

image

Closing for now; please feel free to re-open.

from tsync.

Wulf avatar Wulf commented on August 18, 2024

Great solution @code-mart! Thank you for sharing. That's definitely an elegant way to go about static values.

There's one adaptation which may make it easier to integrate with tsync. It would be to have the #[tsync] macro keep track of all statics using something like inventory. That way, before the tsync binary is run, it will have a list of your values and their identifiers all ready without needing a compilation step in between. We could either force users to specify the identifier's : type (by raising an error if not specified) or guess the type using serialization.

Definitely a great feature to add! This alongside @AravindPrabhs's cargo expansion would make tsync more powerful than it was initially intended to be 😅

🙌

from tsync.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.