aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2023-10-06 19:05:58 +0200
committerAlex Auvolat <alex@adnab.me>2023-10-06 19:05:58 +0200
commit89c53131c152eb17a1632e58c12f00a24000162d (patch)
treefd396c68d82216ebdf0f571c691ee040aa35f686 /src
parentb521ce6ed06f8a3f6c5fe1f9a79f3b97d4151349 (diff)
downloaddatagengo-89c53131c152eb17a1632e58c12f00a24000162d.tar.gz
datagengo-89c53131c152eb17a1632e58c12f00a24000162d.zip
serialize charsets as strings
Diffstat (limited to 'src')
-rw-r--r--src/charset.rs25
-rw-r--r--src/main.rs8
2 files changed, 28 insertions, 5 deletions
diff --git a/src/charset.rs b/src/charset.rs
index a79eddb..71e1e84 100644
--- a/src/charset.rs
+++ b/src/charset.rs
@@ -2,7 +2,8 @@ use std::cmp::Ordering;
use serde::{Deserialize, Serialize};
-#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize, Default)]
+#[derive(Debug, Eq, PartialEq, Hash, Clone, Default, Serialize, Deserialize)]
+#[serde(into = "String", from = "CharsetSerializedEnum")]
pub struct Charset(pub Vec<char>);
impl Charset {
@@ -153,6 +154,28 @@ impl Charset {
}
}
+impl Into<String> for Charset {
+ fn into(self) -> String {
+ self.to_string()
+ }
+}
+
+#[derive(Deserialize)]
+#[serde(untagged)]
+enum CharsetSerializedEnum {
+ Array(Vec<char>),
+ Str(String),
+}
+
+impl From<CharsetSerializedEnum> for Charset {
+ fn from(s: CharsetSerializedEnum) -> Charset {
+ match s {
+ CharsetSerializedEnum::Array(a) => Charset::from_iter(a.into_iter()),
+ CharsetSerializedEnum::Str(s) => Charset::new(&s),
+ }
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;
diff --git a/src/main.rs b/src/main.rs
index 9e08418..4addd02 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -88,7 +88,7 @@ fn main() {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
- .unwrap_or_default();
+ .expect("failed to decode batches.json");
for batch in batches.iter_mut() {
simplify_batch(batch);
}
@@ -104,7 +104,7 @@ fn main() {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
- .unwrap_or_default();
+ .expect("failed to decode batches.json");
let kanji_levels = read_kanji_levels().expect("read_kanji_levels");
let kanji_levels = kanji_levels
.into_iter()
@@ -123,7 +123,7 @@ fn main() {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
- .unwrap_or_default();
+ .expect("failed to decode batches.json");
let jlpt_vocab = load_jlpt_vocab().expect("load_jlpt_vocab");
add_vocab(&mut batches, &jlpt_vocab);
fs::write(
@@ -150,7 +150,7 @@ fn main() {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
- .unwrap_or_default();
+ .expect("failed to decode batches.json");
add_examples(&mut batches, &ex);