Comments (4)
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.
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.
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.
Closing for now; please feel free to re-open.
from tsync.
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)
- Feature Request: Generate documentation in jsdoc format HOT 3
- error[E0603]: enum `TokenTree` is private HOT 3
- Enums with custom types HOT 3
- Feature request - `undefined` fields using the "optional parameter" `?` syntax HOT 1
- Custom type mapping HOT 1
- [feature request] Honour `#[serde(rename = "...")]` in the generated TypeScript types HOT 2
- [feature request] Tuples HOT 3
- [bug] Numeric enum is not converted correctly on v2.0.0. HOT 1
- Feature request: support tuple structs HOT 4
- [feature request] Support serde flatten HOT 3
- Feature Request: Supporting `const` scalars such as numbers and strings + enums (?) HOT 5
- tsync from other proc-macros? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tsync.