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 as_of(&self) -> OffsetDateTime {
119 match &self.position {
120 Position::Initial(as_of) => *as_of,
121 Position::Continuation(cursor) => cursor.as_of,
122 }
123 }
124
125 pub(super) fn project(
126 self,
127 context: &runs::Context,
128 text: &str,
129 ) -> Result<Page, super::error::Error> {
130 let revision_inputs = (
131 platform::path_text(context.repo()),
132 platform::path_text(context.state()),
133 &self.key.0,
134 text,
135 );
136 let revision = Sha256Digest::of(&serde_json::to_vec(&revision_inputs)?);
137 let offset = match &self.position {
138 Position::Initial(_) => 0,
139 Position::Continuation(cursor) => {
140 if cursor.revision != revision {
141 return Err(super::error::Error::Cursor(CursorError::Expired));
142 }
143 let offset = cursor.offset.get();
144 if offset >= text.len() || !text.is_char_boundary(offset) {
145 return Err(super::error::Error::Cursor(CursorError::Offset));
146 }
147 offset
148 }
149 };
150 let end = text.floor_char_boundary(offset.saturating_add(PAGE_BYTES));
151 Ok(Page {
152 encoding: Encoding::Json,
153 entries: [Entry {
154 offset,
155 text: text[offset..end].to_owned(),
156 }],
157 next_cursor: (end < text.len()).then(|| Cursor {
158 revision,
159 offset: NonZeroUsize::new(end).expect("a page with more text advances the cursor"),
160 as_of: self.as_of(),
161 }),
162 })
163 }
164}
165
166#[derive(Serialize)]
167pub struct Page {
168 encoding: Encoding,
169 entries: [Entry; PAGE_ENTRIES],
170 next_cursor: Option<Cursor>,
171}
172
173#[derive(Serialize)]
174#[serde(rename_all = "lowercase")]
175enum Encoding {
176 Json,
177}
178
179#[derive(Serialize)]
180struct Entry {
181 offset: usize,
182 text: String,
183}