Skip to main content

Vocabulary

Struct Vocabulary 

Source
pub struct Vocabulary { /* private fields */ }
Expand description

Parsed personal entries. Region declarations and flags are validated but not kept, so lookup is locale-neutral. Entries are grouped by lowercase form, so a lookup compares case rules within one group.

Implementations§

Source§

impl Vocabulary

Source

pub fn parse(source: &[u8]) -> Result<Self, ParseError>

Parse a Vim word list line by line, including BOM, CRLF, case and forbidden flags.

A line whose first byte is # is a comment whatever follows it, because Vim’s zw, zug and zuw comment out an entry by overwriting its first byte. A line that isn’t UTF-8, an unknown / directive, an unsupported flag or another invalid line is skipped and counted in Vocabulary::skipped.

use keyrill_core::vocabulary::{Membership, Vocabulary};

let words = Vocabulary::parse("# personal\nrustc\nNeovim/=\nteh/!\nbad/*\n".as_bytes())?;
let units = |s: &str| s.encode_utf16().collect::<Vec<u16>>();
assert_eq!(words.lookup(&units("RUSTC")), Membership::Recognized);
assert_eq!(words.lookup(&units("neovim")), Membership::Absent);
assert_eq!(words.lookup(&units("teh")), Membership::Forbidden);
assert_eq!((words.len(), words.skipped()), (3, 1));
§Errors

A ParseError for oversized input, NUL anywhere or a leading non-UTF-8 /encoding=; nothing is partially kept.

Source

pub fn parse_words( source: &[u8], membership: Membership, ) -> Result<Self, ParseError>

Parse a plain word list, one word per line, as OS spell checkers keep learned words: UTF-8 with an optional BOM, or UTF-16 LE with its BOM. Every word gets membership and the case rule of an unflagged Vim entry; # and / are ordinary characters. A line that doesn’t decode or holds a control character is skipped and counted.

use keyrill_core::vocabulary::{Membership, Vocabulary};

let units = |s: &str| s.encode_utf16().collect::<Vec<u16>>();
let learned = Vocabulary::parse_words(b"km/h\r\nrustc\n", Membership::Recognized)?;
assert_eq!(learned.lookup(&units("km/h")), Membership::Recognized);
assert_eq!(learned.lookup(&units("Rustc")), Membership::Recognized);

let mut utf16 = vec![0xff, 0xfe];
utf16.extend("teh\r\n".encode_utf16().flat_map(u16::to_le_bytes));
let excluded = Vocabulary::parse_words(&utf16, Membership::Forbidden)?;
assert_eq!(excluded.lookup(&units("teh")), Membership::Forbidden);
§Errors

A ParseError for oversized input, NUL anywhere, a UTF-16 LE source of odd length or a big-endian UTF-16 BOM; nothing is partially kept.

Source

pub fn len(&self) -> usize

Number of accepted entries; a word declared twice counts twice.

Source

pub fn is_empty(&self) -> bool

Whether a valid source explicitly contains no entries.

Source

pub fn skipped(&self) -> usize

Number of skipped lines that weren’t comments, blank or accepted entries and directives.

Source

pub fn lookup(&self, word: &[u16]) -> Membership

Membership of word under the case rule of each entry that matches it; across matching entries, forbidden wins, then recognized, then rare.

Trait Implementations§

Source§

impl Default for Vocabulary

Source§

fn default() -> Vocabulary

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.