inflectived/src/entry.rs

81 lines
1.7 KiB
Rust
Raw Normal View History

2021-12-25 17:30:14 -05:00
use std::cmp;
2021-12-31 12:22:05 -05:00
use std::slice::Iter;
use serde_json::Value;
use serde::Deserialize;
2021-12-25 17:30:14 -05:00
2021-12-31 12:22:05 -05:00
#[derive (Clone, Debug)]
2021-12-25 17:30:14 -05:00
pub struct WiktionaryEntry {
pub word: String,
pub type_: String,
2021-12-31 12:22:05 -05:00
pub parsed_json: Value,
2021-12-25 17:30:14 -05:00
}
impl cmp::PartialEq for WiktionaryEntry {
fn eq(&self, other: &Self) -> bool {
self.word.eq(&other.word)
}
}
impl cmp::Eq for WiktionaryEntry {}
impl cmp::PartialOrd for WiktionaryEntry {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl cmp::Ord for WiktionaryEntry {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.word.cmp(&other.word)
}
}
impl WiktionaryEntry {
pub fn parse(unparsed_json: &str) -> Self {
2021-12-31 12:22:05 -05:00
let json: Value = serde_json::from_str(unparsed_json).unwrap();
let word = String::from(json["word"].as_str().unwrap());
let type_ = String::from(json["pos"].as_str().unwrap());
2021-12-25 17:30:14 -05:00
Self {
word,
type_,
parsed_json: json
}
}
2021-12-31 12:22:05 -05:00
pub fn new(word: String, type_: String, parsed_json: Value) -> Self {
Self {
word,
type_,
parsed_json
}
}
2021-12-25 17:30:14 -05:00
}
pub struct WiktionaryEntries(Vec<WiktionaryEntry>);
impl WiktionaryEntries {
pub fn parse_data(data: String) -> Self {
let mut entries: Vec<WiktionaryEntry> = Vec::new();
for line in data.lines() {
entries.push(WiktionaryEntry::parse(line));
}
Self(entries)
}
2021-12-31 12:22:05 -05:00
pub fn iter(&self) -> Iter<WiktionaryEntry> {
self.0.iter()
2021-12-25 17:30:14 -05:00
}
}
2021-12-31 12:22:05 -05:00
#[derive(Debug, Deserialize)]
pub struct Form {
pub form: String,
pub tags: Vec<String>,
pub source: Option<String>,
}