use std::fmt;

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

pub struct Anchored<T> {
    inner: T,
    at: VPathBuf,
}

impl<T> Anchored<T> {
    pub fn new(inner: T, at: VPathBuf) -> Self {
        Self { inner, at }
    }
}

impl<T> Dir for Anchored<T>
where
    T: Dir,
{
    fn query(&self, path: &VPath, query: &mut Query) {
        query.provide(|| Anchor {
            path: self.at.join(path),
        });

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

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