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//! Change orders ([`eco`]): a set of revisions to release or obsolete, with
56//! its own numbering, reviewed as a whole and released as a whole — every
57//! item's gates and hooks, then all of them applied in one transaction, or
58//! none.
59//!
60//! Library import is designed but not built — where the design named a field it
61//! will need ([`model::Origin`], `external_ref`), the field is here and inert.
62//!
63//! # The document API
64//!
65//! `/api/store/*` is deliberately shaped to what the CAD app's `StoreBackend`
66//! requires of a remote backend: a metadata-only index (so a client never
67//! hydrates the corpus), a per-key read, a write, a delete, and a monotonic
68//! change sequence with a "what moved" feed. The CAD app is not touched by
69//! this crate; this is the surface it will adopt.
70
71pub mod api;
72pub mod attach;
73pub mod audit;
74pub mod auth;
75pub mod backup;
76pub mod bake_worker;
77pub mod bom;
78pub mod bom_config;
79pub mod cad;
80pub mod catalog;
81pub mod db;
82pub mod dirlock;
83pub mod eco;
84pub mod family;
85pub mod journal;
86pub mod lifecycle;
87pub mod replace;
88pub mod review;
89pub mod model;
90pub mod scripting;
91pub mod security;
92pub mod seed;
93pub mod sourcing;
94pub mod sql;
95pub mod table;
96pub mod thumbnail;
97pub mod geometry;
98pub mod tls;
99pub mod version;
100pub mod workspace;
101
102use axum::http::StatusCode;
103use axum::response::{IntoResponse, Response};
104use axum::Json;
105
106/// A refusal, with the status the API answers it with and the message a person
107/// reads. Every refusal in this server carries a reason — a bare status code
108/// tells the user nothing about what to do next.
109#[derive(Debug, Clone)]
110pub struct Error {
111 pub status: StatusCode,
112 pub message: String,
113}
114
115impl Error {
116 pub fn bad_request(message: impl Into<String>) -> Self {
117 Self { status: StatusCode::BAD_REQUEST, message: message.into() }
118 }
119
120 pub fn unauthorized(message: impl Into<String>) -> Self {
121 Self { status: StatusCode::UNAUTHORIZED, message: message.into() }
122 }
123
124 pub fn forbidden(message: impl Into<String>) -> Self {
125 Self { status: StatusCode::FORBIDDEN, message: message.into() }
126 }
127
128 pub fn not_found(what: impl std::fmt::Display) -> Self {
129 Self { status: StatusCode::NOT_FOUND, message: format!("no such {what}") }
130 }
131
132 /// A rule refused it — a lock held elsewhere, an immutable revision, an
133 /// exhausted number scheme. 409 is the status the CAD app's write-behind
134 /// lane will surface as a notice.
135 pub fn conflict(message: impl Into<String>) -> Self {
136 Self { status: StatusCode::CONFLICT, message: message.into() }
137 }
138
139 /// Too many failed sign-ins: the caller must wait.
140 pub fn too_many(message: impl Into<String>) -> Self {
141 Self { status: StatusCode::TOO_MANY_REQUESTS, message: message.into() }
142 }
143
144 pub fn internal(error: impl std::fmt::Display) -> Self {
145 Self {
146 status: StatusCode::INTERNAL_SERVER_ERROR,
147 message: error.to_string(),
148 }
149 }
150}
151
152impl std::fmt::Display for Error {
153 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154 write!(f, "{}: {}", self.status, self.message)
155 }
156}
157
158impl std::error::Error for Error {}
159
160impl IntoResponse for Error {
161 fn into_response(self) -> Response {
162 (
163 self.status,
164 Json(serde_json::json!({ "error": self.message })),
165 )
166 .into_response()
167 }
168}
169
170pub mod identity;
171
172pub mod workflow;