Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add serializer #6

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod fullcontext_label;
mod parser;
mod question;
mod serializer;

pub use fullcontext_label::*;
pub use question::{question, AllQuestion};
5 changes: 1 addition & 4 deletions src/parser/mod.rs → src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a> LabelTokenizer<'a> {
}
}

/// `/F:f1_f2#_f3_f4@_f5_f6|f7_f8`
/// `/F:f1_f2#f3_f4@f5_f6|f7_f8`
fn f(&mut self) -> Result<Option<AccentPhraseCurrent>, ParseError> {
let f1 = Self::parse_or_xx(self.until("_")?)?;
let f2 = Self::parse_or_xx(self.until("#")?)?;
Expand Down Expand Up @@ -321,6 +321,3 @@ impl FromStr for Label {
LabelTokenizer::new(s).consume()
}
}

#[cfg(test)]
mod test;
315 changes: 315 additions & 0 deletions src/serializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
use std::fmt::{Display, Formatter, Result, Write};

use crate::fullcontext_label::{
AccentPhraseCurrent, AccentPhrasePrevNext, BreathGroupCurrent, BreathGroupPrevNext, Label,
Mora, Phoneme, Utterance, Word,
};

struct Serializer<'a, 'b> {
f: &'b mut Formatter<'a>,
}

