use std::{collections::BTreeMap, 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>>,
}
#[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 })
}
}
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 })
}
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 fn destination(name: &str, store: &str, rel: &str) -> Option<String> {
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}"))
}
#[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", "/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);
}
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}");
}
}