pub(crate) fn try_child<P>(&self, path: P) -> Result<Child<'_>, StripPrefixError> where
P: AsRef<Path>,
{ let path = path.as_ref(); if path.is_absolute() {
path.strip_prefix(&self.path)?;
}
Ok(Child {
root: self,
path: self.path.join(path),
})
}
#[track_caller] pub(crate) fn child<P>(&self, path: P) -> Child<'_> where
P: AsRef<Path>,
{ self.try_child(path)
.into_diagnostic()
.wrap_err("invariant violation: `path` is absolute and not a child of this file root")
.unwrap()
}
fn removed_dir<P>(&self, path: P) -> miette::Result<Child<'_>> where
P: AsRef<Path>,
{ let path = path.as_ref(); let child = self.child(path); if child.exists() {
log::info!("removing old contents of {child}…",);
log::trace!("removing directory {:?}", &*child);
fs::remove_dir_all(&*child)
.map_err(miette::Report::msg)
.wrap_err_with(|| format!("failed to remove old contents of {child}"))?;
}
Ok(child)
}
fn removed_file<P>(&self, path: P) -> miette::Result<Child<'_>> where
P: AsRef<Path>,
{ let path = path.as_ref(); let child = self.child(path); if child.exists() {
log::info!("removing old copy of {child}…",);
fs::remove_file(&*child)
.map_err(miette::Report::msg)
.wrap_err_with(|| format!("failed to remove old copy of {child}"))?;
}
Ok(child)
}
pub(crate) fn regen_dir<P>(
&self,
path: P, gen: impl FnOnce(&Child<'_>) -> miette::Result<()>,
) -> miette::Result<Child<'_>> where
P: AsRef<Path>,
{ let child = self.removed_dir(path)?; gen(&child)?;
ensure!(
child.is_dir(), "{} was not regenerated for an unknown reason",
child,
);
Ok(child)
}
pub(crate) fn regen_file<P>(
&self,
path: P, gen: impl FnOnce(&Child<'_>) -> miette::Result<()>,
) -> miette::Result<Child<'_>> where
P: AsRef<Path>,
{ let child = self.removed_file(path)?; gen(&child)?;
ensure!(
child.is_file(), "{} was not regenerated for an unknown reason",
child,
);
Ok(child)
}
}
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] pub(crate) struct Child<'a> {
root: &'a FileRoot, /// NOTE: This is always an absolute path that is a child of the `root`.
path: PathBuf,
}
#[track_caller] pub(crate) fn child<P>(&self, path: P) -> Self where
P: AsRef<Path>,
{ self.try_child(path)
.into_diagnostic()
.wrap_err("invariant violation: `path` is absolute and not a child of this child")
.unwrap()
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.