-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathenum_wrapper.rs
More file actions
74 lines (61 loc) · 1.84 KB
/
enum_wrapper.rs
File metadata and controls
74 lines (61 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::{
any::type_name,
fmt::{Debug, Display},
};
#[derive(Debug, Clone, PartialEq)]
pub struct EnumWrapper {
pub name: String,
pub variant: String,
pub value: String,
}
pub trait ToEnumWrapper {
fn to_enum_wrapper(&self) -> EnumWrapper;
}
impl<T> ToEnumWrapper for T
where
T: odict::schema::EnumIdentifier + Display,
{
fn to_enum_wrapper(&self) -> EnumWrapper {
let name = type_name::<T>();
let value = self.to_string();
let variant = self.id();
EnumWrapper {
name: name.split("::").last().unwrap_or(name).to_string(),
variant,
value,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use odict::schema::{FormKind, PartOfSpeech};
#[test]
fn test_to_enum_wrapper_pos_custom() {
let wrapper = PartOfSpeech::Other("Custom".to_string()).to_enum_wrapper();
assert_eq!(wrapper.name, "PartOfSpeech");
assert_eq!(wrapper.variant, "other");
assert_eq!(wrapper.value, "Custom");
}
#[test]
fn test_to_enum_wrapper_pos() {
let wrapper = PartOfSpeech::AdjKari.to_enum_wrapper();
assert_eq!(wrapper.name, "PartOfSpeech");
assert_eq!(wrapper.variant, "adj_kari");
assert_eq!(wrapper.value, "'kari' adjective (archaic)");
}
#[test]
fn test_to_enum_wrapper_form_kind() {
let wrapper = FormKind::Conjugation.to_enum_wrapper();
assert_eq!(wrapper.name, "FormKind");
assert_eq!(wrapper.variant, "conjugation");
assert_eq!(wrapper.value, "conjugation");
}
#[test]
fn test_to_enum_wrapper_form_kind_custom() {
let wrapper = FormKind::Other("Custom".to_string()).to_enum_wrapper();
assert_eq!(wrapper.name, "FormKind");
assert_eq!(wrapper.variant, "other");
assert_eq!(wrapper.value, "Custom");
}
}