Skip to main content

typst_pack/
write.rs

1use crate::CanonicalIdentity;
2
3/// Knowledge about whether one attempted destination effect completed.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum CommitCertainty {
6    NotCommitted,
7    Committed,
8    Indeterminate,
9}
10
11/// The outcome observed for one successfully completed write entry.
12#[derive(Clone, Copy, Debug, Eq, PartialEq)]
13#[non_exhaustive]
14pub enum WriteKeyOutcome {
15    /// The adapter created an entry under a create-only guarantee.
16    Created,
17    /// The adapter observed that the destination already contained the exact bytes.
18    AlreadyMatching,
19    /// The adapter wrote an entry without distinguishing creation from replacement.
20    Written,
21}
22
23/// One completed entry in Pack Extraction write-plan order.
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub struct PackExtractionWriteEntry {
26    relative_path: String,
27    outcome: WriteKeyOutcome,
28}
29
30impl PackExtractionWriteEntry {
31    pub(crate) fn new(relative_path: String, outcome: WriteKeyOutcome) -> Self {
32        Self {
33            relative_path,
34            outcome,
35        }
36    }
37
38    pub fn relative_path(&self) -> &str {
39        &self.relative_path
40    }
41
42    pub const fn outcome(&self) -> WriteKeyOutcome {
43        self.outcome
44    }
45}
46
47/// Completed Pack Extraction write entries in write-plan order.
48#[derive(Clone, Debug, Default, Eq, PartialEq)]
49pub struct PackExtractionWriteProgress {
50    completed: Vec<PackExtractionWriteEntry>,
51}
52
53impl PackExtractionWriteProgress {
54    pub const fn new() -> Self {
55        Self {
56            completed: Vec::new(),
57        }
58    }
59
60    pub fn completed(&self) -> &[PackExtractionWriteEntry] {
61        &self.completed
62    }
63
64    #[cfg(feature = "fs")]
65    pub(crate) fn from_completed(completed: Vec<PackExtractionWriteEntry>) -> Self {
66        Self { completed }
67    }
68
69    #[cfg(feature = "opendal")]
70    pub(crate) fn clear(&mut self) {
71        self.completed.clear();
72    }
73
74    #[cfg(feature = "opendal")]
75    pub(crate) fn push(&mut self, entry: PackExtractionWriteEntry) {
76        self.completed.push(entry);
77    }
78}
79
80/// Evidence from successful write of one Pack Extraction Plan.
81#[derive(Clone, Debug, Eq, PartialEq)]
82pub struct PackExtractionWriteReceipt {
83    pack_identity: CanonicalIdentity,
84    progress: PackExtractionWriteProgress,
85}
86
87impl PackExtractionWriteReceipt {
88    pub(crate) const fn new(
89        pack_identity: CanonicalIdentity,
90        progress: PackExtractionWriteProgress,
91    ) -> Self {
92        Self {
93            pack_identity,
94            progress,
95        }
96    }
97
98    pub const fn pack_identity(&self) -> CanonicalIdentity {
99        self.pack_identity
100    }
101
102    pub const fn progress(&self) -> &PackExtractionWriteProgress {
103        &self.progress
104    }
105
106    pub fn completed(&self) -> &[PackExtractionWriteEntry] {
107        self.progress.completed()
108    }
109}
110
111/// One completed Compilation Output Artifact write entry.
112#[derive(Clone, Debug, Eq, PartialEq)]
113pub struct CompilationArtifactWriteEntry {
114    artifact_index: usize,
115    outcome: WriteKeyOutcome,
116}
117
118impl CompilationArtifactWriteEntry {
119    pub(crate) const fn new(artifact_index: usize, outcome: WriteKeyOutcome) -> Self {
120        Self {
121            artifact_index,
122            outcome,
123        }
124    }
125
126    pub const fn artifact_index(&self) -> usize {
127        self.artifact_index
128    }
129
130    pub const fn outcome(&self) -> WriteKeyOutcome {
131        self.outcome
132    }
133}
134
135/// Completed Compilation Output Artifact entries in canonical artifact order.
136#[derive(Clone, Debug, Default, Eq, PartialEq)]
137pub struct CompilationArtifactWriteProgress {
138    completed: Vec<CompilationArtifactWriteEntry>,
139}
140
141impl CompilationArtifactWriteProgress {
142    pub const fn new() -> Self {
143        Self {
144            completed: Vec::new(),
145        }
146    }
147
148    pub fn completed(&self) -> &[CompilationArtifactWriteEntry] {
149        &self.completed
150    }
151
152    #[cfg(feature = "fs")]
153    pub(crate) fn from_completed(completed: Vec<CompilationArtifactWriteEntry>) -> Self {
154        Self { completed }
155    }
156
157    #[cfg(feature = "opendal")]
158    pub(crate) fn clear(&mut self) {
159        self.completed.clear();
160    }
161
162    #[cfg(feature = "opendal")]
163    pub(crate) fn push(&mut self, entry: CompilationArtifactWriteEntry) {
164        self.completed.push(entry);
165    }
166}
167
168/// Evidence from successful write of one Compilation Result's artifacts.
169#[derive(Clone, Debug, Eq, PartialEq)]
170pub struct CompilationArtifactWriteReceipt {
171    compilation_result_identity: CanonicalIdentity,
172    progress: CompilationArtifactWriteProgress,
173}
174
175impl CompilationArtifactWriteReceipt {
176    pub(crate) const fn new(
177        compilation_result_identity: CanonicalIdentity,
178        progress: CompilationArtifactWriteProgress,
179    ) -> Self {
180        Self {
181            compilation_result_identity,
182            progress,
183        }
184    }
185
186    pub const fn compilation_result_identity(&self) -> CanonicalIdentity {
187        self.compilation_result_identity
188    }
189
190    pub const fn progress(&self) -> &CompilationArtifactWriteProgress {
191        &self.progress
192    }
193
194    pub fn completed(&self) -> &[CompilationArtifactWriteEntry] {
195        self.progress.completed()
196    }
197}