use core::fmt;

use super::{Dir, Query, VPath};

/// This Dir exists to serve as a compatibility layer for very old links that end with .html.
pub struct HtmlCanonicalize<T> {
    inner: T,
}

impl<T> HtmlCanonicalize<T> {
    pub fn new(inner: T) -> Self {
        Self { inner }
    }
}

impl<T> Dir for HtmlCanonicalize<T>
where
    T: Dir,
{
    fn query(&self, path: &VPath, query: &mut Query) {
        let mut path = path.to_owned();
        if path.extension() == Some("html") {
            path.set_extension("");
        }

        self.inner.query(&path, query);
    }
}

impl<T> fmt::Debug for HtmlCanonicalize<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HtmlCanonicalize({:?})", self.inner)
    }
}
