use std::{
collections::{BTreeMap, BTreeSet},
fs,
path::Path,
};
use super::manifest::{MANIFEST, PackManifest, parse_manifest};
use crate::{Error, Result};
pub const BUILTIN: &str = "builtin:";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
Builtin(String),
Git(String),
}
impl Source {
pub fn parse(s: &str) -> Source {
if let Some(name) = s.strip_prefix(BUILTIN) {
Source::Builtin(name.to_string())
} else if !s.contains(':') && !s.contains('/') {
Source::Builtin(s.to_string())
} else {
Source::Git(s.to_string())
}
}
pub fn label(&self) -> String {
match self {
Source::Builtin(name) => format!("{BUILTIN}{name}"),
Source::Git(url) => url.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackFiles {
pub manifest: PackManifest,
pub files: BTreeMap<String, Vec<u8>>,
pub executable: BTreeSet<String>,
}
#[derive(Debug, Clone, Copy)]
pub struct EmbeddedPack {
pub dir: &'static str,
pub files: &'static [(&'static str, &'static str)],
}
pub static EMBEDDED: &[EmbeddedPack] = include!(concat!(env!("OUT_DIR"), "/packs.rs"));
impl EmbeddedPack {
pub fn read(&self) -> Result<PackFiles> {
let mut manifest = None;
let mut files = BTreeMap::new();
for (rel, text) in self.files {
if *rel == MANIFEST {
manifest = Some(parse_manifest(text, &format!("builtin:{}", self.dir))?);
} else {
files.insert(rel.to_string(), text.as_bytes().to_vec());
}
}
let manifest = manifest.ok_or_else(|| {
Error::Internal(format!("the embedded pack {} has no manifest", self.dir))
})?;
Ok(PackFiles {
manifest,
files,
executable: BTreeSet::new(),
})
}
}
pub fn embedded_packs(embedded: &[EmbeddedPack]) -> Result<Vec<PackManifest>> {
let mut out: Vec<PackManifest> = embedded
.iter()
.map(|p| p.read().map(|f| f.manifest))
.collect::<Result<_>>()?;
out.sort_by(|a, b| a.name.cmp(&b.name));
Ok(out)
}
pub fn find_embedded(embedded: &[EmbeddedPack], name: &str) -> Result<PackFiles> {
for pack in embedded {
let files = pack.read()?;
if files.manifest.name == name {
return Ok(files);
}
}
let names: Vec<String> = embedded_packs(embedded)?
.into_iter()
.map(|m| m.name)
.collect();
let list = if names.is_empty() {
"the binary embeds no pack".to_string()
} else {
format!("the embedded packs are {}", names.join(", "))
};
Err(Error::Pack(format!(
"no embedded pack is named `{name}`: {list}"
)))
}
pub fn read_dir(dir: &Path, label: &str) -> Result<PackFiles> {
if !dir.is_dir() {
return Err(Error::Pack(format!("{label} holds no {MANIFEST}")));
}
let mut files = BTreeMap::new();
collect(dir, "", label, &mut files)?;
let text = files
.remove(MANIFEST)
.ok_or_else(|| Error::Pack(format!("{label} holds no {MANIFEST}")))?;
let text = String::from_utf8(text)
.map_err(|_| Error::Pack(format!("{label}/{MANIFEST} is not UTF-8")))?;
let manifest = parse_manifest(&text, &format!("{label}/{MANIFEST}"))?;
Ok(PackFiles {
manifest,
files,
executable: BTreeSet::new(),
})
}
fn collect(
dir: &Path,
prefix: &str,
label: &str,
out: &mut BTreeMap<String, Vec<u8>>,
) -> Result<()> {
let entries = fs::read_dir(dir).map_err(|e| Error::io(dir, e))?;
for entry in entries {
let entry = entry.map_err(|e| Error::io(dir, e))?;
let name = entry.file_name().to_string_lossy().into_owned();
if name == ".git" {
continue;
}
let path = entry.path();
let rel = format!("{prefix}{name}");
let kind = fs::symlink_metadata(&path)
.map_err(|e| Error::io(&path, e))?
.file_type();
if kind.is_symlink() {
return Err(Error::Pack(format!(
"{label}: {rel} is a symbolic link, and a pack from git holds only files and directories"
)));
}
if kind.is_dir() {
collect(&path, &format!("{rel}/"), label, out)?;
} else {
let bytes = fs::read(&path).map_err(|e| Error::io(&path, e))?;
out.insert(rel, bytes);
}
}
Ok(())
}
pub fn clone_dir(worktree: &Path, path: &str) -> Result<std::path::PathBuf> {
let mut dir = worktree.to_path_buf();
for segment in path.split('/').filter(|s| !s.is_empty() && *s != ".") {
if segment == ".." {
return Err(Error::Pack(format!(
"--path {path}: the path leaves the repository"
)));
}
dir.push(segment);
let Ok(meta) = fs::symlink_metadata(&dir) else {
continue;
};
if meta.file_type().is_symlink() {
return Err(Error::Pack(format!(
"--path {path}: {segment} is a symbolic link, so the path can leave the repository"
)));
}
}
Ok(dir)
}
pub const FILES: &str = "files/";
pub fn destination(name: &str, store: Option<&str>, rel: &str) -> Option<String> {
if let Some(rest) = rel.strip_prefix(FILES) {
return Some(format!("/{rest}"));
}
if rel == "formats.yaml" {
return Some(super::lock::formats_of(name).as_str().to_string());
}
if rel == "instructions.md" {
return Some(super::lock::instructions_of(name).as_str().to_string());
}
if rel == "index.md" || rel.ends_with("/index.md") {
return None;
}
let store = store?.trim_end_matches('/');
if let Some(rest) = rel.strip_prefix("schemas/") {
return Some(format!("{store}/schemas/{rest}"));
}
rel.strip_prefix("documents/")
.map(|rest| format!("{store}/{rest}"))
}
pub fn origin(
name: &str,
store: Option<&str>,
roots: &[&str],
dest: &str,
) -> std::result::Result<String, String> {
if dest == super::lock::instructions_of(name).as_str() {
return Ok("instructions.md".into());
}
if dest == super::lock::formats_of(name).as_str() {
return Ok("formats.yaml".into());
}
let under = |r: &str| {
dest.strip_prefix(r.trim_end_matches('/'))?
.strip_prefix('/')
};
let store = store.map(|s| s.trim_end_matches('/'));
let holder = roots
.iter()
.copied()
.chain(store)
.map(|r| r.trim_end_matches('/'))
.filter(|r| under(r).is_some())
.max_by_key(|r| r.len());
let Some(holder) = holder else {
return Ok(format!("{FILES}{}", dest.trim_start_matches('/')));
};
match store {
Some(s) if s == holder => {
let rest = under(s).unwrap_or_default();
if rest == "index.md" || rest.ends_with("/index.md") {
Err("an index.md of a store is the listing that `sokf validate --fix` writes, and no pack holds one".into())
} else if rest == "manifest.sokf.yaml" {
Err("the manifest of a store belongs to the project, and no pack holds one".into())
} else if rest.starts_with("schemas/") {
Ok(rest.to_string())
} else {
Ok(format!("documents/{rest}"))
}
}
Some(s) => Err(format!(
"the file lies in the store {holder}, and the pack's store is {s}"
)),
None => Err(format!(
"the file lies in the store {holder}, and the pack has no store, so each of its files lies outside every store"
)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_source_without_a_colon_or_a_slash_is_an_embedded_pack() {
assert_eq!(Source::parse("sokf"), Source::Builtin("sokf".into()));
assert_eq!(
Source::parse("builtin:sokf"),
Source::Builtin("sokf".into())
);
assert_eq!(
Source::parse("git@example.com:o/r.git"),
Source::Git("git@example.com:o/r.git".into())
);
assert_eq!(Source::parse("../pack"), Source::Git("../pack".into()));
assert_eq!(Source::parse("sokf").label(), "builtin:sokf");
}
#[test]
fn each_file_lands_at_its_destination() {
let d = |rel| destination("m", Some("/knowledge"), rel);
assert_eq!(
d("schemas/issue.md").unwrap(),
"/knowledge/schemas/issue.md"
);
assert_eq!(
d("documents/documentation-standards/general.md").unwrap(),
"/knowledge/documentation-standards/general.md"
);
assert_eq!(d("formats.yaml").unwrap(), "/.sokf/packs/m/formats.yaml");
assert_eq!(
d("instructions.md").unwrap(),
"/.sokf/packs/m/instructions.md"
);
assert_eq!(d("README.md"), None);
assert_eq!(d("schemas/index.md"), None);
assert_eq!(d("files/rustfmt.toml").unwrap(), "/rustfmt.toml");
assert_eq!(
d("files/.github/workflows/ci.yml").unwrap(),
"/.github/workflows/ci.yml"
);
assert_eq!(d("files/docs/index.md").unwrap(), "/docs/index.md");
assert_eq!(destination("m", None, "schemas/issue.md"), None);
assert_eq!(
destination("m", None, "files/deny.toml").unwrap(),
"/deny.toml"
);
}
#[test]
fn each_file_maps_back_to_its_place_in_the_pack() {
let roots = ["/knowledge", "/docs/kb"];
let o = |dest| origin("m", Some("/knowledge"), &roots, dest);
for (dest, rel) in [
("/knowledge/schemas/runbook.md", "schemas/runbook.md"),
(
"/knowledge/recipes/recipe-a.md",
"documents/recipes/recipe-a.md",
),
("/knowledge/a.md", "documents/a.md"),
("/.sokf/packs/m/instructions.md", "instructions.md"),
("/.sokf/packs/m/formats.yaml", "formats.yaml"),
("/scripts/check.sh", "files/scripts/check.sh"),
("/docs/index.md", "files/docs/index.md"),
("/knowledge.toml", "files/knowledge.toml"),
] {
assert_eq!(o(dest).as_deref(), Ok(rel), "{dest}");
assert_eq!(
destination("m", Some("/knowledge"), rel).as_deref(),
Some(dest)
);
}
assert_eq!(
o("/.sokf/packs/other/formats.yaml").as_deref(),
Ok("files/.sokf/packs/other/formats.yaml")
);
assert_eq!(
origin("m", None, &roots, "/deny.toml").as_deref(),
Ok("files/deny.toml")
);
}
#[test]
fn a_file_of_another_store_or_an_index_has_no_place_in_the_pack() {
let roots = ["/knowledge", "/knowledge/nested", "/docs/kb"];
let o = |store, dest| origin("m", store, &roots, dest);
let e = o(Some("/knowledge"), "/knowledge/index.md").unwrap_err();
assert!(e.contains("index.md"), "{e}");
let e = o(Some("/knowledge"), "/knowledge/schemas/index.md").unwrap_err();
assert!(e.contains("index.md"), "{e}");
let e = o(Some("/knowledge"), "/knowledge/manifest.sokf.yaml").unwrap_err();
assert!(e.contains("the manifest of a store"), "{e}");
assert_eq!(
o(Some("/knowledge"), "/knowledge/notes/manifest.sokf.yaml").as_deref(),
Ok("documents/notes/manifest.sokf.yaml")
);
let e = o(Some("/knowledge"), "/docs/kb/a.md").unwrap_err();
assert!(e.contains("the store /docs/kb"), "{e}");
let e = o(Some("/knowledge"), "/knowledge/nested/a.md").unwrap_err();
assert!(e.contains("the store /knowledge/nested"), "{e}");
let e = o(None, "/knowledge/schemas/a.md").unwrap_err();
assert!(e.contains("the pack has no store"), "{e}");
}
static FAKE: &[EmbeddedPack] = &[EmbeddedPack {
dir: "fake",
files: &[
(
"pack.sokf.yaml",
"name: fake\nversion: 1.0.0\nrequires-sokf: 0.1.0\ndescription: A fake pack.\n",
),
("schemas/a.md", "a\n"),
],
}];
#[test]
fn an_unknown_embedded_pack_lists_the_embedded_packs() {
assert_eq!(find_embedded(FAKE, "fake").unwrap().files.len(), 1);
let e = find_embedded(FAKE, "other").unwrap_err().to_string();
assert!(e.contains("the embedded packs are fake"), "{e}");
let e = find_embedded(&[], "other").unwrap_err().to_string();
assert!(e.contains("the binary embeds no pack"), "{e}");
}
}