mirror of
https://github.com/ElnuDev/rust-jmdict.git
synced 2025-07-01 13:26:01 -07:00
add all_variants, from_constant_name to trait Enum
This commit is contained in:
parent
ba8cfff126
commit
c4f646d556
3 changed files with 36 additions and 0 deletions
|
@ -439,6 +439,28 @@ fn process(e: Enum) -> String {
|
|||
lines.push(" }".into());
|
||||
lines.push(" }\n".into());
|
||||
|
||||
//fn from_constant_name(&str) -> Self
|
||||
lines.push(" fn from_constant_name(text: &str) -> Option<Self> {".into());
|
||||
lines.push(" match text {".into());
|
||||
for v in e.variants.iter().filter(|v| v.enabled) {
|
||||
lines.push(format!(
|
||||
" \"{}\" => Some({}::{}),",
|
||||
v.name, e.name, v.name
|
||||
));
|
||||
}
|
||||
lines.push(" _ => None,".into());
|
||||
lines.push(" }".into());
|
||||
lines.push(" }\n".into());
|
||||
|
||||
//fn all_variants() -> &'static [Self]
|
||||
lines.push(" fn all_variants() -> &'static [Self] {".into());
|
||||
lines.push(" &[".into());
|
||||
for v in e.variants.iter().filter(|v| v.enabled) {
|
||||
lines.push(format!(" {}::{},", e.name, v.name));
|
||||
}
|
||||
lines.push(" ]".into());
|
||||
lines.push(" }\n".into());
|
||||
|
||||
//end impl Enum
|
||||
lines.push("}\n".into());
|
||||
|
||||
|
|
|
@ -43,6 +43,10 @@ pub trait EnumPayload {
|
|||
|
||||
///Common methods provided by all enums in this crate.
|
||||
pub trait Enum: Sized {
|
||||
///Returns a list of all variant values in this enum. No particular order is guaranteed or
|
||||
///implied.
|
||||
fn all_variants() -> &'static [Self];
|
||||
|
||||
///Returns the string that marks this enum variant in the JMdict. For values that JMdict
|
||||
///represents as XML entities, only the entity name is returned, e.g. `adj-n` instead of
|
||||
///`&adj-n;`.
|
||||
|
@ -55,6 +59,11 @@ pub trait Enum: Sized {
|
|||
///Returns the variant name. This is used to generate Rust code for this enum. The `impl
|
||||
///Display` for enums uses this same representation.
|
||||
fn constant_name(&self) -> &'static str;
|
||||
|
||||
///Returns the variant that is identified the given name in Rust code, or `None` if there is no
|
||||
///such variant. This is the reverse of `self.constant_name()`, i.e.
|
||||
///`Self::from_constant_name(self.constant_name()) == Some(self)`.
|
||||
fn from_constant_name(name: &str) -> Option<Self>;
|
||||
}
|
||||
|
||||
///PriorityInCorpus appears in struct [Priority]. It describes how often a dictionary entry
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue