betacode

Crates.iobetacode
lib.rsbetacode
version1.2.0
sourcesrc
created_at2022-08-17 18:28:41.988537
updated_at2024-01-18 12:20:03.21789
descriptionA rust library for Betacode conversion
homepage
repositoryhttps://github.com/caiogeraldes/betacode-rs
max_upload_size
id647705
size24,389
silenus (caiogeraldes)

documentation

README

betacode

A rust library for Betacode conversion.

Conversion

Example:

use betacode::converter;

let input = String::from("mh=nin a)/eide qea\\ *phlhi+a/dew *a)xilh=os");
let output = String::from("μῆνιν ἄειδε θεὰ Πηληϊάδεω Ἀχιλῆος");
let result = betacode::converter::convert(input);
assert_eq!(result, output);

Validation

Validating a Betacode text consists in validating whether or not it follows the rules:

  • it is composed solely by ASCII characters, else it returns ValidationError::NotASCII with the invalid characters;
  • its characters are handled by the converter module, else it returns ValidationError::InvalidChars with the invalid characters;
  • the disposition of its characters is interpretable by the converter module, else it returns ValidationError::InvalidDiacriticOrder with the invalid sequences.

The later is arguably the more easily recoverable, by means of the function converter::reorder_diacritics. The former pair might be recovered by ignoring invalid characters.

Details:

If the text is input in proper ASCII Betacode (and the converter(super::converter) can convert it), it returns Ok().

let input = String::from("mh=nin a)/eide qea\\ *phlhi+a/dew *a)xilh=os");
assert!(betacode::validator::validate(input).is_ok());

Otherwise, it specifies what error occurred.

For example, if passed a string with non-ASCII characters such as ἄλγεα, it stores a list of all characters that break the validation in the enum ValidationError::NotASCII.

let input = String::from("ἄλγεα");
let result = betacode::validator::validate(input);
assert!(result.is_err());
match result {
    Ok(_) => (),
    Err(e) => {
        if let betacode::validator::ValidationError::NotASCII(b) = e {
            assert_eq!(b, vec!['ἄ', 'λ','γ','ε','α']);
        }
    }
}

If the string is ASCII, but the proper conversion rule has not been implemented, it stores the list of characters that are not convertable in the enum ValidationError::InvalidChars

let input = String::from("9");
let result = betacode::validator::validate(input);
assert!(result.is_err());
match result {
    Ok(_) => (),
    Err(e) => {
        if let betacode::validator::ValidationError::InvalidChars(b) = e {
            assert_eq!(b, vec!['9']);
        }
    }
}

If, on other hand, the text contains an order of diacritics that can not be directly converted, it returns the list of sequences that are not valid. The converter module still can convert it, but this is implemented to assure that the corpus is properly built for other tools to operate. It stores all the patterns that break the BREATH/DIAIRESIS + ACCENT + SUB-IOTA order in ValidationError::InvalidDiacriticOrder.

let input = String::from("h\\( a/)ndra");
let result = betacode::validator::validate(input);
assert!(result.is_err());
match result {
    Ok(_) => (),
    Err(e) => {
        if let betacode::validator::ValidationError::InvalidDiacriticOrder(b) = e {
            assert_eq!(b, vec!["\\(".to_string(), "/)".to_string()]);
        }
    }
}
Commit count: 41

cargo fmt