use std::path::{Path, PathBuf};
use crate::error::{CoreError, ErrorCode};
use crate::model::TomlDocument;
use crate::model::ids::{self, IdKind};
use crate::model::settings::Settings;
use crate::model::skill_meta::SkillMeta;
use crate::skills::frontmatter::{self, Frontmatter};
use crate::skills::link::remove_link;
use crate::skills::{
Entry, SKILL_FILE, SkillFiles, WARN_TOTAL_BYTES, entry, io_error, read_folder, summarize_folder,
};
use crate::store::{Store, StoreRead, Transaction};
pub const SKILLS_DIR: &str = "skills";
pub const META_DIR: &str = "skill-meta";
const SETTINGS_FILE: &str = "settings.toml";
pub fn meta_path(id: &str) -> Result<String, CoreError> {
ids::check(IdKind::Skill, id)?;
Ok(format!("{META_DIR}/{id}.toml"))
}
pub fn content_dir(store: &Store<'_>, id: &str) -> Result<PathBuf, CoreError> {
ids::check(IdKind::Skill, id)?;
Ok(store.root().join(SKILLS_DIR).join(id))
}
#[derive(Debug, Clone)]
pub struct StoreSkill {
pub id: String,
pub meta: Option<SkillMeta>,
pub frontmatter: Option<Frontmatter>,
pub bytes: u64,
pub has_content: bool,
pub problems: Vec<CoreError>,
}
impl StoreSkill {
pub fn name(&self) -> &str {
self.frontmatter
.as_ref()
.and_then(Frontmatter::name)
.or_else(|| self.meta.as_ref().and_then(|meta| meta.name.as_deref()))
.unwrap_or(&self.id)
}
pub fn description(&self) -> Option<&str> {
self.frontmatter.as_ref().and_then(Frontmatter::description)
}
}
pub fn read_meta(read: &dyn StoreRead, id: &str) -> Result<Option<SkillMeta>, CoreError> {
let relative = meta_path(id)?;
let Some(text) = read.read_file(&relative)? else {
return Ok(None);
};
let doc = TomlDocument::<SkillMeta>::parse(&text)
.map_err(|diagnostic| diagnostic.to_core_error(Some(Path::new(&relative))))?;
if let Some(err) = doc
.value()
.validate(Some(id))
.first_error(Some(Path::new(&relative)))
{
return Err(err);
}
Ok(Some(doc.into_value()))
}
pub fn stage_meta(tx: &mut Transaction<'_>, meta: &SkillMeta) -> Result<(), CoreError> {
let relative = meta_path(&meta.id)?;
let text = TomlDocument::new(meta.clone()).to_text()?;
tx.write(&relative, text)
}
pub const MAX_TAG_CHARS: usize = 20;
pub fn set_tags(
store: &Store<'_>,
id: &str,
tags: &[String],
) -> Result<Option<crate::store::CommitId>, CoreError> {
ids::check(IdKind::Skill, id)?;
let mut clean: Vec<String> = Vec::new();
for tag in tags {
let tag = tag.trim();
if tag.is_empty() {
continue;
}
if tag.chars().count() > MAX_TAG_CHARS || tag.chars().any(char::is_control) {
return Err(CoreError::new(
ErrorCode::InvalidValue,
format!("tag {tag:?} is longer than {MAX_TAG_CHARS} characters or not one line"),
));
}
if !clean.iter().any(|known| known.eq_ignore_ascii_case(tag)) {
clean.push(tag.to_owned());
}
}
let operation = crate::store::Operation::new("skill.tag", format!("skill: tag {id}"))
.entity(format!("skill/{id}"));
let committed = store.transaction(operation, |tx| {
if !exists(tx, id)? {
return Err(not_found(id));
}
let mut meta = read_meta(tx, id)?.unwrap_or_else(|| SkillMeta::new(id));
meta.tags = (!clean.is_empty()).then(|| clean.clone());
stage_meta(tx, &meta)
})?;
Ok(committed.commit)
}
pub fn stage_content(
tx: &mut Transaction<'_>,
id: &str,
files: &SkillFiles,
) -> Result<(), CoreError> {
let tree = worktree(tx.store(), id)?;
if let Some(link) = tree.links.iter().find(|link| {
files
.keys()
.any(|path| link.is_empty() || path == *link || path.starts_with(&format!("{link}/")))
}) {
let dir = content_dir(tx.store(), id)?;
return Err(CoreError::new(
ErrorCode::InvalidPath,
"the skill's store folder holds a link where a skill file goes; remove the link",
)
.with_path(link.split('/').fold(dir, |path, part| path.join(part))));
}
for relative in tree.files {
if !files.contains_key(&relative) {
tx.remove(&format!("{SKILLS_DIR}/{id}/{relative}"))?;
}
}
for (relative, bytes) in files {
tx.write(&format!("{SKILLS_DIR}/{id}/{relative}"), bytes.clone())?;
}
Ok(())
}
pub fn stage_remove(tx: &mut Transaction<'_>, id: &str) -> Result<(), CoreError> {
for relative in worktree(tx.store(), id)?.files {
tx.remove(&format!("{SKILLS_DIR}/{id}/{relative}"))?;
}
let meta = meta_path(id)?;
if tx.exists(&meta)? {
tx.remove(&meta)?;
}
Ok(())
}
pub fn prune_empty_dirs(store: &Store<'_>, id: &str) -> Result<(), CoreError> {
let dir = content_dir(store, id)?;
let fs = store.fs();
match entry(fs, &dir)? {
Entry::Link(_) => remove_link(fs, &dir),
Entry::Dir if prune(fs, &dir)? => fs
.remove_dir_all(&dir)
.map_err(|err| io_error("cannot remove folder", &dir, &err)),
_ => Ok(()),
}
}
fn prune(fs: &dyn crate::fs::Fs, dir: &Path) -> Result<bool, CoreError> {
let mut empty = true;
for path in fs
.read_dir(dir)
.map_err(|err| io_error("cannot list directory", dir, &err))?
{
match entry(fs, &path)? {
Entry::Link(_) => remove_link(fs, &path)?,
Entry::Dir if prune(fs, &path)? => fs
.remove_dir_all(&path)
.map_err(|err| io_error("cannot remove folder", &path, &err))?,
Entry::Missing => {}
Entry::Dir | Entry::File => empty = false,
}
}
Ok(empty)
}
struct Worktree {
files: Vec<String>,
links: Vec<String>,
}
fn worktree(store: &Store<'_>, id: &str) -> Result<Worktree, CoreError> {
let dir = content_dir(store, id)?;
let mut tree = Worktree {
files: Vec::new(),
links: Vec::new(),
};
match entry(store.fs(), &dir)? {
Entry::Link(_) => tree.links.push(String::new()),
Entry::Dir => collect(store, &dir, "", &mut tree)?,
Entry::Missing | Entry::File => {}
}
Ok(tree)
}
fn collect(
store: &Store<'_>,
dir: &Path,
prefix: &str,
tree: &mut Worktree,
) -> Result<(), CoreError> {
let fs = store.fs();
let entries = fs
.read_dir(dir)
.map_err(|err| io_error("cannot list directory", dir, &err))?;
for path in entries {
let Some(name) = path.file_name().and_then(|name| name.to_str()) else {
continue;
};
let relative = if prefix.is_empty() {
name.to_owned()
} else {
format!("{prefix}/{name}")
};
match entry(fs, &path)? {
Entry::Link(_) => tree.links.push(relative),
Entry::Dir => collect(store, &path, &relative, tree)?,
Entry::File => tree.files.push(relative),
Entry::Missing => {}
}
}
Ok(())
}
pub fn read_content(store: &Store<'_>, id: &str) -> Result<SkillFiles, CoreError> {
let dir = content_dir(store, id)?;
match entry(store.fs(), &dir)? {
Entry::Dir => read_folder(store.fs(), &dir),
Entry::Link(_) => Err(CoreError::new(
ErrorCode::InvalidPath,
format!("the store folder of skill {id:?} is a link"),
)
.with_path(&dir)),
Entry::Missing | Entry::File => Err(not_found(id)),
}
}
pub fn ids(store: &Store<'_>) -> Result<Vec<String>, CoreError> {
let fs = store.fs();
let mut ids: Vec<String> = Vec::new();
let dir = store.root().join(SKILLS_DIR);
if fs.is_dir(&dir) {
for path in fs
.read_dir(&dir)
.map_err(|err| io_error("cannot list directory", &dir, &err))?
{
if fs.is_dir(&path)
&& let Some(name) = path.file_name().and_then(|name| name.to_str())
&& ids::is_valid(IdKind::Skill, name)
{
ids.push(name.to_owned());
}
}
}
for name in store.list_files(META_DIR)? {
if let Some(id) = name.strip_suffix(".toml")
&& ids::is_valid(IdKind::Skill, id)
&& !ids.iter().any(|known| known == id)
{
ids.push(id.to_owned());
}
}
ids.sort();
Ok(ids)
}
pub fn list(store: &Store<'_>) -> Result<Vec<StoreSkill>, CoreError> {
ids(store)?.into_iter().map(|id| load(store, &id)).collect()
}
pub fn get(store: &Store<'_>, id: &str) -> Result<Option<StoreSkill>, CoreError> {
let dir = content_dir(store, id)?;
if !store.fs().is_dir(&dir) && !store.fs().exists(&store.path(&meta_path(id)?)?) {
return Ok(None);
}
load(store, id).map(Some)
}
fn load(store: &Store<'_>, id: &str) -> Result<StoreSkill, CoreError> {
let fs = store.fs();
let dir = content_dir(store, id)?;
let mut problems = Vec::new();
let meta = match read_meta(store, id) {
Ok(meta) => meta,
Err(err) => {
problems.push(err);
None
}
};
let found = entry(fs, &dir)?;
let mut has_content = found == Entry::Dir;
let mut bytes = 0;
let mut frontmatter = None;
if has_content {
match summarize_folder(fs, &dir) {
Ok(summary) => {
bytes = summary.bytes;
match &summary.skill_file {
Some(text) => match frontmatter::parse(&String::from_utf8_lossy(text)) {
Ok(parsed) => frontmatter = parsed,
Err(err) => problems.push(err.with_path(dir.join(SKILL_FILE))),
},
None => problems.push(missing_skill_file(&dir)),
}
}
Err(err) => {
has_content = false;
problems.push(err);
}
}
} else if let Entry::Link(_) = found {
problems.push(
CoreError::new(
ErrorCode::InvalidPath,
format!("the store folder of skill {id:?} is a link; it is never distributed"),
)
.with_path(&dir),
);
} else {
problems.push(
CoreError::new(
ErrorCode::NotFound,
format!("skill {id:?} has metadata but no content folder"),
)
.with_path(&dir),
);
}
Ok(StoreSkill {
id: id.to_owned(),
meta,
frontmatter,
bytes,
has_content,
problems,
})
}
pub(crate) fn missing_skill_file(dir: &Path) -> CoreError {
CoreError::new(
ErrorCode::NotFound,
format!("the skill has no {SKILL_FILE}"),
)
.with_path(dir)
}
pub(crate) fn not_found(id: &str) -> CoreError {
CoreError::new(ErrorCode::NotFound, format!("skill {id:?} does not exist"))
}
pub fn exists(read: &Transaction<'_>, id: &str) -> Result<bool, CoreError> {
let store = read.store();
Ok(store.fs().is_dir(&content_dir(store, id)?) || read.exists(&meta_path(id)?)?)
}
pub fn free_id(read: &Transaction<'_>, base: &str) -> Result<String, CoreError> {
if !exists(read, base)? {
return Ok(base.to_owned());
}
for n in 2..1000 {
let candidate = format!("{base}-{n}");
if ids::is_valid(IdKind::Skill, &candidate) && !exists(read, &candidate)? {
return Ok(candidate);
}
}
Err(CoreError::new(
ErrorCode::AlreadyExists,
format!("no free id for skill {base:?}"),
))
}
pub fn total_size_warning(store: &Store<'_>) -> Option<CoreError> {
size_warning(&list(store).ok()?)
}
pub fn size_warning(skills: &[StoreSkill]) -> Option<CoreError> {
let total: u64 = skills.iter().map(|skill| skill.bytes).sum();
(total > WARN_TOTAL_BYTES).then(|| {
CoreError::new(
ErrorCode::InvalidValue,
format!(
"store skills take {} MB, more than {} MB",
total / (1024 * 1024),
WARN_TOTAL_BYTES / (1024 * 1024)
),
)
})
}
pub fn settings(read: &dyn StoreRead) -> Settings {
read.read_file(SETTINGS_FILE)
.ok()
.flatten()
.and_then(|text| TomlDocument::<Settings>::parse(&text).ok())
.map(TomlDocument::into_value)
.unwrap_or_default()
}