[core] API: scaena-core public API (canon, claims, hooks), bodies pending
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "scaena-core"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.85"
|
||||
license = "CPAL-1.0"
|
||||
description = "Pure core of Scaena: canon parser, claim checker, hook templates. No I/O."
|
||||
repository = "https://gitea.marek-novak.cz/mnovak/ScaenaPublic"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
thiserror = "2"
|
||||
unicode-segmentation = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
proptest = "1"
|
||||
@@ -0,0 +1,200 @@
|
||||
// SPDX-License-Identifier: CPAL-1.0
|
||||
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
||||
|
||||
//! Canon files: `apps/<id>.md`, the only facts Praeco Domi may state.
|
||||
//!
|
||||
//! Format (strict):
|
||||
//!
|
||||
//! ```text
|
||||
//! ---
|
||||
//! id: tabula
|
||||
//! name: Tabula
|
||||
//! one_liner: Notes that stay on your own computer.
|
||||
//! audience: People who want notes without an account.
|
||||
//! price: (assumption) one-time purchase
|
||||
//! clicks: New note in 1 click.
|
||||
//! status: released
|
||||
//! aula: https://aula.marek-novak.cz/tabula/
|
||||
//! ---
|
||||
//!
|
||||
//! # Tabula
|
||||
//! - [F1] Tabula keeps notes on your own computer.
|
||||
//! - [F2] It syncs between your devices without a cloud account. (assumption)
|
||||
//! ```
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Largest canon file accepted, in bytes.
|
||||
pub const MAX_FILE_BYTES: usize = 64 * 1024;
|
||||
|
||||
/// Highest fact number (`[F9999]`).
|
||||
pub const MAX_FACT_ID: u32 = 9999;
|
||||
|
||||
/// A parsed canon file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CanonFile {
|
||||
pub id: String,
|
||||
pub front: FrontMatter,
|
||||
pub facts: Vec<Fact>,
|
||||
}
|
||||
|
||||
/// The front matter of a canon file. Values are kept as written, including a leading `(assumption) `.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct FrontMatter {
|
||||
pub name: String,
|
||||
pub one_liner: String,
|
||||
pub audience: String,
|
||||
pub price: String,
|
||||
pub clicks: String,
|
||||
pub status: Status,
|
||||
pub aula: Option<String>,
|
||||
pub site: Option<String>,
|
||||
}
|
||||
|
||||
/// Release status of an app.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Status {
|
||||
Released,
|
||||
Beta,
|
||||
Planned,
|
||||
}
|
||||
|
||||
/// One fact line `- [F<id>] <text>`. `text` has the trailing ` (assumption)` marker removed;
|
||||
/// `assumption` says whether the fact is unconfirmed. `line` is 1-based (0 for ad-hoc facts).
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Fact {
|
||||
pub id: u32,
|
||||
pub text: String,
|
||||
pub assumption: bool,
|
||||
pub line: usize,
|
||||
}
|
||||
|
||||
/// A canon error: the 1-based line it was found on (0 = the whole file) and what is wrong.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
#[error("line {line}: {code}")]
|
||||
pub struct CanonError {
|
||||
pub line: usize,
|
||||
pub code: CanonCode,
|
||||
}
|
||||
|
||||
/// What is wrong with a canon file.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
#[non_exhaustive]
|
||||
pub enum CanonCode {
|
||||
#[error("the file is {bytes} bytes; the limit is 65536")]
|
||||
TooLarge { bytes: usize },
|
||||
#[error("forbidden character U+{codepoint:04X} (control, hidden or direction-changing)")]
|
||||
ForbiddenCharacter { codepoint: u32 },
|
||||
#[error("a carriage return that is not part of a CRLF line ending")]
|
||||
LoneCarriageReturn,
|
||||
#[error("the file must start with a '---' line")]
|
||||
NoFrontMatter,
|
||||
#[error("the front matter is not closed with a '---' line")]
|
||||
UnclosedFrontMatter,
|
||||
#[error("front matter lines must be 'key: value'")]
|
||||
FrontMatterLine,
|
||||
#[error("unknown front matter key '{0}'")]
|
||||
UnknownKey(String),
|
||||
#[error("front matter key '{0}' appears twice")]
|
||||
DuplicateKey(String),
|
||||
#[error("front matter key '{0}' is missing")]
|
||||
MissingKey(String),
|
||||
#[error("front matter key '{0}' has no value")]
|
||||
EmptyValue(String),
|
||||
#[error("'{0}' is not a valid app id (1-32 of a-z 0-9 -, no leading or trailing -)")]
|
||||
BadId(String),
|
||||
#[error("id '{id}' does not match the file name '{stem}'")]
|
||||
IdMismatch { id: String, stem: String },
|
||||
#[error("status '{0}' is not one of released, beta, planned")]
|
||||
BadStatus(String),
|
||||
#[error("'{0}' is not a single https:// URL")]
|
||||
BadUrl(String),
|
||||
#[error("a line starting '- [F' must be '- [F<n>] <text>'")]
|
||||
MalformedFact,
|
||||
#[error("fact number must be 1-9999 without leading zeros")]
|
||||
FactIdOutOfRange,
|
||||
#[error("fact F{0} appears twice")]
|
||||
DuplicateFact(u32),
|
||||
#[error("fact has no text")]
|
||||
EmptyFact,
|
||||
#[error("app '{0}' is already in the canon")]
|
||||
DuplicateApp(String),
|
||||
}
|
||||
|
||||
/// Parse `apps/<file_stem>.md`. Strict: any deviation from the format is an error.
|
||||
pub fn parse(file_stem: &str, text: &str) -> Result<CanonFile, CanonError> {
|
||||
let _ = (file_stem, text);
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// True if `id` is a valid app id: 1-32 of `[a-z0-9-]`, no leading or trailing `-`.
|
||||
pub fn is_valid_app_id(id: &str) -> bool {
|
||||
let _ = id;
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// True if a front matter value is marked `(assumption) `.
|
||||
pub fn is_assumption_value(value: &str) -> bool {
|
||||
let _ = value;
|
||||
todo!()
|
||||
}
|
||||
|
||||
impl CanonFile {
|
||||
/// The fact with this number.
|
||||
pub fn fact(&self, id: u32) -> Option<&Fact> {
|
||||
let _ = id;
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Allowed URLs of this file: `aula`, `site`, and every `https://` URL inside a fact's text. Sorted, unique.
|
||||
pub fn urls(&self) -> Vec<String> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// An in-memory canon file (for the `release` source). Facts count as confirmed. Not validated.
|
||||
pub fn adhoc(id: &str, name: &str, facts: Vec<(u32, String)>) -> CanonFile {
|
||||
let _ = (id, name, facts);
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// The set of canon files, keyed by app id.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Canon {
|
||||
files: BTreeMap<String, CanonFile>,
|
||||
}
|
||||
|
||||
impl Canon {
|
||||
pub fn new() -> Self {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Add a file. A second file with the same id is an error.
|
||||
pub fn insert(&mut self, f: CanonFile) -> Result<(), CanonError> {
|
||||
let _ = f;
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn get(&self, app: &str) -> Option<&CanonFile> {
|
||||
let _ = app;
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// All files, sorted by id.
|
||||
pub fn apps(&self) -> impl Iterator<Item = &CanonFile> {
|
||||
self.files.values()
|
||||
}
|
||||
|
||||
pub fn fact(&self, app: &str, id: u32) -> Option<&Fact> {
|
||||
let _ = (app, id);
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// The union of every file's [`CanonFile::urls`].
|
||||
pub fn allowed_urls(&self) -> BTreeSet<String> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// SPDX-License-Identifier: CPAL-1.0
|
||||
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
||||
|
||||
//! The claim checker.
|
||||
//!
|
||||
//! Every sentence that states a fact carries at least one `[app#Fn]` reference (several may be adjacent:
|
||||
//! `[tabula#F3][tabula#F7]`) at its end, and every reference must point to a confirmed canon fact.
|
||||
//! Fail closed: a sentence states a fact unless it is one of a short list of plain phrases
|
||||
//! ("Here's what's new.", "Try it today.").
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::canon::Canon;
|
||||
|
||||
/// A claim reference `[app#F<fact>]`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
pub struct Ref {
|
||||
pub app: String,
|
||||
pub fact: u32,
|
||||
}
|
||||
|
||||
/// Why a text fails the claim check.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[non_exhaustive]
|
||||
pub enum Problem {
|
||||
/// A sentence that states a fact without any reference.
|
||||
Unsourced { sentence: String },
|
||||
/// The app exists, the fact does not.
|
||||
MissingFact { r: Ref },
|
||||
/// No canon file for the app.
|
||||
UnknownApp { r: Ref },
|
||||
/// The fact is marked `(assumption)`.
|
||||
Unconfirmed { r: Ref },
|
||||
/// Something that looks like a reference but is not `[app#Fn]`.
|
||||
MalformedRef { text: String },
|
||||
/// A URL that is not in [`Canon::allowed_urls`].
|
||||
UrlNotInCanon { url: String },
|
||||
/// A control, hidden or direction-changing character (addition to the Day 1 contract).
|
||||
HiddenCharacter { codepoint: u32 },
|
||||
}
|
||||
|
||||
impl Problem {
|
||||
/// Plain English, for "Needs facts".
|
||||
pub fn reason(&self) -> String {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
/// The result of [`check`].
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Report {
|
||||
/// Every well-formed reference found, sorted and unique.
|
||||
pub refs: Vec<Ref>,
|
||||
pub problems: Vec<Problem>,
|
||||
}
|
||||
|
||||
impl Report {
|
||||
pub fn ok(&self) -> bool {
|
||||
self.problems.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Check generated text against the canon.
|
||||
pub fn check(text: &str, canon: &Canon) -> Report {
|
||||
let _ = (text, canon);
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Remove every well-formed `[app#Fn]` reference (and the spaces before it), for display and posting.
|
||||
pub fn strip_refs(text: &str) -> String {
|
||||
let _ = text;
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Split text into sentences the way [`check`] does.
|
||||
pub fn sentences(text: &str) -> Vec<String> {
|
||||
let _ = text;
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Whether [`check`] treats this sentence (without its references) as stating a fact. Fail closed.
|
||||
pub fn states_fact(sentence: &str, canon: &Canon) -> bool {
|
||||
let _ = (sentence, canon);
|
||||
todo!()
|
||||
}
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// SPDX-License-Identifier: CPAL-1.0
|
||||
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
||||
|
||||
//! `scaena-core`: the pure core of Scaena and Praeco Domi.
|
||||
//!
|
||||
//! - [`canon`]: the strict parser for canon files (`apps/<id>.md`), the only facts Praeco Domi may state.
|
||||
//! - [`claims`]: the claim checker. Every sentence that states a fact must carry an `[app#Fn]` reference to a
|
||||
//! confirmed canon fact. Fail closed: when unsure whether a sentence states a fact, it does.
|
||||
//! - [`hooks`]: hook templates (`brand/hooks.md`) with a fixed slot set, filling, and a simple genome.
|
||||
//!
|
||||
//! No I/O, no unsafe code.
|
||||
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
pub mod canon;
|
||||
pub mod claims;
|
||||
pub mod hooks;
|
||||
|
||||
mod text;
|
||||
@@ -0,0 +1,4 @@
|
||||
// SPDX-License-Identifier: CPAL-1.0
|
||||
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
||||
|
||||
//! Shared text scanning: forbidden characters, URLs, line splitting.
|
||||
Reference in New Issue
Block a user