xidl_parser/versioning/mod.rs
1//! API versioning: one `@version` enum, `@added`/`@removed` lifecycle annotations
2//! and per-version HIR projection.
3//!
4//! Phase 1 is deliberately small. A specification is versioned by marking one
5//! enum with `@version`; its members, in declaration order, are the version
6//! history. `@added(Versions::v1)` and `@removed(Versions::v2)` mark when a
7//! declaration exists; annotation order never matters. Renames are expressed by
8//! declaring a new member with a disjoint interval plus `@rename`, exactly like
9//! the desugaring `versioned idl -> multi idl` describes.
10//!
11//! Projection is a pure HIR rewrite: each version is wrapped in
12//! `module <version-ident> { ... }`, lifecycle annotations and the version enum
13//! are removed, and the document version pragma carries the version value. The
14//! merged form keeps one module per selected version so generators can stay
15//! version-free.
16
17mod claims;
18mod error;
19mod lifecycle;
20pub mod map;
21mod paths;
22mod project;
23mod timeline;
24
25#[cfg(test)]
26mod tests;
27
28pub use error::VersioningError;
29pub(crate) use timeline::VersionSelection;
30pub use timeline::{VersionDef, VersionId, VersionTimeline};
31
32use lifecycle::VersionVisitor as _;
33
34use crate::error::{ParseError, ParserResult};
35use crate::hir::{ParserProperties, Specification};
36use crate::rest_hir::ProjectedHir;
37
38/// Generation property that selects one API version by member ident or value.
39pub const API_VERSION_PROPERTY: &str = "api_version";
40/// Generation property that selects several API versions (comma separated, or `*`).
41pub const API_VERSIONS_PROPERTY: &str = "api_versions";
42/// Generation property that selects the version output layout.
43pub const VERSION_LAYOUT_PROPERTY: &str = "version_layout";
44/// Generation property that lists the API version modules of a projection.
45///
46/// The HIR stage sets it for every versioned contract; targets that cannot
47/// render module-scoped output yet use it to fail loudly instead of emitting
48/// code that does not compile.
49pub const API_VERSION_MODULES_PROPERTY: &str = "api_version_modules";
50/// Layout value: one specification holding one module per version.
51pub const LAYOUT_MERGED: &str = "merged";
52/// Layout value: one projection per version.
53pub const LAYOUT_PER_VERSION: &str = "per_version";
54/// What kind of declaration a [`MemberInterval`] describes.
55#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
56#[serde(rename_all = "lowercase")]
57pub enum IntervalKind {
58 /// A struct, exception, union, enum or typedef declaration.
59 Type,
60 /// A member of a struct, exception or union.
61 Member,
62 /// A member of the version enum's own timeline is not reported; this
63 /// covers enumerators of ordinary enums.
64 Enumerator,
65 /// A value of a bitmask.
66 BitmaskValue,
67 /// An interface operation.
68 Operation,
69 /// One parameter of an operation.
70 Parameter,
71 /// An interface attribute (expanded into operations for HTTP).
72 Attribute,
73}
74
75/// The existence interval of one declaration, as half-open timeline indices.
76#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
77pub struct MemberInterval {
78 /// Declaration path from the specification root, `::`-joined, without
79 /// any version module prefix.
80 pub path: String,
81 /// What the declaration is.
82 pub kind: IntervalKind,
83 /// First timeline index where the declaration exists (inclusive).
84 pub from: usize,
85 /// First timeline index where it no longer exists (exclusive).
86 pub until: usize,
87}
88
89/// One HIR projection produced from a specification.
90#[derive(Debug)]
91pub struct VersionedProjection {
92 /// API versions this projection contains; empty for unversioned contracts.
93 pub versions: Vec<VersionDef>,
94 /// Target version of a single-version projection; `None` for merged and
95 /// unversioned projections.
96 pub version: Option<VersionDef>,
97 /// The projected HIR.
98 pub hir: ProjectedHir,
99}
100
101/// Result of analysing the API versioning annotations of a specification.
102#[derive(Debug, Clone)]
103pub struct VersioningRecord {
104 timeline: VersionTimeline,
105}
106
107impl VersioningRecord {
108 /// Validates every API versioning annotation of a specification.
109 ///
110 /// Validation always runs, even when no version is projected. Returns
111 /// `None` for specifications without a `@version` enum, and fails when
112 /// lifecycle annotations appear without one.
113 pub fn analyze(spec: &Specification) -> ParserResult<Option<VersioningRecord>> {
114 let discovery = lifecycle::discover(&spec.0)?;
115 let Some((ident, members)) = discovery.version_enum else {
116 if let Some((annotation, decl)) = discovery.saw_lifecycle {
117 return Err(VersioningError::MissingVersionEnum { annotation, decl }.into());
118 }
119 claims::check(spec, None)?;
120 return Ok(None);
121 };
122 let timeline = VersionTimeline::from_members(ident, &members)?;
123 let mut validator = lifecycle::LifecycleValidator {
124 timeline: &timeline,
125 };
126 validator.walk(&spec.0, "", lifecycle::Availability::full())?;
127 claims::check(spec, Some(&timeline))?;
128 Ok(Some(VersioningRecord { timeline }))
129 }
130
131 /// The version history declared by the `@version` enum.
132 pub fn timeline(&self) -> &VersionTimeline {
133 &self.timeline
134 }
135
136 /// The existence interval of every versioned declaration, read from the
137 /// unprojected specification. Version modules carry no lifecycle
138 /// annotations after projection, so this is the only view the adapter
139 /// stage can pair versions from.
140 pub fn member_intervals(&self, spec: &Specification) -> ParserResult<Vec<MemberInterval>> {
141 lifecycle::collect_intervals(&spec.0, &self.timeline)
142 }
143
144 /// Projects `version` into an ordinary specification.
145 pub fn project(&self, spec: &Specification, version: VersionId) -> ParserResult<Specification> {
146 project::project_version(spec, &self.timeline, version)
147 }
148
149 /// Projects every selected version into one specification with one module
150 /// per version, in timeline order.
151 pub fn merge(
152 &self,
153 spec: &Specification,
154 versions: &[VersionId],
155 ) -> ParserResult<Specification> {
156 project::merge_versions(spec, &self.timeline, versions)
157 }
158}
159
160/// How versioned output is laid out for the target.
161#[derive(Debug, Clone, Copy, PartialEq, Eq)]
162pub(crate) enum VersionLayout {
163 /// One specification with one module per selected version.
164 Merged,
165 /// One projection per selected version.
166 PerVersion,
167}
168
169impl VersionLayout {
170 /// Reads [`VERSION_LAYOUT_PROPERTY`] from generation properties.
171 pub(crate) fn from_properties(properties: &ParserProperties) -> ParserResult<Self> {
172 let Some(value) = properties
173 .get(VERSION_LAYOUT_PROPERTY)
174 .and_then(serde_json::Value::as_str)
175 else {
176 return Ok(Self::Merged);
177 };
178 match value {
179 LAYOUT_MERGED => Ok(Self::Merged),
180 LAYOUT_PER_VERSION => Ok(Self::PerVersion),
181 other => Err(VersioningError::UnknownVersionLayout {
182 value: other.to_string(),
183 }
184 .into()),
185 }
186 }
187}
188
189/// Reads the requested version selection from generation properties.
190///
191/// A selection property on a specification without a `@version` enum is an
192/// error, so build flags cannot silently do nothing.
193pub(crate) fn selection_from_properties(
194 properties: &ParserProperties,
195) -> ParserResult<Option<VersionSelection>> {
196 VersionSelection::from_properties(properties)
197}
198
199/// Resolves the requested versions, defaulting to every declared version.
200pub(crate) fn selected_versions(
201 selection: Option<&VersionSelection>,
202 record: &VersioningRecord,
203) -> ParserResult<Vec<VersionId>> {
204 match selection {
205 Some(selection) => selection.resolve(&record.timeline),
206 None => Ok(record.timeline.iter().map(|(id, _)| id).collect()),
207 }
208}
209
210/// Reports a selection requested for an unversioned specification.
211pub(crate) fn unversioned_selection_error(selection: &VersionSelection) -> ParseError {
212 VersioningError::UnversionedSpecification {
213 selection: selection.describe(),
214 }
215 .into()
216}