use std::borrow::Cow;
use crate::Error;
#[must_use]
pub fn terminal_width(text: &str, ambiguous_wide: bool) -> usize {
crate::width::terminal_width_opts(text, ambiguous_wide)
}
#[must_use]
pub fn grapheme_width(cluster: &str, ambiguous_wide: bool) -> usize {
crate::width::grapheme_width_opts(cluster, ambiguous_wide)
}
#[must_use]
pub fn collapse_whitespace(text: &str, strip_control: bool, strip_zero_width: bool) -> String {
crate::whitespace::collapse_whitespace(text, strip_control, strip_zero_width)
}
#[must_use]
pub fn strip_control_chars(text: &str) -> String {
crate::whitespace::strip_control_chars(text)
}
#[must_use]
pub fn strip_zero_width_chars(text: &str) -> String {
crate::whitespace::strip_zero_width_chars(text)
}
#[must_use]
pub fn is_zalgo(text: &str, threshold: usize) -> bool {
crate::zalgo::is_zalgo(text, threshold)
}
#[must_use]
pub fn strip_zalgo(text: &str, max_marks: usize) -> String {
crate::zalgo::strip_zalgo(text, max_marks)
}
#[must_use]
pub fn fold_case(text: &str) -> Cow<'_, str> {
crate::case_fold::fold_case_cow(text)
}
#[must_use]
pub fn grapheme_len(text: &str) -> usize {
crate::grapheme::grapheme_len(text)
}
#[must_use]
pub fn grapheme_split(text: &str) -> Vec<String> {
crate::grapheme::grapheme_split(text)
}
pub fn graphemes(text: &str) -> impl Iterator<Item = &str> {
crate::grapheme::clusters(text)
}
#[must_use]
pub fn grapheme_truncate(text: &str, max_graphemes: usize) -> String {
crate::grapheme::truncate_to_graphemes(text, max_graphemes)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NormalizationForm {
Nfc,
Nfd,
Nfkc,
Nfkd,
}
impl NormalizationForm {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
NormalizationForm::Nfc => "NFC",
NormalizationForm::Nfd => "NFD",
NormalizationForm::Nfkc => "NFKC",
NormalizationForm::Nfkd => "NFKD",
}
}
}
impl std::fmt::Display for NormalizationForm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for NormalizationForm {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"NFC" => Ok(Self::Nfc),
"NFD" => Ok(Self::Nfd),
"NFKC" => Ok(Self::Nfkc),
"NFKD" => Ok(Self::Nfkd),
_ => Err(Error::from(crate::ErrorRepr::InvalidNormForm {
got: s.to_owned(),
})),
}
}
}
#[must_use]
pub fn normalize(text: &str, form: NormalizationForm) -> String {
crate::normalize::normalize(text, form.as_str())
.expect("NormalizationForm is always a valid form")
}
#[must_use]
pub fn is_normalized(text: &str, form: NormalizationForm) -> bool {
crate::normalize::is_normalized(text, form.as_str())
.expect("NormalizationForm is always a valid form")
}
#[must_use]
pub fn escape_html(text: &str) -> Cow<'_, str> {
crate::encoders::escape_html_str(text)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UrlComponent {
Path,
Segment,
Query,
Form,
}
impl UrlComponent {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
UrlComponent::Path => "path",
UrlComponent::Segment => "segment",
UrlComponent::Query => "query",
UrlComponent::Form => "form",
}
}
}
impl std::fmt::Display for UrlComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for UrlComponent {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"path" => Ok(Self::Path),
"segment" => Ok(Self::Segment),
"query" => Ok(Self::Query),
"form" => Ok(Self::Form),
_ => Err(Error::from(crate::ErrorRepr::InvalidUrlComponent {
got: s.to_owned(),
})),
}
}
}
#[must_use]
pub fn percent_encode(text: &str, component: UrlComponent) -> String {
crate::encoders::percent_encode_str(text, component.as_str())
.expect("UrlComponent always names a known component")
}
pub use crate::slugify::SlugConfig;
#[must_use]
pub fn slugify(text: &str, config: &SlugConfig) -> String {
crate::slugify::slugify_impl(text, config)
}
#[must_use]
pub fn demojize(text: &str, strip_modifiers: bool) -> String {
crate::emoji::demojize_rust(text, strip_modifiers)
}