twilight-rs / twilight

Powerful, flexible, and scalable ecosystem of Rust libraries for the Discord API.
https://discord.gg/twilight-rs
ISC License
656 stars 130 forks source link

Formatter macros #2279

Open ghost opened 12 months ago

ghost commented 12 months ago

What would you like implemented? What do you want that Twilight lacks?

I would like to see the addition of macros like bold!, heading!, code_block!, and spoiler! in the Twilight library. These macros should allow developers to easily format text for Discord messages without the need for manual string manipulation. This feature would enhance the library's capabilities and improve the developer experience.

Are you willing to help towards contributing this feature?

Yes, I am enthusiastic about contributing to the implementation of these macros. I am willing to assist with code contributions, documentation updates, and any other tasks necessary to bring this feature to fruition.

Is there any other information that we should know?

Reference Documentation: You can find more information about Discord message formatting in the official Discord Developer Portal documentation. This reference will help users understand the standard formatting options used in Discord and how the proposed macros would align with these conventions.

Thank you for considering this feature request. I believe it will be a valuable addition to Twilight and greatly benefit the broader Discord bot development community.

laralove143 commented 12 months ago

Thank you for this! It could be part of twilight-util under another feature gate.

ghost commented 12 months ago

I think so too.

AEnterprise commented 12 months ago

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

ghost commented 12 months ago

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

IMHO;

If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.

laralove143 commented 12 months ago

Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?

ghost commented 12 months ago

Would you be able to use a dynamic strings and also inline it with string literals with the same macro though?

There are ways to achieve similar functionality using Rust macros by using token manipulation and code generation. You can generate code or tokens based on certain input, but this is not the same as creating truly dynamic strings within the macro.

macro_rules! bold {
    ($content:expr) => {
        format!("**{}**", $content)
    };
}
ghost commented 12 months ago

But yes, I agree that macros tend to be less general-purpose, and functions can often serve as more versatile and maintainable alternatives. Functions are typically more suitable for common tasks and can be used in a wider range of scenarios. However, macros still have their place in Rust for code generation, metaprogramming, and other compile-time tasks where their capabilities shine. I still believe that macros are useful and can be used effectively.

AEnterprise commented 12 months ago

just strapping an #[inline] on the function will cause it to be inlined, thus making it functionally equivalent to the macro, but much more readable imo

macro's are useful but i don't think they offer any benefit here over just a macro

Gelbpunkt commented 12 months ago

why would this need to be a macro and not just a regular function (that would probably be inlined anyways by the compiler)?

IMHO;

* Macros are evaluated at compile time, which means that the formatting code is processed before the program runs. In contrast, regular functions are executed at runtime. This compile-time evaluation can lead to performance benefits, especially when formatting is used extensively, as macros can potentially be inlined by the compiler, resulting in more efficient code.

* When formatting is performed through functions, there is an overhead associated with function calls. While modern compilers can inline small functions, macros ensure that there is no function call overhead at all, which can be important for frequently used formatting operations in Discord bot messages.

If there's any point where I'm mistaken, I would appreciate it if you could correct me. Thank you for reading.

You will get zero performance benefits for using format!. The std::fmt APIs are much more overhead than just prepending and appending the string. It also won't be evaluated at compile time since the APIs aren't const. What you probably want is to use concat!.

ghost commented 12 months ago

Yes, I agree. Thank you for sharing your perspective. So functions are the best approach to these requirements, right?