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