use crate::{Add, Format};
use core::fmt;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NewLine;
impl<E: ?Sized> Format<E> for NewLine {
fn fmt(_: &E, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("\n")
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Space;
impl<E: ?Sized> Format<E> for Space {
fn fmt(_: &E, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(" ")
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Empty;
impl<E: ?Sized> Format<E> for Empty {
fn fmt(_: &E, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ColonChar;
impl<E: ?Sized> Format<E> for ColonChar {
fn fmt(_: &E, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(":")
}
}
pub type ColonSpace = Add<ColonChar, Space>;
pub type WithSep<L, Sep, R> = Add<Add<L, Sep>, R>;
pub type WithSpace<L, R> = WithSep<L, Space, R>;
pub type WithNewLine<L, R> = WithSep<L, NewLine, R>;
pub type WithColonSpace<L, R> = WithSep<L, ColonSpace, R>;
#[cfg(test)]
mod tests {
use super::*;
use crate::{
Add, Formatted, OneLine,
tests::{Error, Inner},
};
#[test]
fn test_space_between_repeats() {
let error = Error::One;
assert_eq!(
Formatted::<_, Add<OneLine, Add<Space, OneLine>>>::new(error).to_string(),
"One One"
);
}
#[test]
fn test_colon_space_alias() {
let error = Error::One;
assert_eq!(
Formatted::<_, Add<OneLine, Add<ColonSpace, OneLine>>>::new(error).to_string(),
"One: One"
);
}
#[test]
fn test_with_space_alias() {
let error = Error::One;
assert_eq!(
Formatted::<_, WithSpace<OneLine, OneLine>>::new(error).to_string(),
"One One"
);
}
#[test]
fn test_with_newline_alias() {
let error = Error::Two(Inner::A);
assert_eq!(
Formatted::<_, WithNewLine<OneLine, OneLine>>::new(error).to_string(),
"Two: InnerA\nTwo: InnerA"
);
}
#[test]
fn test_with_colon_space_alias() {
let error = Error::One;
assert_eq!(
Formatted::<_, WithColonSpace<OneLine, OneLine>>::new(error).to_string(),
"One: One"
);
}
#[test]
fn test_add_sep_generic_alias() {
let error = Error::One;
assert_eq!(
Formatted::<_, WithSep<OneLine, ColonChar, OneLine>>::new(error).to_string(),
"One:One"
);
}
}