Hello,
I'm the author of crate color-print, and I got an interesting issue about nesting color_print::cstr!()
with indoc::indoc!()
.
To my actual understanding about Rust proc macros, it's not possible to do that, because the indoc
crate is not accessible from the companion crate color-print-proc-macro : even if I would test for a match between indoc!(...)
and the token stream, I could not "call" indoc!()
because it's not accessible from the proc macro crate. But maybe I'm missing something?
My main questions are:
- Is there a solution to solve this kind of problem?
- (Crate design question) Otherwise, do you consider as a good choice to add a
indoc
feature as explained below in my anwser (see " Possible solution for a future version" below)?
Any help is welcome:) Please answer with simple english.
I copy/pasted below the OP's issue, and my initial anwser (extract):
Copy/paste of the issue:
I was hoping to use this crate together with clap
as per their documentation.
But my help messages are huge and I'm already using indoc crate to wrap them.
Unfortunately it seems that two crates don't combine:
# Error: Format string must be a string literal
let s = color_print::cstr!(indoc::indoc!(
r#"
My message
"#
));
Extract of my first response:
The problem...
Actually, cstr!()
macro is implemented as a proc macro, and as far as i know, nesting proc macro calls is not possible, because the indoc
crate is an external crate, not accessible from the color-print-proc-macro
crate.
I may be wrong about this (I will take a look on it a bit deeper); but I'm quite confident about it because indoc!()
also raises an error when calling indoc!(cstr!(""))
(with a more readable error message).
Possible solution for a future version
indoc
is a very popular crate, and using it in with cstr!()
could be useful for other people; so maybe a indoc
feature could be added to color-print
, which would allow the following special syntax: cstr!(indoc!("..."))
...
This solution is far from perfect, but it's not really hard to implement and could solve this kind of issue at least for indoc
.