Skip to main content

specfold_core/placement/
paths.rs

1//! Where a document's files go for a given owner, type and name.
2
3use crate::config::Config;
4use crate::model::Tier;
5use crate::model::doctype::{Form, KeyKind, TypeDef};
6use crate::workspace::{Owner, Owners};
7
8/// The stem of a document: `<key>-<slug>` for timestamp types, `<slug>` for slug types.
9pub fn stem(ty: &TypeDef, key: &str, slug: &str) -> String {
10    match ty.key {
11        KeyKind::Stamp => format!("{key}-{slug}"),
12        KeyKind::Slug => slug.to_string(),
13    }
14}
15
16/// The type directory of `owner`, relative to the repository root.
17pub fn type_dir(cfg: &Config, owners: &Owners, owner: &Owner, tier: Tier, ty: &TypeDef) -> String {
18    match tier {
19        Tier::Committed => format!("{}/{}", owners.spec_dir(owner, cfg.spec_dir()), ty.name),
20        Tier::Local => format!("{}/{}/{}", cfg.local_dir(), owner.id, ty.name),
21    }
22}
23
24/// The main file of a document. Directory-form types, and documents that need a bundle, get
25/// `<stem>/index.<type>.md`.
26pub fn main_file(type_dir: &str, ty: &TypeDef, stem: &str, bundle: bool) -> String {
27    if bundle || ty.form == Form::Dir {
28        format!("{type_dir}/{stem}/index.{}.md", ty.name)
29    } else {
30        format!("{type_dir}/{stem}.{}.md", ty.name)
31    }
32}