ewts 1.0.0

Converter between EWTS (Extended Wylie Transliteration Scheme) and Tibetan Unicode symbols (lib)
Documentation
//! Converter between EWTS (Extended Wylie Transliteration Scheme) and Tibetan Unicode, in both directions.
//!
//! EWTS writes Tibetan in Latin letters: `bkra shis bde legs/` is `བཀྲ་ཤིས་བདེ་ལེགས།`. The converter follows the
//! [EWTS rules](https://www.thlib.org/reference/transliteration/#!essay=/thl/ewts/rules/) of The Tibetan and
//! Himalayan Library.
//!
//! - [`ewts_to_unicode`] converts EWTS to Unicode and returns [`EwtsToUnicode`].
//! - [`unicode_to_ewts`] converts Unicode to EWTS and returns [`UnicodeToEwts`].
//!
//! Neither fails nor panics: what they cannot convert is copied as is, escaped or put in brackets, and reported in
//! `warnings` ([`EwtsWarning`], [`UnicodeWarning`]), sorted by position. The converter does not fix the input: if it
//! is written incorrectly, the result has incorrect characters.
//!
//! # Examples
//! ```
//! let unicode = ewts::ewts_to_unicode("oM aHhU~M` badz+ra gu ru pad+ma sid+d+hi hU~M`:").unicode;
//! assert_eq!(unicode, "ཨོཾ་ཨཿཧཱུྂ་བཛྲ་གུ་རུ་པདྨ་སིདྡྷི་ཧཱུྂ༔");
//!
//! // And back.
//! assert_eq!(ewts::unicode_to_ewts("བཀྲ་ཤིས་བདེ་ལེགས།").ewts, "bkra shis bde legs/");
//!
//! // "q" is not EWTS.
//! for warning in ewts::ewts_to_unicode("kqa").warnings {
//!     assert_eq!(warning.to_string(), "not EWTS at byte 1: 'q'");
//! }
//! ```
//!
//! # Round trip
//! [`unicode_to_ewts`] gives EWTS that [`ewts_to_unicode`] converts back to the source text, unless there is a
//! [lossy](UnicodeWarningKind::is_lossy) warning: with [`SignOrder`](UnicodeWarningKind::SignOrder) the signs of a
//! stack come back in canonical order (`ཀིྱ` gives `kyi`, which gives `ཀྱི`), with
//! [`NotEquivalent`](UnicodeWarningKind::NotEquivalent) a char comes back as chars that are not canonically
//! equivalent to it (`ༀ` gives `oM`, which gives `ཨོཾ`).
//!
//! Precomposed chars come back as their canonically equivalent parts: `གྷ` (U+0F43) gives `g+ha`, which gives
//! `ག` + `ྷ`. Non-Tibetan text is kept: ASCII other than line breaks and tabs is put in `[...]`, other text
//! stays as is.
//!
//! The other way round is not guaranteed: EWTS has several spellings for the same text, and [`unicode_to_ewts`]
//! gives the usual one (`k+ya` gives `ཀྱ`, which gives `kya`).
//!
//! # Typographic apostrophes
//! `ʼ` `ʹ` `‘` `’` `ʾ`, which word processors often put instead of `'`, are read as `'`: `ba’i` gives `བའི`.
//! Inside `[...]` they are kept as is. [`unicode_to_ewts`] puts them in brackets (`ka[’]`), so its result
//! converts back exactly.

#![warn(missing_docs)]

#[cfg(test)]
mod rules_test;

mod converter;
mod dict;
mod ewts_warning;
mod from_unicode;
mod tokenizer;
mod util;

pub use converter::EwtsToUnicode;
use converter::EwtsToUnicodeConverter;
pub use ewts_warning::{EwtsWarning, EwtsWarningKind};
pub use from_unicode::{UnicodeToEwts, UnicodeWarning, UnicodeWarningKind};

/// Converts EWTS to Tibetan Unicode, with warnings about input that is not valid EWTS.
///
/// Besides the EWTS letters, signs and punctuation:
/// - `[...]` is non-Tibetan text, copied without the brackets. Brackets nest, and `\[`, `\]`, `\\` and `\uXXXX`
///   work inside them.
/// - `\uXXXX` / `\UXXXXXXXX` is the char with that hex code; a backslash before any other char gives that char.
/// - A `+` that is not between letters or vowels is kept as is.
/// - Non-ASCII text is copied as is, except [typographic apostrophes](crate#typographic-apostrophes).
///
/// ```
/// use ewts::EwtsWarningKind;
///
/// let result = ewts::ewts_to_unicode("bkra shis bde legs/");
///
/// assert_eq!(result.unicode, "བཀྲ་ཤིས་བདེ་ལེགས།");
/// assert!(result.warnings.is_empty());
///
/// assert_eq!(ewts::ewts_to_unicode("Hello [world]").unicode, "ཿཨེལལོ་world");
/// assert_eq!(ewts::ewts_to_unicode("[a [b] \\] c]d").unicode, "a [b] ] cད");
/// assert_eq!(ewts::ewts_to_unicode("\\u0f40 ka +ta").unicode, "ཀ་ཀ་+ཏ");
///
/// // "q" is not EWTS: it is copied as is.
/// let result = ewts::ewts_to_unicode("kqa");
///
/// assert_eq!(result.unicode, "ཀqཨ");
/// assert_eq!(result.warnings[0].kind, EwtsWarningKind::NotEwts);
/// assert_eq!(result.warnings[0].pos, 1);
/// assert_eq!(result.warnings[0].ch, 'q');
/// ```
pub fn ewts_to_unicode(src: &str) -> EwtsToUnicode {
    EwtsToUnicodeConverter::convert(src)
}

/// Converts Tibetan Unicode to EWTS, with warnings about input that does not convert back exactly or looks
/// broken. Without [lossy](UnicodeWarningKind::is_lossy) warnings, converting the result back with [`ewts_to_unicode`]
/// gives the source string; precomposed chars come back decomposed. See [Round trip](crate#round-trip).
///
/// ```
/// use ewts::UnicodeWarningKind;
///
/// let unicode = "བཀྲ་ཤིས་བདེ་ལེགས།";
/// let result = ewts::unicode_to_ewts(unicode);
///
/// assert_eq!(result.ewts, "bkra shis bde legs/");
/// assert!(result.warnings.is_empty());
/// assert_eq!(ewts::ewts_to_unicode(&result.ewts).unicode, unicode);
///
/// // A vowel sign before a subjoined letter.
/// let result = ewts::unicode_to_ewts("ཀིྱ");
///
/// assert_eq!(result.ewts, "kyi");
/// assert_eq!(result.warnings[0].kind, UnicodeWarningKind::SignOrder);
/// assert_eq!(result.warnings[0].pos, 6);
/// assert_eq!(result.warnings[0].ch, '\u{0fb1}');
/// ```
pub fn unicode_to_ewts(src: &str) -> UnicodeToEwts {
    from_unicode::convert(src)
}

#[cfg(test)]
mod tests;