use core::fmt::{self, Display, Formatter};
#[cfg(feature = "std")]
use std::path::Path;
#[allow(unused_imports)] use crate::add::separator::{ColonSpace, WithSep};
use crate::{Format, add::separator::WithColonSpace};
use super::WithContext;
pub type WithContextColon<C, E> = WithContext<C, E, Colon>;
#[cfg(feature = "std")]
pub type WithContextPathColon<C, E> = WithContext<C, E, PathColon>;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ContextField;
impl<C: Display, E, WithContextFormat> Format<WithContext<C, E, WithContextFormat>>
for ContextField
{
fn fmt(w: &WithContext<C, E, WithContextFormat>, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&w.context, f)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ErrorField;
impl<C, E: Display, WithContextFormat> Format<WithContext<C, E, WithContextFormat>> for ErrorField {
fn fmt(w: &WithContext<C, E, WithContextFormat>, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&w.error, f)
}
}
#[cfg(feature = "std")]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ContextPath;
#[cfg(feature = "std")]
impl<P: AsRef<Path>, E, WithContextFormat> Format<WithContext<P, E, WithContextFormat>>
for ContextPath
{
fn fmt(w: &WithContext<P, E, WithContextFormat>, f: &mut Formatter<'_>) -> fmt::Result {
w.context.as_ref().display().fmt(f)
}
}
pub type Colon = WithColonSpace<ContextField, ErrorField>;
#[cfg(feature = "std")]
pub type PathColon = WithColonSpace<ContextPath, ErrorField>;
#[cfg(test)]
mod tests {
use super::*;
use crate::{WithContext, separator::WithSpace, tests::Inner};
#[cfg(feature = "std")]
use std::io;
#[cfg(feature = "std")]
#[test]
fn test_path_colon_strategy() {
use std::path::{Path, PathBuf};
let io_err = io::Error::new(io::ErrorKind::NotFound, "file missing");
let w = WithContext::<_, _, PathColon>::new(PathBuf::from("a/b/c.txt"), io_err);
assert_eq!(w.to_string(), "a/b/c.txt: file missing");
let io_err = io::Error::new(io::ErrorKind::NotFound, "file missing");
let path: &Path = Path::new("a/b/c.txt");
let w = WithContext::<_, _, PathColon>::new(path, io_err);
assert_eq!(w.to_string(), "a/b/c.txt: file missing");
}
#[test]
fn test_composed_separator() {
type SpacePair = WithSpace<ContextField, ErrorField>;
let w = WithContext::<_, _, SpacePair>::new("ctx", Inner::A);
assert_eq!(w.to_string(), "ctx InnerA");
}
}