Skip to content

Commit 6382c29

Browse files
committed
Make methods on CharacterDefinitions real methods
i.e. move them within the impl block of CharacterDefinition
1 parent 33589da commit 6382c29

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

src/translator.rs

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -103,42 +103,42 @@ impl CharacterDefinition {
103103
fn get(&self, c: &char) -> Option<&Translation> {
104104
self.0.get(c)
105105
}
106-
}
107106

108-
fn resolve_implicit_dots(
109-
chars: &str,
110-
character_definitions: &CharacterDefinition,
111-
) -> Result<String, TranslationError> {
112-
chars
113-
.chars()
114-
.map(|c| {
115-
character_definitions
116-
.get(&c)
117-
.ok_or(TranslationError::ImplicitCharacterNotDefined(c))
118-
.map(|t| t.output.to_string())
119-
})
120-
.collect()
121-
}
107+
fn resolve_implicit_dots(
108+
&self,
109+
chars: &str,
110+
) -> Result<String, TranslationError> {
111+
chars
112+
.chars()
113+
.map(|c| {
114+
self
115+
.get(&c)
116+
.ok_or(TranslationError::ImplicitCharacterNotDefined(c))
117+
.map(|t| t.output.to_string())
118+
})
119+
.collect()
120+
}
122121

123-
/// Convert braille dots to Unicode characters.
124-
///
125-
/// Convert given braille `dots` to Unicode characters. If the dots are `Explicit` then simply
126-
/// delegate to the [dots_to_unicode] function. Otherwise, if the dots are `Implicit` convert the
127-
/// given `chars` to braille with the given `character_definitions` and using the
128-
/// [resolve_implicit_dots] function
129-
///
130-
/// Returns the braille Unicode characters or `TranslationError` if the implicit characters could
131-
/// not be converted.
132-
fn braille_to_unicode(
133-
dots: &Braille,
134-
chars: &str,
135-
character_definitions: &CharacterDefinition,
136-
) -> Result<String, TranslationError> {
137-
let dots = match dots {
138-
Braille::Implicit => resolve_implicit_dots(&chars, character_definitions)?,
139-
Braille::Explicit(dots) => dots_to_unicode(&dots),
140-
};
141-
Ok(dots)
122+
/// Convert braille dots to Unicode characters.
123+
///
124+
/// Convert given braille `dots` to Unicode characters. If the dots are `Explicit` then simply
125+
/// delegate to the [dots_to_unicode] function. Otherwise, if the dots are `Implicit` convert the
126+
/// given `chars` to braille with the given `character_definitions` and using the
127+
/// [resolve_implicit_dots] function
128+
///
129+
/// Returns the braille Unicode characters or `TranslationError` if the implicit characters could
130+
/// not be converted.
131+
fn braille_to_unicode(
132+
&self,
133+
dots: &Braille,
134+
chars: &str,
135+
) -> Result<String, TranslationError> {
136+
let dots = match dots {
137+
Braille::Implicit => self.resolve_implicit_dots(&chars)?,
138+
Braille::Explicit(dots) => dots_to_unicode(&dots),
139+
};
140+
Ok(dots)
141+
}
142142
}
143143

144144
#[derive(Debug)]
@@ -291,23 +291,23 @@ impl TranslationTable {
291291
}
292292
}
293293
Rule::Comp6 { chars, dots } | Rule::Always { chars, dots, .. } => {
294-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
294+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
295295
translations.insert(chars.to_string(), dots, Boundary::None, Boundary::None);
296296
}
297297
Rule::Word { chars, dots, .. } => {
298-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
298+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
299299
translations.insert(chars.to_string(), dots, Boundary::Word, Boundary::Word);
300300
}
301301
Rule::Begword { chars, dots, .. } => {
302-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
302+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
303303
translations.insert(chars.to_string(), dots, Boundary::Word, Boundary::NotWord)
304304
}
305305
Rule::Sufword { chars, dots, .. } => {
306-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
306+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
307307
translations.insert(chars.to_string(), dots, Boundary::Word, Boundary::None);
308308
}
309309
Rule::Midword { chars, dots, .. } | Rule::Partword { chars, dots, .. } => {
310-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
310+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
311311
translations.insert(
312312
chars.to_string(),
313313
dots,
@@ -316,15 +316,15 @@ impl TranslationTable {
316316
)
317317
}
318318
Rule::Midendword { chars, dots, .. } => {
319-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
319+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
320320
translations.insert(chars.to_string(), dots, Boundary::NotWord, Boundary::None);
321321
}
322322
Rule::Endword { chars, dots, .. } | Rule::Prfword { chars, dots, .. } => {
323-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
323+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
324324
translations.insert(chars.to_string(), dots, Boundary::None, Boundary::Word);
325325
}
326326
Rule::Begmidword { chars, dots, .. } => {
327-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
327+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
328328
translations.insert(chars.to_string(), dots, Boundary::None, Boundary::NotWord);
329329
}
330330
Rule::Joinword { chars, dots, .. } | Rule::Lowword { chars, dots, .. } => {
@@ -348,7 +348,7 @@ impl TranslationTable {
348348
Boundary::Number,
349349
),
350350
Rule::Endnum { chars, dots, .. } => {
351-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
351+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
352352
translations.insert(
353353
chars.to_string(),
354354
dots,
@@ -363,7 +363,7 @@ impl TranslationTable {
363363
dots,
364364
..
365365
} => {
366-
let dots = braille_to_unicode(dots, chars, &character_definitions)?;
366+
let dots = character_definitions.braille_to_unicode(dots, chars)?;
367367
translations.insert_match(chars.to_string(), dots, pre, post);
368368
}
369369

@@ -542,13 +542,13 @@ mod tests {
542542
fn resolve_implicit_dots_test() {
543543
let char_defs = CharacterDefinition::new();
544544
assert_eq!(
545-
resolve_implicit_dots("xs", &char_defs),
545+
char_defs.resolve_implicit_dots("xs"),
546546
Err(TranslationError::ImplicitCharacterNotDefined('x'))
547547
);
548548
let mut char_defs = CharacterDefinition::new();
549549
char_defs.insert('a', Translation::new("a".to_string(), "A".to_string(), 1));
550550
char_defs.insert('h', Translation::new("h".to_string(), "H".to_string(), 1));
551-
assert_eq!(resolve_implicit_dots("haha", &char_defs), Ok("HAHA".into()));
551+
assert_eq!(char_defs.resolve_implicit_dots("haha"), Ok("HAHA".into()));
552552
}
553553

554554
#[test]

0 commit comments

Comments
 (0)