pub use if_empty_derive::IfEmpty;
pub trait IfEmpty {
fn if_empty(self, val: Self) -> Self;
}
pub trait IfEmptyBorrowed {
fn if_empty<'a>(&'a self, val: &'a Self) -> &'a Self;
}
impl IfEmptyBorrowed for str {
fn if_empty<'a>(&'a self, input: &'a Self) -> &'a Self {
if self.is_empty() {
input
} else {
self
}
}
}
impl IfEmpty for String {
fn if_empty(self, input: Self) -> Self {
if self.is_empty() {
input
} else {
self
}
}
}
impl IfEmptyBorrowed for std::ffi::OsStr {
fn if_empty<'a>(&'a self, input: &'a Self) -> &'a Self {
if self.is_empty() {
input
} else {
self
}
}
}
impl IfEmpty for std::ffi::OsString {
fn if_empty(self, input: Self) -> Self {
if self.is_empty() {
input
} else {
self
}
}
}
#[cfg(test)]
mod tests {
use std::ffi::{OsStr, OsString};
use crate::{IfEmpty, IfEmptyBorrowed};
#[test]
fn string() {
let string = String::default();
assert!(string.is_empty());
let replacement = "text".to_string();
let replaced = string.if_empty(replacement.clone());
assert!(!replaced.is_empty());
assert_eq!(replacement, replaced);
let string = "not empty".to_string();
assert!(!string.is_empty());
assert_eq!("not empty", string.if_empty("should not be returned".to_string()));
}
#[test]
fn str() {
let string: &str = "";
assert!(string.is_empty());
let replacement = "text";
let replaced = string.if_empty(replacement);
assert!(!replaced.is_empty());
assert_eq!(replacement, replaced);
let string: &str = "not empty";
assert!(!string.is_empty());
assert_eq!("not empty", string.if_empty("should not be returned"));
}
#[test]
fn os_string() {
let string = OsString::default();
assert!(string.is_empty());
let replacement = OsString::from("text");
let replaced = string.if_empty(replacement.clone());
assert!(!replaced.is_empty());
assert_eq!(replacement, replaced);
let string = OsString::from("not empty");
assert!(!string.is_empty());
assert_eq!(
OsString::from("not empty"),
string.if_empty(OsString::from("should not be returned"))
);
}
#[test]
fn os_str() {
let string = OsStr::new("");
assert!(string.is_empty());
let replacement = OsStr::new("text");
let replaced = string.if_empty(replacement);
assert!(!replaced.is_empty());
assert_eq!(replacement, replaced);
let string = OsStr::new("not empty");
assert!(!string.is_empty());
}
#[test]
fn custom() {
struct Fake {
value: bool,
}
impl IfEmpty for Fake {
fn if_empty(self, value: Self) -> Self {
if self.value {
self
} else {
value
}
}
}
let f = Fake {
value: false,
};
assert!(
f.if_empty(Fake {
value: true
})
.value
);
let f = Fake {
value: true,
};
assert!(
f.if_empty(Fake {
value: false
})
.value
);
}
#[test]
fn derive_macro() {
#[derive(IfEmpty)]
struct Example {
value: String,
}
impl Example {
fn is_empty(&self) -> bool {
self.value.is_empty()
}
}
let e = Example {
value: String::new(),
};
assert_eq!(
e.if_empty(Example {
value: "not empty".to_string(),
})
.value,
"not empty"
);
let e = Example {
value: "a string".to_string(),
};
assert_eq!(
e.if_empty(Example {
value: "not empty".to_string(),
})
.value,
"a string"
);
}
}