use std::fmt;
use std::sync::Arc;
use crate::segment::{Segmenter, SpecSegmenter};
pub type FixationLengthFn = Arc<dyn Fn(&str, usize, &Options) -> usize + Send + Sync>;
const PERCENT: [usize; 5] = [20, 35, 50, 65, 80];
static SPEC_SEGMENTER: SpecSegmenter = SpecSegmenter;
#[derive(Clone)]
pub struct Options {
fixation: u8,
saccade: usize,
min_word_length: usize,
emphasize_numbers: bool,
locale: Option<String>,
fixation_length: Option<FixationLengthFn>,
segmenter: Option<Arc<dyn Segmenter>>,
}
impl Default for Options {
fn default() -> Self {
Self::new()
}
}
impl Options {
#[must_use]
pub const fn new() -> Self {
Self {
fixation: 3,
saccade: 1,
min_word_length: 1,
emphasize_numbers: false,
locale: None,
fixation_length: None,
segmenter: None,
}
}
#[must_use]
pub const fn fixation(mut self, fixation: u8) -> Self {
self.fixation = if fixation < 1 {
1
} else if fixation > 5 {
5
} else {
fixation
};
self
}
#[must_use]
pub const fn saccade(mut self, saccade: usize) -> Self {
self.saccade = if saccade < 1 { 1 } else { saccade };
self
}
#[must_use]
pub const fn min_word_length(mut self, min_word_length: usize) -> Self {
self.min_word_length = min_word_length;
self
}
#[must_use]
pub const fn emphasize_numbers(mut self, emphasize_numbers: bool) -> Self {
self.emphasize_numbers = emphasize_numbers;
self
}
#[must_use]
pub fn locale(mut self, locale: impl Into<String>) -> Self {
self.locale = Some(locale.into());
self
}
#[must_use]
pub fn fixation_length<F>(mut self, fixation_length: F) -> Self
where
F: Fn(&str, usize, &Options) -> usize + Send + Sync + 'static,
{
self.fixation_length = Some(Arc::new(fixation_length));
self
}
#[must_use]
pub fn segmenter(mut self, segmenter: impl Segmenter + 'static) -> Self {
self.segmenter = Some(Arc::new(segmenter));
self
}
#[must_use]
pub fn shared_segmenter(mut self, segmenter: Arc<dyn Segmenter>) -> Self {
self.segmenter = Some(segmenter);
self
}
#[must_use]
pub const fn get_fixation(&self) -> u8 {
self.fixation
}
#[must_use]
pub const fn get_saccade(&self) -> usize {
self.saccade
}
#[must_use]
pub const fn get_min_word_length(&self) -> usize {
self.min_word_length
}
#[must_use]
pub const fn get_emphasize_numbers(&self) -> bool {
self.emphasize_numbers
}
#[must_use]
pub fn get_locale(&self) -> Option<&str> {
self.locale.as_deref()
}
#[must_use]
pub const fn get_fixation_length(&self) -> Option<&FixationLengthFn> {
self.fixation_length.as_ref()
}
#[must_use]
pub const fn get_segmenter(&self) -> Option<&Arc<dyn Segmenter>> {
self.segmenter.as_ref()
}
#[must_use]
pub fn ratio(&self) -> f64 {
#[allow(clippy::cast_precision_loss)]
let percent = self.percent() as f64;
percent / 100.0
}
pub(crate) const fn percent(&self) -> usize {
PERCENT[(self.fixation - 1) as usize]
}
pub(crate) fn segmenter_impl(&self) -> &dyn Segmenter {
match &self.segmenter {
Some(segmenter) => segmenter.as_ref(),
None => &SPEC_SEGMENTER,
}
}
}
impl fmt::Debug for Options {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Options")
.field("fixation", &self.fixation)
.field("saccade", &self.saccade)
.field("min_word_length", &self.min_word_length)
.field("emphasize_numbers", &self.emphasize_numbers)
.field("locale", &self.locale)
.field(
"fixation_length",
&self.fixation_length.as_ref().map(|_| "<custom>"),
)
.field("segmenter", &self.segmenter.as_ref().map(|_| "<custom>"))
.finish()
}
}