brep_plm/version.rs
1//! The version handshake: what this server is, and the oldest CAD app it
2//! serves.
3//!
4//! Both are semver strings, `MAJOR.MINOR.PATCH`, and both ride `/api/me`
5//! (`server_version`, `min_client_version`) so the CAD app checks them right
6//! after sign-in and refuses to connect on a mismatch, naming both
7//! (plm-cad-integration-todo §2 P1). This file is their single source: every
8//! route that reports them (`/api/me`, and `GET /cad/config` when it lands)
9//! reads these constants — through `ServerConfig::min_client_version`, which
10//! is [`MIN_CLIENT_VERSION`] unless `serve --min-client-version` raised it.
11//!
12//! # Who compares what
13//!
14//! The CAD app compares its OWN version (`BREP_app`'s `Cargo.toml` version,
15//! `CARGO_PKG_VERSION` there) against [`MIN_CLIENT_VERSION`]: an app older
16//! than it is refused. The server does not see the client's version, so the
17//! refusal is the client's; [`serves`] is the comparison, written once here
18//! so a test on either side can pin the same rule.
19//!
20//! # Bumping [`MIN_CLIENT_VERSION`]
21//!
22//! Only when this server stops honouring a contract an older app relies on —
23//! a route removed or reshaped, a field renamed, a refusal that changes
24//! meaning (the contracts are plm-cad-integration-todo §4). Adding a route or
25//! a field never bumps it. Set it to the first `BREP_app` version that speaks
26//! the new contract, in the same commit as the change that breaks the old
27//! one, and say so in that commit's validation record.
28
29/// This server's version: `BREP_plm`'s own `Cargo.toml` version.
30pub const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
31
32/// The oldest CAD app (`BREP_app` version) this server serves. `0.4.0` is the
33/// app's version when the handshake was built, before any app spoke to a PLM.
34pub const MIN_CLIENT_VERSION: &str = "0.4.0";
35
36/// What this server can do beyond the contracts every served client may
37/// assume, by name. It rides `/api/me` and `GET /cad/config` as `features`.
38///
39/// A client reads it to tell an old server from a new one where the
40/// difference is otherwise invisible: a server without `attribute-filters`
41/// ignores `attr.*` query parameters and answers UNFILTERED, which looks
42/// exactly like a filter every part matched.
43///
44/// Add a name in the commit that adds the capability. A name is never
45/// removed or renamed without bumping [`MIN_CLIENT_VERSION`] (an older app may
46/// rely on it). The names are listed in plm-cad-integration-todo §4.
47pub const FEATURES: &[&str] = &[
48 // P6: /api/workspaces and /api/workspace/*, and `newest_revision` on
49 // GET /api/parts/:id, which also takes a number.
50 "workspaces",
51 // P7: attr.<key>=, attr.<key>.min / .max and include=attributes on
52 // GET /api/parts.
53 "attribute-filters",
54 // P8: external_ref and origin on POST /api/parts (external_ref unique per
55 // part type), origin on POST /api/parts/:id/revisions, and
56 // ?external_ref= / ?part_type= with external_ref and latest_* on rows.
57 "external-ref",
58 // S5 derived: PUT /api/store/doc/*key sets the revision's uses list from
59 // the document in the same write (the CAD app's PUT …/uses body), and its
60 // answer says `uses_refused` when it could not; a client seeing this name
61 // sends no PUT …/uses after a document. The route stays for older clients.
62 "derived-uses",
63];
64
65/// `MAJOR.MINOR.PATCH` as numbers. A pre-release or build suffix
66/// (`-rc.1`, `+abc`) is ignored — the handshake compares releases.
67pub fn parse(version: &str) -> Option<(u64, u64, u64)> {
68 let core = version.trim().split(['-', '+']).next()?;
69 let mut fields = core.split('.').map(|f| f.parse::<u64>().ok());
70 let triple = (fields.next()??, fields.next()??, fields.next()??);
71 fields.next().is_none().then_some(triple)
72}
73
74/// Whether a client at `client_version` is served by a server whose oldest
75/// served client is `min_client_version`. A version that does not parse is
76/// not served: the app would otherwise connect on a guess.
77pub fn serves(min_client_version: &str, client_version: &str) -> bool {
78 match (parse(min_client_version), parse(client_version)) {
79 (Some(min), Some(client)) => client >= min,
80 _ => false,
81 }
82}
83
84/// Check a `--min-client-version` floor: a version, and not below
85/// [`MIN_CLIENT_VERSION`] — lowering it would claim to serve apps that speak
86/// a contract this server no longer honours.
87pub fn check_floor(floor: &str) -> Result<(), String> {
88 if parse(floor).is_none() {
89 return Err(format!("'{floor}' is not a MAJOR.MINOR.PATCH version"));
90 }
91 if !serves(MIN_CLIENT_VERSION, floor) {
92 return Err(format!("{floor} is below this server's oldest served client, {MIN_CLIENT_VERSION}; the floor can only be raised"));
93 }
94 Ok(())
95}
96