mod set;
mod operators;
mod query;
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
pub use set::{Flags, FlagsMarker};
pub use query::Query;
#[doc(hidden)]
pub use ecow::EcoString;
#[doc(hidden)]
pub use identconv::lower_strify;
#[doc(hidden)]
pub use convert_case;
static MATCH_RATIO: AtomicUsize = AtomicUsize::new(8);
pub fn set_debug_match_ratio(value: usize) {
MATCH_RATIO.store(value, Relaxed)
}
#[cfg(not(feature = "debug"))]
#[doc(hidden)]
#[inline(always)]
pub fn str_eq(a: &str, b: &str) -> bool {
a == b
}
#[cfg(feature = "debug")]
#[doc(hidden)]
#[inline(always)]
pub fn str_eq(a: &str, b: &str) -> bool {
if a.len() > 3 && b.len() > 3 {
if levenshtein::levenshtein(a, b) <= (a.len() + b.len()) / MATCH_RATIO.load(Relaxed) {
log::warn!("{} and {} are similar, maybe a typo?", a, b)
}
}
a == b
}
#[macro_export]
macro_rules! str_enum {
($(#[$main_attr:meta])* $vis:vis $name: ident: [$($(#[$attr: meta])* $fields: ident),* $(,)?]) => {
#[derive(Debug, Clone, Eq, Hash)]
$(#[$main_attr])*
$vis struct $name(::strflags::EcoString);
const _: () = {
#[allow(non_upper_case_globals)]
impl $name {
$($(#[$attr])*
pub const $fields: Self = Self(::strflags::EcoString::inline(
::strflags::lower_strify!($fields)
));)*
#[inline]
pub fn new(s: &str) -> Self {
use ::strflags::convert_case::{Casing, Case::*};
Self(s.to_case(Flat).into())
}
}
impl ::std::fmt::Display for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0.as_str())
}
}
impl ::std::str::FromStr for $name {
type Err=::std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err>{
Ok(Self::new(s))
}
}
impl Into<::strflags::Flags<Self>> for $name {
fn into(self) -> ::strflags::Flags<Self> {
::strflags::Flags::new(self)
}
}
impl std::borrow::Borrow<str> for $name {
fn borrow(&self) -> &str {
use std::borrow::Borrow;
self.0.borrow()
}
}
impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl PartialEq<str> for $name {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl<T: AsRef<str>> ::std::cmp::PartialEq<T> for $name {
fn eq(&self, other: &T) -> bool {
::strflags::str_eq(self.0.as_ref(), other.as_ref())
}
}
};
::strflags::impl_serde!($name);
};
}
#[macro_export]
macro_rules! str_flags {
($(#[$main_attr: meta])* $vis:vis $name: ident: [$($(#[$attr: meta])* $fields: ident),* $(,)?]) => {
::strflags::str_enum!($(#[$main_attr])* $vis $name: [$($(#[$attr])* $fields),*]);
const _: () = {
impl ::strflags::FlagsMarker for $name {}
impl ::std::ops::BitOr for $name {
type Output = ::strflags::Flags<Self>;
fn bitor(self, rhs: Self) -> Self::Output {
::strflags::Flags::pair(self, rhs)
}
}
impl ::std::ops::BitOr<::strflags::Flags<Self>> for $name {
type Output = ::strflags::Flags<Self>;
fn bitor(self, rhs: ::strflags::Flags<Self>) -> Self::Output {
rhs | self
}
}
impl ::std::ops::BitAnd<::strflags::Flags<Self>> for $name {
type Output = ::strflags::Flags<Self>;
fn bitand(self, rhs: ::strflags::Flags<Self>) -> Self::Output {
rhs & self
}
}
impl ::std::ops::BitXor<::strflags::Flags<Self>> for $name {
type Output = ::strflags::Flags<Self>;
fn bitxor(self, rhs: ::strflags::Flags<Self>) -> Self::Output {
rhs ^ self
}
}
};
}
}
#[cfg(not(feature="serde"))]
#[doc(hidden)]
#[macro_export]
macro_rules! impl_serde {
($name: ident) => {}
}
#[cfg(feature="serde")]
#[doc(hidden)]
#[macro_export]
macro_rules! impl_serde {
($name: ident) => {
const _: () = {
use ::serde::{Serialize, Deserialize};
impl Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
self.0.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> {
let s = <::std::borrow::Cow<str>>::deserialize(deserializer)?;
Ok(Self::new(s.as_ref()))
}
}
};
}
}