Skip to main content

wire/answer/
document_versions.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4// Copyright (c) 2026 2youg1 and the sprawling contributors
5
6//! The versions of one document, newest first
7//! (`crates/wire/spec/Answer/DocumentVersions.lean` ยง8-83).
8//!
9//! The history knows the versions a page wrote; a resident's `edit`, a
10//! command or the person's own editor writes past the city's document
11//! door and leaves no line. Those versions are named only where the city
12//! saw them - on disk now, or as the version a recorded save was made
13//! on - and the answer says no more about them than that.
14
15use kernel::{Address, B3Hash, Seq, TimeMs};
16use serde::{Deserialize, Serialize};
17
18/// The most versions one answer lists.
19pub const VERSIONS_MAX: usize = 100;
20
21/// One document's versions, newest first.
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
23#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
24pub struct VersionsAnswer {
25    pub at: Address,
26    pub versions: Vec<DocumentVersion>,
27    /// Older versions were left out at [`VERSIONS_MAX`].
28    pub more: bool,
29}
30
31/// One version, and where the city learnt of it.
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
34pub struct DocumentVersion {
35    pub version: B3Hash,
36    /// Its length, when a line or the content store says it.
37    pub bytes: Option<u64>,
38    /// The content store holds its bytes, so `Range`, `Bytes` and
39    /// `Export` can read it.
40    pub kept: bool,
41    pub source: VersionSource,
42}
43
44/// Where a version came from, as far as the city can say.
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
46#[serde(rename_all = "snake_case")]
47#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
48pub enum VersionSource {
49    /// On disk now, and written by no save the history recorded.
50    OnDisk,
51    /// Written through a page - a save, or proposal cards a person
52    /// accepted - by the `document_written` line at `seq`.
53    Saved { seq: Seq, at: TimeMs },
54    /// The version the save at `seq` was made on, which no earlier
55    /// recorded save wrote: written outside the page before it.
56    Before { seq: Seq },
57}