Struct contractions::Contractions[][src]

pub struct Contractions { /* fields omitted */ }
Expand description

Main actor in the contractions crate

Stores Contractions in a Vec

Example

let contractions = contractions::Contractions::default();
assert_eq!("I am sure you would have been fine.", contractions.apply("I’m sure you’d’ve been fine."));
assert_eq!("Are you sure?", contractions.apply("R u sure?"));

Implementations

impl Contractions[src]

#[must_use]
pub const fn new() -> Self
[src]

Creates empty Contractions

Example

use contractions::{self, Contractions};
let contractions = Contractions::new();

pub fn from_json(contractions_as_str: &[&str]) -> Result<Self, Box<dyn Error>>[src]

Deserialize Contraction from json

Convenience method that chains Contractions::new() and Contractions::add_from_json()

Example

use contractions::{self, Contractions};
let contractions = Contractions::from_json(&[contractions::SINGLE_CONTRACTIONS_JSON, contractions::SINGLE_NO_APOSTROPHE_CONTRACTIONS_JSON]);

Errors

Returns an Error if deserialization fails

pub fn add_from_json(
    &mut self,
    contractions_as_str: &str
) -> Result<(), Box<dyn Error>>
[src]

Add Contractions from a json string to an existing Contractions struct

Example

use contractions::{self, Contractions};
let mut contractions = Contractions::new();
contractions.add_from_json(contractions::SINGLE_CONTRACTIONS_JSON);

Errors

Returns an Error if deserialization fails

pub fn remove(&mut self, key: &str)[src]

Remove a Contraction from Contractions

Provide the exact find key to delete the corresponding Contraction

Example

use contractions::{self, Contractions};
let mut contractions = Contractions::new();
assert_eq!("I’m happy", contractions.apply("I’m happy"));
contractions.add_from_json(contractions::SINGLE_CONTRACTIONS_JSON);
assert_eq!("I am happy", contractions.apply("I’m happy"));
contractions.remove("\\b(?i)i['’`]m(?-i)\\b");
assert_eq!("I’m happy", contractions.apply("I’m happy"));

pub fn add(
    &mut self,
    find: &str,
    replace: LinkedHashMap<&str, &str>
) -> Result<(), Box<dyn Error>>
[src]

Add a contraction to Contractions

Example

use contractions::{self, Contractions};
let mut contractions = Contractions::new();
assert_eq!("I’m happy", contractions.apply("I’m happy"));
let find = r#"\b(?i)i['’`]m(?-i)\b"#;
let mut replace = linked_hash_map::LinkedHashMap::new();
replace.insert(r#"\bi['’`]m\b"#, "i am");
replace.insert(r#"\bI['’`]m\b"#, "I am");
replace.insert(r#"\bI['’`]M\b"#, "I AM");
contractions.add(find, replace);
assert_eq!("I am happy", contractions.apply("I’m happy"));

Errors

Returns an Error if find or the key in the replace cannot be successfully turned into a Regex

#[must_use]
pub fn apply(&self, input: &str) -> String
[src]

Replace contractions with their long form

Example

use contractions::Contractions;
let contractions = Contractions::default();
assert_eq!("I am your brother’s son", contractions.apply("I’m your brother’s son"));

Trait Implementations

impl Debug for Contractions[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for Contractions[src]

fn default() -> Self[src]

Returns the built in configuration for Contractions

Example

use contractions::Contractions;
let contractions = Contractions::new();

Panics

Only panics when the library internal configuration is faulty this ought to only happen during development

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.