[core] API: scaena-core public API (canon, claims, hooks), bodies pending

This commit is contained in:
2026-11-23 01:15:41 +01:00
parent 5dd554d52e
commit 6f30efb79b
8 changed files with 832 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
//! Hook templates: `brand/hooks.md` lines `- [H<n>] <template>` with slots from a fixed set.
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
/// The only slot names a template may use.
pub const SLOTS: [&str; 7] = ["app", "feature", "pain", "number", "platform", "clicks", "audience"];
/// Largest hooks file accepted, in bytes.
pub const MAX_FILE_BYTES: usize = 64 * 1024;
/// One hook template. `id` is `H<n>`; `slots` are unique, in order of first use.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HookTemplate {
pub id: String,
pub text: String,
pub slots: Vec<String>,
pub genome: Genome,
}
/// How a hook opens.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Opening {
Question,
Statement,
Number,
Command,
}
/// A simple genome derived from a hook's text (Day 4 evolves it).
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Genome {
pub opening: Opening,
/// Words, a slot counts as one.
pub words: usize,
/// A digit or the `{number}` slot.
pub has_number: bool,
/// The first word, lowercased, when the hook opens with a command.
pub lead_verb: Option<String>,
}
impl Genome {
/// Derive the genome from a template or a filled hook.
pub fn of(text: &str) -> Genome {
let _ = text;
todo!()
}
}
/// A hooks file error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum HookError {
#[error("the file is {bytes} bytes; the limit is 65536")]
TooLarge { bytes: usize },
#[error("line {line}: forbidden character U+{codepoint:04X} (control, hidden or direction-changing)")]
ForbiddenCharacter { line: usize, codepoint: u32 },
#[error("line {line}: a line starting '- [H' must be '- [H<n>] <template>'")]
Malformed { line: usize },
#[error("line {line}: hook number must be 1-9999 without leading zeros")]
IdOutOfRange { line: usize },
#[error("line {line}: hook {id} appears twice")]
Duplicate { line: usize, id: String },
#[error("line {line}: unknown slot '{{{slot}}}'")]
UnknownSlot { line: usize, slot: String },
#[error("line {line}: unbalanced '{{' or '}}'")]
UnbalancedBrace { line: usize },
#[error("line {line}: hook has no text")]
Empty { line: usize },
#[error("no value for slot '{{{slot}}}'")]
MissingValue { slot: String },
}
/// Parse `brand/hooks.md`. Lines that do not start `- [H` are ignored (headings, prose).
pub fn parse(md: &str) -> Result<Vec<HookTemplate>, HookError> {
let _ = md;
todo!()
}
impl HookTemplate {
/// Fill every slot. A missing value is an error; braces inside values are not expanded again.
pub fn fill(&self, values: &BTreeMap<String, String>) -> Result<String, HookError> {
let _ = values;
todo!()
}
}