impl<'a, 'b> Serializer<'a, 'b> {
fn new(f: &'b mut Formatter<'a>) -> Self {
Self { f }
}

fn xx(&mut self) -> Result {
self.f.write_str("xx")
}

fn all_xx<const N: usize>(&mut self, sep: &[char; N]) -> Result {
self.xx()?;
for c in sep {
self.f.write_char(*c)?;
self.xx()?;
}

Ok(())
}

fn or_xx<T: Display>(&mut self, value: &Option<T>) -> Result {
match value {
Some(v) => v.fmt(self.f),
None => self.xx(),
}
}

fn d02_or_xx<T: Display>(&mut self, value: &Option<T>) -> Result {
match value {
Some(v) => write!(self.f, "{:02}", v),
None => self.xx(),
}
}

fn bool(&mut self, value: bool) -> Result {
match value {
true => self.f.write_char('1'),

Check warning on line 47 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L47

Added line #L47 was not covered by tests
false => self.f.write_char('0'),
}
}

fn bool_or_xx(&mut self, value: &Option<bool>) -> Result {
match value {
Some(v) => self.bool(*v),

Check warning on line 54 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L54

Added line #L54 was not covered by tests
None => self.xx(),
}
}

/// `p1ˆp2-p3+p4=p5`
fn p(&mut self, phoneme: &Phoneme) -> Result {
self.or_xx(&phoneme.p2)?;
self.f.write_char('^')?;
self.or_xx(&phoneme.p1)?;
self.f.write_char('-')?;
self.or_xx(&phoneme.c)?;
self.f.write_char('+')?;
self.or_xx(&phoneme.n1)?;
self.f.write_char('=')?;
self.or_xx(&phoneme.n2)?;

Ok(())
}

/// `/A:a1+a2+a3`
fn a(&mut self, mora: &Option<Mora>) -> Result {
self.f.write_str("/A:")?;

if let Some(mora) = mora {
mora.relative_accent_position.fmt(self.f)?;
self.f.write_char('+')?;
mora.position_forward.fmt(self.f)?;
self.f.write_char('+')?;
mora.position_backward.fmt(self.f)?;
} else {
self.all_xx(&['+', '+'])?;
}

Ok(())
}

/// `/B:b1-b2_b3`
fn b(&mut self, word_prev: &Option<Word>) -> Result {
self.f.write_str("/B:")?;

if let Some(word_prev) = word_prev {
self.d02_or_xx(&word_prev.pos)?;
self.f.write_char('-')?;
self.d02_or_xx(&word_prev.ctype)?;
self.f.write_char('_')?;
self.d02_or_xx(&word_prev.cform)?;

Check warning on line 100 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L96-L100

Added lines #L96 - L100 were not covered by tests
} else {
self.all_xx(&['-', '_'])?;
}

Ok(())
}

/// `/C:c1_c2+c3`
fn c(&mut self, word_curr: &Option<Word>) -> Result {
self.f.write_str("/C:")?;

if let Some(word_curr) = word_curr {
self.d02_or_xx(&word_curr.pos)?;
self.f.write_char('_')?;
self.d02_or_xx(&word_curr.ctype)?;
self.f.write_char('+')?;
self.d02_or_xx(&word_curr.cform)?;
} else {
self.all_xx(&['_', '+'])?;
}

Ok(())
}

/// `/D:d1+d2_d3`
fn d(&mut self, word_next: &Option<Word>) -> Result {
self.f.write_str("/D:")?;

if let Some(word_next) = word_next {
self.d02_or_xx(&word_next.pos)?;
self.f.write_char('+')?;
self.d02_or_xx(&word_next.ctype)?;
self.f.write_char('_')?;
self.d02_or_xx(&word_next.cform)?;

Check warning on line 134 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L130-L134

Added lines #L130 - L134 were not covered by tests
} else {
self.all_xx(&['+', '_'])?;
}

Ok(())
}

///`/E:e1_e2!e3_e4-e5`
fn e(&mut self, accent_phrase_prev: &Option<AccentPhrasePrevNext>) -> Result {
self.f.write_str("/E:")?;

if let Some(accent_phrase_prev) = accent_phrase_prev {
accent_phrase_prev.mora_count.fmt(self.f)?;
self.f.write_char('_')?;
accent_phrase_prev.accent_position.fmt(self.f)?;
self.f.write_char('!')?;
self.bool(accent_phrase_prev.is_interrogative)?;
self.f.write_char('_')?;
self.xx()?;
self.f.write_char('-')?;
self.bool_or_xx(&accent_phrase_prev.is_pause_insertion)?;
} else {
self.all_xx(&['_', '!', '_', '-'])?;
}

Ok(())
}

/// `/F:f1_f2#f3_f4@f5_f6|f7_f8`
fn f(&mut self, accent_phrase_curr: &Option<AccentPhraseCurrent>) -> Result {
self.f.write_str("/F:")?;

if let Some(accent_phrase_curr) = accent_phrase_curr {
accent_phrase_curr.mora_count.fmt(self.f)?;
self.f.write_char('_')?;
accent_phrase_curr.accent_position.fmt(self.f)?;
self.f.write_char('#')?;
self.bool(accent_phrase_curr.is_interrogative)?;
self.f.write_char('_')?;
self.xx()?;
self.f.write_char('@')?;
accent_phrase_curr
.accent_phrase_position_forward

Check warning on line 177 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L177

Added line #L177 was not covered by tests
.fmt(self.f)?;
self.f.write_char('_')?;
accent_phrase_curr
.accent_phrase_position_backward

Check warning on line 181 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L181

Added line #L181 was not covered by tests
.fmt(self.f)?;
self.f.write_char('|')?;
accent_phrase_curr.mora_position_forward.fmt(self.f)?;
self.f.write_char('_')?;
accent_phrase_curr.mora_position_backward.fmt(self.f)?;
} else {
self.all_xx(&['_', '#', '_', '@', '_', '|', '_'])?;
}

Ok(())
}

/// `/G:g1_g2%g3_g4_g5`
fn g(&mut self, accent_phrase_next: &Option<AccentPhrasePrevNext>) -> Result {
self.f.write_str("/G:")?;

if let Some(accent_phrase_next) = accent_phrase_next {
accent_phrase_next.mora_count.fmt(self.f)?;
self.f.write_char('_')?;
accent_phrase_next.accent_position.fmt(self.f)?;
self.f.write_char('%')?;
self.bool(accent_phrase_next.is_interrogative)?;
self.f.write_char('_')?;
self.xx()?;
self.f.write_char('_')?;
self.bool_or_xx(&accent_phrase_next.is_pause_insertion)?;
} else {
self.all_xx(&['_', '%', '_', '_'])?;
}

Ok(())
}

/// `/H:h1_h2`
fn h(&mut self, breath_group_prev: &Option<BreathGroupPrevNext>) -> Result {
self.f.write_str("/H:")?;

if let Some(breath_group_prev) = breath_group_prev {
breath_group_prev.accent_phrase_count.fmt(self.f)?;
self.f.write_char('_')?;
breath_group_prev.mora_count.fmt(self.f)?;
} else {
self.all_xx(&['_'])?;
}

Ok(())
}

/// `/I:i1-i2@i3+i4&i5-i6|i7+i8`
fn i(&mut self, breath_group_curr: &Option<BreathGroupCurrent>) -> Result {
self.f.write_str("/I:")?;

if let Some(breath_group_curr) = breath_group_curr {
breath_group_curr.accent_phrase_count.fmt(self.f)?;
self.f.write_char('-')?;
breath_group_curr.mora_count.fmt(self.f)?;
self.f.write_char('@')?;
breath_group_curr
.breath_group_position_forward

Check warning on line 240 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L240

Added line #L240 was not covered by tests
.fmt(self.f)?;
self.f.write_char('+')?;
breath_group_curr
.breath_group_position_backward

Check warning on line 244 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L244

Added line #L244 was not covered by tests
.fmt(self.f)?;
self.f.write_char('&')?;
breath_group_curr
.accent_phrase_position_forward

Check warning on line 248 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L248

Added line #L248 was not covered by tests
.fmt(self.f)?;
self.f.write_char('-')?;
breath_group_curr
.accent_phrase_position_backward

Check warning on line 252 in src/serializer.rs

View check run for this annotation

Codecov / codecov/patch

src/serializer.rs#L252

Added line #L252 was not covered by tests
.fmt(self.f)?;
self.f.write_char('|')?;
breath_group_curr.mora_position_forward.fmt(self.f)?;
self.f.write_char('+')?;
breath_group_curr.mora_position_backward.fmt(self.f)?;
} else {
self.all_xx(&['-', '@', '+', '&', '-', '|', '+'])?;
}

Ok(())
}

/// `/J:j1_j2`
fn j(&mut self, breath_group_next: &Option<BreathGroupPrevNext>) -> Result {
self.f.write_str("/J:")?;

if let Some(breath_group_next) = breath_group_next {
breath_group_next.accent_phrase_count.fmt(self.f)?;
self.f.write_char('_')?;
breath_group_next.mora_count.fmt(self.f)?;
} else {
self.all_xx(&['_'])?;
}

Ok(())
}

/// `/K:k1+k2-k3`
fn k(&mut self, utterance: &Utterance) -> Result {
self.f.write_str("/K:")?;

utterance.breath_group_count.fmt(self.f)?;
self.f.write_char('+')?;
utterance.accent_phrase_count.fmt(self.f)?;
self.f.write_char('-')?;
utterance.mora_count.fmt(self.f)?;

Ok(())
}

fn fmt(&mut self, label: &Label) -> Result {
self.p(&label.phoneme)?;
self.a(&label.mora)?;
self.b(&label.word_prev)?;
self.c(&label.word_curr)?;
self.d(&label.word_next)?;
self.e(&label.accent_phrase_prev)?;
self.f(&label.accent_phrase_curr)?;
self.g(&label.accent_phrase_next)?;
self.h(&label.breath_group_prev)?;
self.i(&label.breath_group_curr)?;
self.j(&label.breath_group_next)?;
self.k(&label.utterance)?;

Ok(())
}
}

