Skip to main content

Options

Struct Options 

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

Algorithm options with the spec defaults (SPEC §4).

Build them with the chaining setters; every value is clamped into range rather than rejected, exactly as in every other port: fixation to 1..=5, saccade to >= 1. Negative values cannot be expressed in the first place (u8/usize), which the spec allows a port whose type system excludes them to rely on.

The setters take self and return it, so a value can be configured in one expression; the getters carry a get_ prefix (as on std::process::Command) because the plain names are taken by the setters.

Options implements Clone and Debug but not PartialEq: it can hold a closure and a segmenter, which have no useful notion of equality.

use smooth_reading::Options;

let options = Options::new().fixation(4).saccade(2);
assert_eq!(options.get_fixation(), 4);
assert_eq!(options.get_saccade(), 2);
assert_eq!(Options::new().fixation(9).get_fixation(), 5); // clamped
assert_eq!(Options::new().saccade(0).get_saccade(), 1); // clamped

Implementations§

Source§

impl Options

Source

pub const fn new() -> Self

The spec defaults: strength 3, every word, no minimum length, numbers not emphasised, no locale, the built-in algorithm and segmenter.

Source

pub const fn fixation(self, fixation: u8) -> Self

Sets the fixation strength, clamped to 1..=5. Default 3.

The strength selects the proportion of each word that is emphasised: 0.20, 0.35, 0.50, 0.65, 0.80.

Source

pub const fn saccade(self, saccade: usize) -> Self

Sets the saccade interval: emphasise every saccade-th word. Clamped to >= 1. Default 1.

Source

pub const fn min_word_length(self, min_word_length: usize) -> Self

Words with fewer grapheme clusters than this get no fixation. Default 1.

Source

pub const fn emphasize_numbers(self, emphasize_numbers: bool) -> Self

Whether words made only of decimal digits are emphasised. Default false.

Source

pub fn locale(self, locale: impl Into<String>) -> Self

Sets the BCP-47 language tag passed to the segmenter. The built-in SpecSegmenter ignores it; a platform break iterator may use it.

Source

pub fn fixation_length<F>(self, fixation_length: F) -> Self
where F: Fn(&str, usize, &Options) -> usize + Send + Sync + 'static,

Replaces the built-in fixation algorithm (SPEC §2, “Custom override”).

fixation_length honours this override, so the closure must not call it with the same options (that would recurse).

use smooth_reading::{Options, to_markdown};

// Always emphasise exactly one cluster (clamped to the word length).
let options = Options::new().fixation_length(|_word, _graphemes, _options| 1);
assert_eq!(to_markdown("Smooth reading", &options, "**"), "**S**mooth **r**eading");
Source

pub fn segmenter(self, segmenter: impl Segmenter + 'static) -> Self

Supplies word boundaries and grapheme clusters (SPEC §3).

The default is the spec’s regular-expression scanner. Supply an ICU-backed Segmenter for dictionary-based Chinese, Japanese and Thai word breaks; the fixation algorithm itself never changes. The segmenter is stored behind an Arc; use Options::shared_segmenter to share one you already hold.

Source

pub fn shared_segmenter(self, segmenter: Arc<dyn Segmenter>) -> Self

Like Options::segmenter, for a segmenter that is already shared (for example one expensive to build and reused across many Options).

Source

pub const fn get_fixation(&self) -> u8

The fixation strength, 1..=5.

Source

pub const fn get_saccade(&self) -> usize

The saccade interval, >= 1.

Source

pub const fn get_min_word_length(&self) -> usize

The minimum word length (in grapheme clusters) that gets a fixation.

Source

pub const fn get_emphasize_numbers(&self) -> bool

Whether digit-only words are emphasised.

Source

pub fn get_locale(&self) -> Option<&str>

The BCP-47 language tag, if one was set.

Source

pub const fn get_fixation_length(&self) -> Option<&FixationLengthFn>

The custom fixation-length function, if one was set.

Source

pub const fn get_segmenter(&self) -> Option<&Arc<dyn Segmenter>>

The custom segmenter, if one was set.

Source

pub fn ratio(&self) -> f64

Fraction of each word that is emphasised at this fixation strength.

use smooth_reading::Options;

assert_eq!(Options::new().ratio(), 0.5);
assert_eq!(Options::new().fixation(5).ratio(), 0.8);

Trait Implementations§

Source§

impl Clone for Options

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Options

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Options

Source§

fn default() -> Self

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.