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 four hooks — part number, revision
15//! label, before release, after release — let an organization override the
16//! defaults with JavaScript files in a directory it can keep in git.
17//!
18//! The catalog ([`catalog`]): a tree of categories, each adding typed
19//! attributes to what it inherits from its parent; every part sits in one
20//! category, carries free tags, and holds attribute values that are
21//! type-checked when written and must be complete before a release.
22//!
23//! Sourcing ([`sourcing`]): manufacturers and suppliers, and on each part its
24//! manufacturer parts with their supplier offers. Administrator settings
25//! ([`model::Settings`]) decide whether a part may have several revisions in
26//! work and whether a released part's catalog values lock.
27//!
28//! Families and templates ([`family`], [`seed`]).
29//!
30//! Assembly structure ([`bom`]): each revision's "uses" list — the one the CAD
31//! app will publish on save — and the indented and flat BOM, the diff between
32//! two revisions' lists, where-used, and an administrator's release gate on
33//! unreleased children, all computed from it.
34//!
35//! Security ([`security`], `api::credentials`, [`tls`]): CSRF tokens on browser
36//! sessions, scoped API tokens for everything else, a sign-in throttle, session
37//! limits, password change and sign-out everywhere, response headers, `Secure`
38//! cookies, optional native TLS, and a switch for the in-browser script editor.
39//!
40//! The audit log ([`audit`]): every change the store commits, found by diffing
41//! what the change touched before and after, attributed to the request's
42//! actor, committed with the change and read through `/api/audit`.
43//!
44//! Storage ([`sql`], [`table`]): the metadata in one SQLite file, each change
45//! one transaction with its audit events; a trigram full-text index over what
46//! a person searches a part by; paged part listings; and a one-time import
47//! from the JSON-file store earlier versions wrote.
48//!
49//! Review and approval ([`review`]): submitting a revision opens a review
50//! round judged by the settings' rule or the nearest override, reviewers
51//! approve or reject, a rejection sends the revision back to Draft, release
52//! waits for the rule, and every revision carries a threaded discussion. One
53//! process holds a data directory at a time ([`dirlock`]).
54//!
55//! Library import is designed but not built — where the design named a field it
56//! will need ([`model::Origin`], `external_ref`), the field is here and inert.
57//!
58//! # The document API
59//!
60//! `/api/store/*` is deliberately shaped to what the CAD app's `StoreBackend`
61//! requires of a remote backend: a metadata-only index (so a client never
62//! hydrates the corpus), a per-key read, a write, a delete, and a monotonic
63//! change sequence with a "what moved" feed. The CAD app is not touched by
64//! this crate; this is the surface it will adopt.
65
66pub mod api;
67pub mod attach;
68pub mod audit;
69pub mod auth;
70pub mod backup;
71pub mod bake_worker;
72pub mod bom;
73pub mod bom_config;
74pub mod cad;
75pub mod catalog;
76pub mod db;
77pub mod dirlock;
78pub mod family;
79pub mod journal;
80pub mod lifecycle;
81pub mod replace;
82pub mod review;
83pub mod model;
84pub mod scripting;
85pub mod security;
86pub mod seed;
87pub mod sourcing;
88pub mod sql;
89pub mod table;
90pub mod thumbnail;
91pub mod geometry;
92pub mod tls;
93pub mod version;
94pub mod workspace;
95
96use axum::http::StatusCode;
97use axum::response::{IntoResponse, Response};
98use axum::Json;
99
100/// A refusal, with the status the API answers it with and the message a person
101/// reads. Every refusal in this server carries a reason — a bare status code
102/// tells the user nothing about what to do next.
103#[derive(Debug, Clone)]
104pub struct Error {
105 pub status: StatusCode,
106 pub message: String,
107}
108
109impl Error {
110 pub fn bad_request(message: impl Into<String>) -> Self {
111 Self { status: StatusCode::BAD_REQUEST, message: message.into() }
112 }
113
114 pub fn unauthorized(message: impl Into<String>) -> Self {
115 Self { status: StatusCode::UNAUTHORIZED, message: message.into() }
116 }
117
118 pub fn forbidden(message: impl Into<String>) -> Self {
119 Self { status: StatusCode::FORBIDDEN, message: message.into() }
120 }
121
122 pub fn not_found(what: impl std::fmt::Display) -> Self {
123 Self { status: StatusCode::NOT_FOUND, message: format!("no such {what}") }
124 }
125
126 /// A rule refused it — a lock held elsewhere, an immutable revision, an
127 /// exhausted number scheme. 409 is the status the CAD app's write-behind
128 /// lane will surface as a notice.
129 pub fn conflict(message: impl Into<String>) -> Self {
130 Self { status: StatusCode::CONFLICT, message: message.into() }
131 }
132
133 /// Too many failed sign-ins: the caller must wait.
134 pub fn too_many(message: impl Into<String>) -> Self {
135 Self { status: StatusCode::TOO_MANY_REQUESTS, message: message.into() }
136 }
137
138 pub fn internal(error: impl std::fmt::Display) -> Self {
139 Self {
140 status: StatusCode::INTERNAL_SERVER_ERROR,
141 message: error.to_string(),
142 }
143 }
144}
145
146impl std::fmt::Display for Error {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 write!(f, "{}: {}", self.status, self.message)
149 }
150}
151
152impl std::error::Error for Error {}
153
154impl IntoResponse for Error {
155 fn into_response(self) -> Response {
156 (
157 self.status,
158 Json(serde_json::json!({ "error": self.message })),
159 )
160 .into_response()
161 }
162}
163
164pub mod identity;
165
166pub mod workflow;