impl Display for Label {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
Serializer::new(f).fmt(self)
}
}
17 changes: 7 additions & 10 deletions src/parser/test.rs → tests/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::*;
use jlabel::{
AccentPhraseCurrent, AccentPhrasePrevNext, BreathGroupCurrent, BreathGroupPrevNext, Label,
Mora, Phoneme, Utterance, Word,
};

#[test]
fn test_parse() {
let tests = [
pub fn fixtures() -> [(&'static str, Label); 11] {
[
(
"xx^xx-sil+k=o/A:xx+xx+xx/B:xx-xx_xx/C:xx_xx+xx/D:xx+xx_xx/E:xx_xx!xx_xx-xx/F:xx_xx#xx_xx@xx_xx|xx_xx/G:5_5%0_xx_xx/H:xx_xx/I:xx-xx@xx+xx&xx-xx|xx+xx/J:1_5/K:1+1-5",
Label {
Expand Down Expand Up @@ -541,10 +543,5 @@ fn test_parse() {
},
},
),
];

for (input, expected) in tests.iter() {
let actual = input.parse::<Label>().unwrap();
assert_eq!(&actual, expected);
}
]
}
11 changes: 11 additions & 0 deletions tests/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod fixtures;
use fixtures::fixtures;
use jlabel::Label;

#[test]
fn test_parse() {
for (input, expected) in fixtures() {
let actual: Label = input.parse().unwrap();
assert_eq!(actual, expected);
}
}
10 changes: 10 additions & 0 deletions tests/serializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod fixtures;
use fixtures::fixtures;

#[test]
fn test_serialize() {
for (expected, label) in fixtures() {
let actual = label.to_string();
assert_eq!(actual, expected);
}
}