Skip to main content

talk/string/
types.rs

1//! Shared types for the talk encoding.
2//!
3//! Named `types` rather than `type` because `type` is a Rust keyword.
4
5use serde::Deserialize;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
8#[serde(rename_all = "lowercase")]
9pub enum Kind {
10  Consonant,
11  Vowel,
12  Symbol,
13}
14
15/// The two phonetic forms. `Kind` adds `Symbol` on top of these.
16///
17/// `Any` is only ever a MODIFIER's base, never a phone's: it marks a feature
18/// that is not about articulation and so attaches to either form. Length and
19/// stress are the two, `b_` being a geminate and `b^` a stressed onset.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
21#[serde(rename_all = "lowercase")]
22pub enum Form {
23  Consonant,
24  Vowel,
25  Any,
26}
27
28impl From<Form> for Kind {
29  fn from(form: Form) -> Kind {
30    match form {
31      Form::Consonant => Kind::Consonant,
32      // A sound's form is never `Any`; only a modifier's base is.
33      Form::Vowel | Form::Any => Kind::Vowel,
34    }
35  }
36}
37
38impl Kind {
39  pub fn as_str(self) -> &'static str {
40    match self {
41      Kind::Consonant => "consonant",
42      Kind::Vowel => "vowel",
43      Kind::Symbol => "symbol",
44    }
45  }
46}
47
48#[derive(Debug, Clone, Deserialize)]
49pub struct Phone {
50  pub ipa: String,
51  pub talk: String,
52  pub xsampa: String,
53  pub simple: String,
54  pub form: Form,
55  pub place: Option<String>,
56  pub manner: Option<String>,
57  pub voicing: Option<String>,
58  pub height: Option<String>,
59  pub backness: Option<String>,
60  pub roundedness: Option<String>,
61  #[serde(default)]
62  pub provisional: bool,
63}
64
65#[derive(Debug, Clone, Deserialize)]
66#[serde(rename_all = "camelCase")]
67pub struct Attaches {
68  pub place: Option<Vec<String>>,
69  pub not_place: Option<Vec<String>>,
70  pub manner: Option<Vec<String>>,
71  pub voicing: Option<Vec<String>>,
72}
73
74#[derive(Debug, Clone, Deserialize)]
75pub struct Modifier {
76  pub ipa: String,
77  pub talk: String,
78  pub xsampa: String,
79  pub simple: String,
80  pub base: Form,
81  pub feature: String,
82  pub slot: String,
83  pub order: i64,
84  #[serde(default)]
85  pub prefix: bool,
86  pub attaches: Option<Attaches>,
87  /// Fine phonetic detail: parsed and spelled, but outside the tone code
88  /// space.
89  ///
90  /// The IPA notation is lossless and the tone notation deliberately
91  /// coarser. Every mark here is real and gets reported by `parse_ipa`, but
92  /// the tone codes are budgeted at one byte for `seed` and two for `band`
93  /// and `mesh`, and twenty-five more axes would multiply that space past
94  /// both. So they contribute a spelling and never a digit, and a tone
95  /// string carrying one has no tone code.
96  #[serde(default)]
97  pub detail: bool,
98}
99
100#[derive(Debug, Clone, PartialEq, Eq)]
101pub struct Sound {
102  pub talk: String,
103  pub ipa: String,
104  pub simple: String,
105  pub machine: i64,
106  pub kind: Kind,
107  pub base: Option<&'static Phone>,
108  pub modifiers: Vec<&'static Modifier>,
109  /// Modifiers preceding the base: pre-aspiration, prenasalization and the
110  /// like. Kept apart from `modifiers` because position carries meaning,
111  /// `ʰk` and `kʰ` being different sounds.
112  pub pre: Vec<&'static Modifier>,
113  pub raw: bool,
114}
115
116// `Phone` and `Modifier` are interned in the runtime, so a sound's base and
117// modifiers are compared by identity rather than field by field.
118impl PartialEq for Phone {
119  fn eq(&self, other: &Phone) -> bool {
120    std::ptr::eq(self, other)
121  }
122}
123
124impl Eq for Phone {}
125
126impl PartialEq for Modifier {
127  fn eq(&self, other: &Modifier) -> bool {
128    std::ptr::eq(self, other)
129  }
130}
131
132impl Eq for Modifier {}
133
134/// A non-phonetic passthrough sound (punctuation, digit, space).
135#[derive(Debug, Clone, PartialEq, Eq)]
136pub struct SymbolEntry {
137  pub talk: String,
138  pub ipa: String,
139  pub simple: String,
140}
141
142/// A canonical sound with its spellings, minus the machine code point.
143#[derive(Debug, Clone, PartialEq, Eq)]
144pub struct SoundInfo {
145  pub talk: String,
146  pub ipa: String,
147  pub simple: String,
148  pub kind: Kind,
149}
150
151/// The machine code space: 24 bits, so every code serializes to exactly three
152/// bytes and the inventory has room to grow by two orders of magnitude.
153pub const CODE_LIMIT: i64 = 0xff_ffff;
154
155/// The code for a sound with no assignment, which is anything outside the
156/// enumerated inventory. Distinct from code 0, which is a real sound.
157pub const NO_CODE: i64 = -1;
158
159/// A unit the scanner can match: a base sound, an affix, or a passthrough
160/// symbol.
161#[derive(Debug, Clone, Copy)]
162pub enum Unit {
163  Phone(&'static Phone),
164  Modifier(&'static Modifier),
165  Symbol(&'static SymbolEntry),
166}