use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use super::Graph;
use crate::fs::ReadStorage;
use crate::index::IdIndex;
use crate::link;
const SIDECAR_EXTENSIONS: &[&str] = &["yaml", "yml", "json", "toml", "fig", "figl"];
pub fn sidecar_candidates(payload: &Path) -> impl Iterator<Item = PathBuf> + '_ {
let name = payload
.file_name()
.and_then(|n| n.to_str())
.unwrap_or_default();
SIDECAR_EXTENSIONS
.iter()
.map(move |ext| payload.with_file_name(format!("{name}.{ext}")))
}
impl<FS: ReadStorage, Ix: IdIndex> Graph<FS, Ix> {
pub async fn sidecar_claims(&self, candidate: &Path, payload: &Path) -> bool {
let Ok((_, doc)) = self.load(candidate).await else {
return false;
};
let Some(content) = doc.content_attr() else {
return false;
};
let dir = candidate.parent().unwrap_or(Path::new(""));
doc.is_attachment() && link::normalize(dir.join(content)) == payload
}
pub async fn is_shadowed_payload(&self, path: &Path, listing: &BTreeSet<PathBuf>) -> bool {
for candidate in sidecar_candidates(path) {
if listing.contains(&candidate) && self.sidecar_claims(&candidate, path).await {
return true;
}
}
false
}
}