Skip to main content

brep_plm/
lib.rs

1//! The BREP PLM server.
2//!
3//! Parts have a permanent identity and a configurable number; revisions carry
4//! the lifecycle and the CAD document; a draft is edited under an explicit
5//! checkout; a released revision is immutable. The HTTP surface is in
6//! [`api`], the rules are in [`db`] and [`lifecycle`], and the records are in
7//! [`model`].
8//!
9//! # What is built
10//!
11//! The spine, plus the administrator's scripting layer ([`scripting`]): part
12//! types number by counter, free text, pattern or script
13//! ([`model::NumberMode`]); revision labels are free text; every part carries a
14//! fixed [`model::DocumentClass`]; and hooks — part number, revision label,
15//! before release, after release, review reviewers and review events — let an
16//! organization override the defaults with JavaScript files in a directory it
17//! can keep in git.
18//!
19//! The catalog ([`catalog`]): a tree of categories, each adding typed
20//! attributes to what it inherits from its parent; every part sits in one
21//! category, carries free tags, and holds attribute values that are
22//! type-checked when written and must be complete before a release.
23//!
24//! Sourcing ([`sourcing`]): manufacturers and suppliers, and on each part its
25//! manufacturer parts with their supplier offers. Administrator settings
26//! ([`model::Settings`]) decide whether a part may have several revisions in
27//! work and whether a released part's catalog values lock.
28//!
29//! Families and templates ([`family`], [`seed`]): the family page, Generate
30//! from the family table's rows — from the CAD app with its documents, or
31//! assembled here and queued for a headless CAD worker's bake ([`bake_worker`])
32//! — CSV import into a family's table, and template spin-out.
33//!
34//! Assembly structure ([`bom`], [`bom_config`]): each revision's "uses" list —
35//! the one the CAD app publishes on save — and the indented and flat BOM, named
36//! BOM column configurations, the diff between
37//! two revisions' lists, where-used, and an administrator's release gate on
38//! unreleased children, all computed from it.
39//!
40//! Security ([`security`], `api::credentials`, [`tls`]): CSRF tokens on browser
41//! sessions, scoped API tokens for everything else, a sign-in throttle, session
42//! limits, password change and sign-out everywhere, response headers, `Secure`
43//! cookies, optional native TLS, and a switch for the in-browser script editor.
44//!
45//! The audit log ([`audit`]): every change the store commits, found by diffing
46//! what the change touched before and after, attributed to the request's
47//! actor, committed with the change and read through `/api/audit`.
48//!
49//! Storage ([`sql`], [`table`]): the metadata in one SQLite file, each change
50//! one transaction with its audit events; a trigram full-text index over what
51//! a person searches a part by; and paged part listings. A data directory's
52//! schema version must match exactly; there is no upgrade path. Attachments
53//! ([`attach`]), per-user workspaces ([`workspace`]) and backups, restore and
54//! export ([`backup`]) sit beside it.
55//!
56//! Review and approval ([`review`]): submitting a revision opens a review
57//! round judged by the settings' rule or the nearest override, reviewers
58//! approve or reject, a rejection sends the revision back to Draft, release
59//! waits for the rule, and every revision carries a threaded discussion. One
60//! process holds a data directory at a time ([`dirlock`]).
61//!
62//! Workflows ([`workflow`]): versioned workflow definitions and durable
63//! executions with forms, reviews and script steps. Jobs ([`jobs`]): shop-floor
64//! builds of released manufacturing instructions, signed off step by step. The
65//! tools library ([`tools`]): the hand tools and fixtures instruction steps
66//! name.
67//!
68//! A part records where it came from ([`model::Origin`]) and an
69//! external reference; the KiCad library setup stamps the parts it imports.
70//!
71//! # The document API
72//!
73//! `/api/store/*` is deliberately shaped to what the CAD app's `StoreBackend`
74//! requires of a remote backend: a metadata-only index (so a client never
75//! hydrates the corpus), a per-key read, a write, a delete, and a monotonic
76//! change sequence with a "what moved" feed. The CAD app's PLM client stores
77//! through it.
78//!
79//! [`api::router`] builds the `axum` router over a [`db::Db`] (opened with
80//! [`db::Db::open`]); the `brep-plm` binary serves it. See the README for the
81//! command line and an embedding example.
82
83pub mod api;
84pub mod attach;
85pub mod audit;
86pub mod auth;
87pub mod backup;
88pub mod bake_worker;
89pub mod bom;
90pub mod bom_config;
91pub mod cad;
92pub mod catalog;
93pub mod db;
94pub mod desktop_link;
95pub mod dirlock;
96pub mod family;
97pub mod jobs;
98pub mod journal;
99pub mod lifecycle;
100pub mod replace;
101pub mod review;
102pub mod model;
103pub mod player;
104pub mod scripting;
105pub mod security;
106pub mod seed;
107pub mod sourcing;
108pub mod sql;
109pub mod table;
110pub mod thumbnail;
111pub mod geometry;
112pub mod tls;
113pub mod tools;
114pub mod version;
115pub mod workspace;
116
117use axum::http::StatusCode;
118use axum::response::{IntoResponse, Response};
119use axum::Json;
120
121/// A refusal, with the status the API answers it with and the message a person
122/// reads. Every refusal in this server carries a reason — a bare status code
123/// tells the user nothing about what to do next.
124#[derive(Debug, Clone)]
125pub struct Error {
126    pub status: StatusCode,
127    pub message: String,
128}
129
130impl Error {
131    pub fn bad_request(message: impl Into<String>) -> Self {
132        Self { status: StatusCode::BAD_REQUEST, message: message.into() }
133    }
134
135    pub fn unauthorized(message: impl Into<String>) -> Self {
136        Self { status: StatusCode::UNAUTHORIZED, message: message.into() }
137    }
138
139    pub fn forbidden(message: impl Into<String>) -> Self {
140        Self { status: StatusCode::FORBIDDEN, message: message.into() }
141    }
142
143    pub fn not_found(what: impl std::fmt::Display) -> Self {
144        Self { status: StatusCode::NOT_FOUND, message: format!("no such {what}") }
145    }
146
147    /// A rule refused it — a lock held elsewhere, an immutable revision, an
148    /// exhausted number scheme. 409 is the status the CAD app's write-behind
149    /// lane will surface as a notice.
150    pub fn conflict(message: impl Into<String>) -> Self {
151        Self { status: StatusCode::CONFLICT, message: message.into() }
152    }
153
154    /// Too many failed sign-ins: the caller must wait.
155    pub fn too_many(message: impl Into<String>) -> Self {
156        Self { status: StatusCode::TOO_MANY_REQUESTS, message: message.into() }
157    }
158
159    pub fn internal(error: impl std::fmt::Display) -> Self {
160        Self {
161            status: StatusCode::INTERNAL_SERVER_ERROR,
162            message: error.to_string(),
163        }
164    }
165}
166
167impl std::fmt::Display for Error {
168    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169        write!(f, "{}: {}", self.status, self.message)
170    }
171}
172
173impl std::error::Error for Error {}
174
175impl IntoResponse for Error {
176    fn into_response(self) -> Response {
177        (
178            self.status,
179            Json(serde_json::json!({ "error": self.message })),
180        )
181            .into_response()
182    }
183}
184
185pub mod identity;
186
187pub mod workflow;