feat(workspace): R2 facade operations for the editor rules, the publish check and the sheet index data

Free ops: sheet.check, report.new, report.add_observation,
report.remove_observation, report.move_observation, vertebra.select.
Sheet ops: index_data, zone.click, zone.add_sides, zone.remove_at,
vertebra.initial, vertebra.sync, finding.toggle. Input contracts exported
to TypeScript; a JavaScript index that names no position removes nothing
in the filters and is refused by the move (the TypeScript swap would have
written a hole). Shipped module 999 KB raw, 319 KB gzip (budget 600 KB).
This commit is contained in:
bchanot committed 2026-10-14 00:19:20 +02:00
1 parent 3639c8778e
commit 6c0533c1da
25 files changed
+470 -11

No files matched your search

+3 -7
View File
@@ -42,7 +42,7 @@ pub fn call(op: &str, input: &str) -> String {
.map_err(|error| Failure::new("invalid_json", error)),
"sheet_ref" => echo::<SheetRef>(input),
"report_zone" => echo::<ReportZone>(input),
_ => Err(unknown(op)),
_ => crate::ops::free(op, input).unwrap_or_else(|| Err(unknown(op))),
};
render(outcome)
}
@@ -57,12 +57,8 @@ pub fn load_sheet(bundle: &str) -> Result<LoadedSheet, Failure> {
}
/// Runs the operation `op` that needs the session's sheet.
pub fn call_with_sheet(sheet: &LoadedSheet, op: &str, _input: &str) -> String {
let outcome = match op {
"sheet.ref" => to_value(&sheet.reference),
_ => Err(unknown(op)),
};
render(outcome)
pub fn call_with_sheet(sheet: &LoadedSheet, op: &str, input: &str) -> String {
render(crate::ops::on_sheet(sheet, op, input).unwrap_or_else(|| Err(unknown(op))))
}
fn unknown(op: &str) -> Failure {
+1
View File
@@ -8,6 +8,7 @@
pub mod domain;
pub mod facade;
pub mod json;
pub mod ops;
/// Version of the core, reported by the wasm facade and the worker.
pub const CORE_VERSION: &str = env!("CARGO_PKG_VERSION");
+276
View File
@@ -0,0 +1,276 @@
//! The operations the facade exposes (R2 plan rev 2 §5): one input contract and one handler
//! per user action or load, each returning a closed failure instead of panicking. Free
//! operations take everything they need; sheet operations take the session's sheet.
use serde::Deserialize;
use serde_json::Value;
#[cfg(feature = "ts")]
use ts_rs::TS;
use crate::domain::editor::{
self, EditorError, LeafClick, MoveDirection, NewReportDraft, NewReportStamp, VertebraAction,
};
use crate::domain::report::{LateralSide, Observation, Report, SheetRef, Vertebra};
use crate::domain::sheet::{LoadedSheet, SheetIndexData, check_sheet_document};
use crate::facade::{Failure, parse_input, to_value};
type Outcome = Result<Value, Failure>;
/// An index as JavaScript sent it: a number. Only a non-negative integer names a position.
#[derive(Debug, Clone, Copy, Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export, type = "number"))]
pub struct JsIndex(f64);
impl JsIndex {
/// The position, or `None` for a negative or fractional number.
fn position(self) -> Option<usize> {
let value = self.0;
let integral = value >= 0.0 && value.fract() == 0.0 && value < 4_294_967_296.0;
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
integral.then_some(value as usize)
}
/// For the filters (`filter((_, i) => i !== index)`): a number that names no position
/// removes nothing, as an index past the end does.
fn or_none(self) -> usize {
self.position().unwrap_or(usize::MAX)
}
}
fn editor_failure(error: EditorError) -> Failure {
Failure::new(error.kind(), "")
}
// ---------------------------------------------------------------------------
// Free operations
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct NewReportInput {
pub draft: NewReportDraft,
pub sheet: SheetRef,
pub id: String,
pub created_at: String,
pub observation_id: String,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct ReportIdInput {
pub report: Report,
pub id: String,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct ReportIndexInput {
pub report: Report,
pub index: JsIndex,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct MoveObservationInput {
pub report: Report,
pub index: JsIndex,
pub direction: MoveDirection,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct VertebraSelectInput {
pub selection: Vec<Vertebra>,
pub action: VertebraAction,
}
/// Runs a free operation; `None` when `op` is not one of them.
pub fn free(op: &str, input: &str) -> Option<Outcome> {
Some(match op {
"sheet.check" => sheet_check(input),
"report.new" => report_new(input),
"report.add_observation" => report_add(input),
"report.remove_observation" => report_remove(input),
"report.move_observation" => report_move(input),
"vertebra.select" => vertebra_select(input),
_ => return None,
})
}
fn sheet_check(input: &str) -> Outcome {
let document: Value = parse_input(input)?;
let check =
check_sheet_document(document).map_err(|error| Failure::new("invalid_shape", error))?;
to_value(&check)
}
fn report_new(input: &str) -> Outcome {
let input: NewReportInput = parse_input(input)?;
let stamp = NewReportStamp {
id: &input.id,
created_at: &input.created_at,
observation_id: &input.observation_id,
};
to_value(&editor::new_report(input.draft, input.sheet, &stamp))
}
fn report_add(input: &str) -> Outcome {
let input: ReportIdInput = parse_input(input)?;
to_value(&editor::add_observation(&input.report, &input.id))
}
fn report_remove(input: &str) -> Outcome {
let input: ReportIndexInput = parse_input(input)?;
to_value(&editor::remove_observation(
&input.report,
input.index.or_none(),
))
}
fn report_move(input: &str) -> Outcome {
let input: MoveObservationInput = parse_input(input)?;
let index = input
.index
.position()
.ok_or_else(|| editor_failure(EditorError::InvalidIndex))?;
editor::move_observation(&input.report, index, input.direction)
.map_err(editor_failure)
.and_then(|report| to_value(&report))
}
fn vertebra_select(input: &str) -> Outcome {
let input: VertebraSelectInput = parse_input(input)?;
editor::select_vertebrae(&input.selection, &input.action)
.map_err(editor_failure)
.and_then(|selection| to_value(&selection))
}
// ---------------------------------------------------------------------------
// Sheet operations
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct ZoneClickInput {
pub observation: Option<Observation>,
pub annotation_id: String,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct AddSidesInput {
pub observation: Observation,
pub annotation_id: String,
pub sides: Vec<LateralSide>,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct ObservationIndexInput {
pub observation: Observation,
pub index: JsIndex,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct VertebraSyncInput {
pub observation: Observation,
pub selection: Vec<Vertebra>,
pub view_id: String,
}
#[derive(Deserialize)]
#[cfg_attr(feature = "ts", derive(TS), ts(export))]
#[serde(rename_all = "camelCase")]
pub struct FindingToggleInput {
pub observation: Observation,
pub finding_zone_id: String,
pub path: Vec<String>,
pub leaf_position: JsIndex,
#[serde(default, deserialize_with = "crate::domain::js::present")]
#[cfg_attr(feature = "ts", ts(optional))]
pub side: Option<LateralSide>,
}
/// Runs an operation on the session's sheet; `None` when `op` is not one of them.
pub fn on_sheet(sheet: &LoadedSheet, op: &str, input: &str) -> Option<Outcome> {
Some(match op {
"index_data" => to_value(&SheetIndexData::of(sheet)),
"zone.click" => zone_click(sheet, input),
"zone.add_sides" => zone_add_sides(sheet, input),
"zone.remove_at" => zone_remove_at(input),
"vertebra.initial" => vertebra_initial(sheet, input),
"vertebra.sync" => vertebra_sync(sheet, input),
"finding.toggle" => finding_toggle(sheet, input),
_ => return None,
})
}
fn zone_click(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: ZoneClickInput = parse_input(input)?;
let click = editor::zone_click(
&sheet.indexed(),
input.observation.as_ref(),
&input.annotation_id,
);
to_value(&click)
}
fn zone_add_sides(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: AddSidesInput = parse_input(input)?;
editor::add_sides(
&sheet.indexed(),
&input.observation,
&input.annotation_id,
&input.sides,
)
.map_err(editor_failure)
.and_then(|observation| to_value(&observation))
}
fn zone_remove_at(input: &str) -> Outcome {
let input: ObservationIndexInput = parse_input(input)?;
to_value(&editor::remove_zone_at(
&input.observation,
input.index.or_none(),
))
}
fn vertebra_initial(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: ObservationIndexInput = parse_input(input)?;
to_value(&editor::vertebra_initial(
&sheet.indexed(),
&input.observation,
input.index.or_none(),
))
}
fn vertebra_sync(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: VertebraSyncInput = parse_input(input)?;
to_value(&editor::sync_vertebrae(
&sheet.indexed(),
&input.observation,
&input.selection,
&input.view_id,
))
}
fn finding_toggle(sheet: &LoadedSheet, input: &str) -> Outcome {
let input: FindingToggleInput = parse_input(input)?;
let click = LeafClick {
finding_zone_id: &input.finding_zone_id,
path: &input.path,
leaf_position: input.leaf_position.or_none(),
side: input.side,
};
editor::toggle_finding(&sheet.indexed(), &input.observation, &click)
.map_err(editor_failure)
.and_then(|observation| to_value(&observation))
}