Factor out common token generation in fluent_messages
.
The failure and success cases are similar enough that they can share code.
This commit is contained in:
parent
e0d7ed1f45
commit
5f4177a786
1 changed files with 20 additions and 38 deletions
|
@ -40,26 +40,35 @@ fn invocation_relative_path_to_absolute(span: Span, path: &str) -> PathBuf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tokens to be returned when the macro cannot proceed.
|
/// Final tokens.
|
||||||
fn failed(crate_name: &Ident) -> proc_macro::TokenStream {
|
fn finish(body: TokenStream, resource: TokenStream) -> proc_macro::TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
pub static DEFAULT_LOCALE_RESOURCE: &'static str = "";
|
/// Raw content of Fluent resource for this crate, generated by `fluent_messages` macro,
|
||||||
|
/// imported by `rustc_driver` to include all crates' resources in one bundle.
|
||||||
|
pub static DEFAULT_LOCALE_RESOURCE: &'static str = #resource;
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
/// Auto-generated constants for type-checked references to Fluent messages.
|
||||||
pub(crate) mod fluent_generated {
|
pub(crate) mod fluent_generated {
|
||||||
pub mod #crate_name {
|
#body
|
||||||
}
|
|
||||||
|
|
||||||
|
/// Constants expected to exist by the diagnostic derive macros to use as default Fluent
|
||||||
|
/// identifiers for different subdiagnostic kinds.
|
||||||
pub mod _subdiag {
|
pub mod _subdiag {
|
||||||
|
/// Default for `#[help]`
|
||||||
pub const help: crate::SubdiagnosticMessage =
|
pub const help: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("help"));
|
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("help"));
|
||||||
|
/// Default for `#[note]`
|
||||||
pub const note: crate::SubdiagnosticMessage =
|
pub const note: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
|
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
|
||||||
|
/// Default for `#[warn]`
|
||||||
pub const warn: crate::SubdiagnosticMessage =
|
pub const warn: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("warn"));
|
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("warn"));
|
||||||
|
/// Default for `#[label]`
|
||||||
pub const label: crate::SubdiagnosticMessage =
|
pub const label: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
|
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
|
||||||
|
/// Default for `#[suggestion]`
|
||||||
pub const suggestion: crate::SubdiagnosticMessage =
|
pub const suggestion: crate::SubdiagnosticMessage =
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("suggestion"));
|
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("suggestion"));
|
||||||
}
|
}
|
||||||
|
@ -68,6 +77,11 @@ fn failed(crate_name: &Ident) -> proc_macro::TokenStream {
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Tokens to be returned when the macro cannot proceed.
|
||||||
|
fn failed(crate_name: &Ident) -> proc_macro::TokenStream {
|
||||||
|
finish(quote! { pub mod #crate_name {} }, quote! { "" })
|
||||||
|
}
|
||||||
|
|
||||||
/// See [rustc_fluent_macro::fluent_messages].
|
/// See [rustc_fluent_macro::fluent_messages].
|
||||||
pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let crate_name = std::env::var("CARGO_PKG_NAME")
|
let crate_name = std::env::var("CARGO_PKG_NAME")
|
||||||
|
@ -311,39 +325,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
finish(constants, quote! { include_str!(#relative_ftl_path) })
|
||||||
/// Raw content of Fluent resource for this crate, generated by `fluent_messages` macro,
|
|
||||||
/// imported by `rustc_driver` to include all crates' resources in one bundle.
|
|
||||||
pub static DEFAULT_LOCALE_RESOURCE: &'static str = include_str!(#relative_ftl_path);
|
|
||||||
|
|
||||||
#[allow(non_upper_case_globals)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
/// Auto-generated constants for type-checked references to Fluent messages.
|
|
||||||
pub(crate) mod fluent_generated {
|
|
||||||
#constants
|
|
||||||
|
|
||||||
/// Constants expected to exist by the diagnostic derive macros to use as default Fluent
|
|
||||||
/// identifiers for different subdiagnostic kinds.
|
|
||||||
pub mod _subdiag {
|
|
||||||
/// Default for `#[help]`
|
|
||||||
pub const help: crate::SubdiagnosticMessage =
|
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("help"));
|
|
||||||
/// Default for `#[note]`
|
|
||||||
pub const note: crate::SubdiagnosticMessage =
|
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
|
|
||||||
/// Default for `#[warn]`
|
|
||||||
pub const warn: crate::SubdiagnosticMessage =
|
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("warn"));
|
|
||||||
/// Default for `#[label]`
|
|
||||||
pub const label: crate::SubdiagnosticMessage =
|
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
|
|
||||||
/// Default for `#[suggestion]`
|
|
||||||
pub const suggestion: crate::SubdiagnosticMessage =
|
|
||||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("suggestion"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variable_references<'a>(msg: &Message<&'a str>) -> Vec<&'a str> {
|
fn variable_references<'a>(msg: &Message<&'a str>) -> Vec<&'a str> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue