artifactize_core/verify_tools/
page.rs1use crate::{platform, runs, types::Sha256Digest};
3use serde::{Serialize, Serializer};
4use std::{fmt, num::NonZeroUsize, str::FromStr};
5use time::OffsetDateTime;
6
7const PAGE_BYTES: usize = 8 * 1024;
10pub(super) const PAGE_ENTRIES: usize = 1;
12const CURSOR_FIELDS: usize = 3;
14
15#[derive(Debug)]
16pub enum CursorError {
17 Shape,
18 Revision,
19 Offset,
20 Clock,
21 Expired,
22}
23
24impl fmt::Display for CursorError {
25 fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
26 out.write_str(match self {
27 Self::Shape => "Invalid detail cursor.",
28 Self::Revision => "Invalid detail cursor revision.",
29 Self::Offset => "Invalid detail cursor offset.",
30 Self::Clock => "Invalid detail cursor clock.",
31 Self::Expired => "Expired or foreign detail cursor; restart this inspection.",
32 })
33 }
34}
35
36#[derive(Clone)]
39pub(super) struct Cursor {
40 revision: Sha256Digest,
41 offset: NonZeroUsize,
42 as_of: OffsetDateTime,
43}
44
45impl FromStr for Cursor {
46 type Err = CursorError;
47 fn from_str(value: &str) -> Result<Self, CursorError> {
48 let mut fields = value.splitn(CURSOR_FIELDS, ':');
49 let revision = fields
50 .next()
51 .ok_or(CursorError::Revision)?
52 .parse()
53 .map_err(|_| CursorError::Revision)?;
54 let offset = fields
55 .next()
56 .ok_or(CursorError::Shape)?
57 .parse()
58 .map_err(|_| CursorError::Offset)?;
59 let nanos = fields
60 .next()
61 .ok_or(CursorError::Shape)?
62 .parse()
63 .map_err(|_| CursorError::Clock)?;
64 let as_of =
65 OffsetDateTime::from_unix_timestamp_nanos(nanos).map_err(|_| CursorError::Clock)?;
66 Ok(Self {
67 revision,
68 offset,
69 as_of,
70 })
71 }
72}
73
74impl fmt::Display for Cursor {
75 fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
76 write!(
77 out,
78 "{}:{}:{}",
79 self.revision,
80 self.offset,
81 self.as_of.unix_timestamp_nanos()
82 )
83 }
84}
85
86impl Serialize for Cursor {
87 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
88 serializer.collect_str(self)
89 }
90}
91
92#[derive(Clone)]
94pub(super) struct QueryKey(Sha256Digest);
95
96impl QueryKey {
97 pub(super) fn new(bytes: &[u8]) -> Self {
98 Self(Sha256Digest::of(bytes))
99 }
100}
101
102#[derive(Clone)]
103enum Position {
104 Initial(OffsetDateTime),
105 Continuation(Cursor),
106}
107
108#[derive(Clone)]
109pub(super) struct Query {
110 key: QueryKey,
111 position: Position,
112}
113
114impl Query {
115 pub(super) fn new(key: QueryKey, cursor: Option<Cursor>, as_of: OffsetDateTime) -> Self {
116 Self {
117 key,
118 position: cursor.map_or(Position::Initial(as_of), Position::Continuation),
119 }
120 }
121
122 pub(super) fn is_initial(&self) -> bool {
123 matches!(self.position, Position::Initial(_))
124 }
125
126 pub(super) fn refreshed(mut self) -> Self {
127 if let Position::Initial(as_of) = &mut self.position {
128 *as_of = OffsetDateTime::now_utc();
129 }
130 self
131 }
132
133 pub(super) fn as_of(&self) -> OffsetDateTime {
134 match &self.position {
135 Position::Initial(as_of) => *as_of,
136 Position::Continuation(cursor) => cursor.as_of,
137 }
138 }
139
140 pub(super) fn project(
141 self,
142 context: &runs::Context,
143 text: &str,
144 ) -> Result<Page, super::error::Error> {
145 let revision_inputs = (
146 platform::path_text(context.repo()),
147 platform::path_text(context.state()),
148 &self.key.0,
149 text,
150 );
151 let revision = Sha256Digest::of(&serde_json::to_vec(&revision_inputs)?);
152 let offset = match &self.position {
153 Position::Initial(_) => 0,
154 Position::Continuation(cursor) => {
155 if cursor.revision != revision {
156 return Err(super::error::Error::Cursor(CursorError::Expired));
157 }
158 let offset = cursor.offset.get();
159 if offset >= text.len() || !text.is_char_boundary(offset) {
160 return Err(super::error::Error::Cursor(CursorError::Offset));
161 }
162 offset
163 }
164 };
165 let end = text.floor_char_boundary(offset.saturating_add(PAGE_BYTES));
166 Ok(Page {
167 encoding: Encoding::Json,
168 entries: [Entry {
169 offset,
170 text: text[offset..end].to_owned(),
171 }],
172 next_cursor: (end < text.len()).then(|| Cursor {
173 revision,
174 offset: NonZeroUsize::new(end).expect("a page with more text advances the cursor"),
175 as_of: self.as_of(),
176 }),
177 })
178 }
179}
180
181#[derive(Serialize)]
182pub struct Page {
183 encoding: Encoding,
184 entries: [Entry; PAGE_ENTRIES],
185 next_cursor: Option<Cursor>,
186}
187
188#[derive(Serialize)]
189#[serde(rename_all = "lowercase")]
190enum Encoding {
191 Json,
192}
193
194#[derive(Serialize)]
195struct Entry {
196 offset: usize,
197 text: String,
198}