pub type Array<T> = Vec<T>;
pub trait Truthy {
type Value;
fn truthy(self) -> Option<Self::Value>;
}
impl Truthy for bool {
type Value = bool;
fn truthy(self) -> Option<bool> {
self.then_some(true)
}
}
impl<T> Truthy for Option<T> {
type Value = T;
fn truthy(self) -> Option<T> {
self
}
}
impl<T, E> Truthy for Result<T, E> {
type Value = T;
fn truthy(self) -> Option<T> {
self.ok()
}
}
#[macro_export]
macro_rules! or_else {
($a:expr, @value $b:expr) => {
match $crate::values::Truthy::truthy($a) {
::core::option::Option::Some(__value) => __value,
::core::option::Option::None => $b,
}
};
($a:expr, $b:expr) => {
match $crate::values::Truthy::truthy($a) {
::core::option::Option::Some(__value) => __value,
#[allow(clippy::useless_conversion)]
::core::option::Option::None => ::core::convert::Into::into($b),
}
};
}
pub trait ToS {
fn to_s(&self) -> String;
}
impl<T: std::fmt::Display + ?Sized> ToS for T {
fn to_s(&self) -> String {
self.to_string()
}
}
pub trait ToI {
fn to_i(&self) -> i64;
}
pub trait ToF {
fn to_f(&self) -> f64;
}
fn leading_number(text: &str, fraction: bool) -> &str {
let text = text.trim_start();
let bytes = text.as_bytes();
let mut end = usize::from(matches!(bytes.first(), Some(b'-' | b'+')));
let digits = |from: usize| {
bytes[from..]
.iter()
.take_while(|b| b.is_ascii_digit())
.count()
};
let whole = digits(end);
end += whole;
if fraction && bytes.get(end) == Some(&b'.') {
let after = digits(end + 1);
if after > 0 {
end += 1 + after;
}
}
if whole == 0 && !text[..end].contains('.') {
return "";
}
&text[..end]
}
impl ToI for str {
fn to_i(&self) -> i64 {
leading_number(self, false).parse().unwrap_or(0)
}
}
impl ToF for str {
fn to_f(&self) -> f64 {
leading_number(self, true).parse().unwrap_or(0.0)
}
}
impl ToI for String {
fn to_i(&self) -> i64 {
self.as_str().to_i()
}
}
impl ToF for String {
fn to_f(&self) -> f64 {
self.as_str().to_f()
}
}
macro_rules! numbers {
($($t:ty),*) => {$(
impl ToI for $t {
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap, clippy::cast_lossless)]
fn to_i(&self) -> i64 {
*self as i64
}
}
impl ToF for $t {
#[allow(clippy::cast_precision_loss, clippy::cast_lossless)]
fn to_f(&self) -> f64 {
*self as f64
}
}
)*};
}
numbers!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, f32, f64);
impl<T: ToI + ?Sized> ToI for &T {
fn to_i(&self) -> i64 {
(**self).to_i()
}
}
impl<T: ToF + ?Sized> ToF for &T {
fn to_f(&self) -> f64 {
(**self).to_f()
}
}
impl<T: ToI> ToI for Option<T> {
fn to_i(&self) -> i64 {
self.as_ref().map_or(0, ToI::to_i)
}
}
impl<T: ToF> ToF for Option<T> {
fn to_f(&self) -> f64 {
self.as_ref().map_or(0.0, ToF::to_f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_gives_the_number_it_starts_with() {
assert_eq!("42".to_i(), 42);
assert_eq!(" +7 days".to_i(), 7);
assert_eq!("-12.9".to_i(), -12);
assert_eq!("abc".to_i(), 0);
assert_eq!("".to_i(), 0);
assert_eq!("-".to_i(), 0);
assert_eq!("3.25 kg".to_f(), 3.25);
assert_eq!("3.".to_f(), 3.0);
assert_eq!(".5".to_f(), 0.5);
assert_eq!(".".to_f(), 0.0);
assert_eq!("x".to_f(), 0.0);
assert_eq!(Some("8").to_i(), 8);
assert_eq!(None::<&str>.to_i(), 0);
assert_eq!(String::from("9").to_i(), 9);
}
}