1use crate::{
4 ActivityKind, ArtifactEvaluationLimits, Error, ErrorKind, MetadataProperty, MetadataValue,
5 MetadataValueKind, RepresentationId, Result, ToolIdentity,
6};
7
8pub const MAX_QUERY_PAGE_SIZE: u32 = 1_000;
10pub const MAX_QUERY_CURSOR_BYTES: usize = 2_048;
12pub const MAX_DEPENDENCY_QUERY_DEPTH: u32 = 64;
14pub const MAX_DEPENDENCY_QUERY_REPRESENTATIONS: u32 = 1_000;
16pub const MAX_PROVENANCE_QUERY_DEPTH: u32 = 64;
18pub const MAX_PROVENANCE_QUERY_REPRESENTATIONS: u32 = 1_000;
20
21#[derive(Clone, Debug, Eq, Hash, PartialEq)]
23pub struct QueryCursor(String);
24
25impl QueryCursor {
26 pub fn new(token: impl Into<String>) -> Result<Self> {
33 let token = token.into();
34 if token.is_empty() || token.len() > MAX_QUERY_CURSOR_BYTES || token.contains('\0') {
35 return Err(Error::new(
36 ErrorKind::InvalidArgument,
37 format!(
38 "query cursor must contain 1-{MAX_QUERY_CURSOR_BYTES} UTF-8 bytes without NUL"
39 ),
40 ));
41 }
42 Ok(Self(token))
43 }
44
45 #[must_use]
47 pub fn as_str(&self) -> &str {
48 &self.0
49 }
50}
51
52#[derive(Clone, Debug, Eq, PartialEq)]
54pub struct QueryPageRequest {
55 limit: u32,
56 cursor: Option<QueryCursor>,
57}
58
59impl QueryPageRequest {
60 pub fn new(limit: u32, cursor: Option<QueryCursor>) -> Result<Self> {
67 if limit == 0 || limit > MAX_QUERY_PAGE_SIZE {
68 return Err(Error::new(
69 ErrorKind::InvalidArgument,
70 format!("query page size must be between 1 and {MAX_QUERY_PAGE_SIZE}"),
71 ));
72 }
73 Ok(Self { limit, cursor })
74 }
75
76 #[must_use]
78 pub const fn limit(&self) -> u32 {
79 self.limit
80 }
81
82 #[must_use]
84 pub const fn cursor(&self) -> Option<&QueryCursor> {
85 self.cursor.as_ref()
86 }
87}
88
89#[derive(Clone, Debug, Eq, PartialEq)]
91pub struct QueryPage<T> {
92 items: Vec<T>,
93 next_cursor: Option<QueryCursor>,
94 traversal_truncated: bool,
95}
96
97impl<T> QueryPage<T> {
98 #[doc(hidden)]
100 #[must_use]
101 pub fn new(items: Vec<T>, next_cursor: Option<QueryCursor>, traversal_truncated: bool) -> Self {
102 Self {
103 items,
104 next_cursor,
105 traversal_truncated,
106 }
107 }
108
109 #[must_use]
111 pub fn items(&self) -> &[T] {
112 &self.items
113 }
114
115 #[must_use]
117 pub const fn next_cursor(&self) -> Option<&QueryCursor> {
118 self.next_cursor.as_ref()
119 }
120
121 #[must_use]
123 pub const fn traversal_truncated(&self) -> bool {
124 self.traversal_truncated
125 }
126
127 #[must_use]
129 pub fn into_items(self) -> Vec<T> {
130 self.items
131 }
132}
133
134#[derive(Clone, Copy, Debug, Eq, PartialEq)]
136pub struct DependencyQueryLimits {
137 max_depth: u32,
138 max_representations: u32,
139}
140
141impl DependencyQueryLimits {
142 pub fn new(max_depth: u32, max_representations: u32) -> Result<Self> {
149 if max_depth == 0 || max_depth > MAX_DEPENDENCY_QUERY_DEPTH {
150 return Err(Error::new(
151 ErrorKind::InvalidArgument,
152 format!(
153 "dependency query depth must be between 1 and {MAX_DEPENDENCY_QUERY_DEPTH}"
154 ),
155 ));
156 }
157 if max_representations == 0 || max_representations > MAX_DEPENDENCY_QUERY_REPRESENTATIONS {
158 return Err(Error::new(
159 ErrorKind::InvalidArgument,
160 format!(
161 "dependency query representation bound must be between 1 and {MAX_DEPENDENCY_QUERY_REPRESENTATIONS}"
162 ),
163 ));
164 }
165 Ok(Self {
166 max_depth,
167 max_representations,
168 })
169 }
170
171 #[must_use]
173 pub const fn max_depth(self) -> u32 {
174 self.max_depth
175 }
176
177 #[must_use]
179 pub const fn max_representations(self) -> u32 {
180 self.max_representations
181 }
182}
183
184#[derive(Clone, Debug, Eq, PartialEq)]
186pub struct MetadataQuery {
187 property: MetadataProperty,
188 exact_value: Option<MetadataValue>,
189}
190
191impl MetadataQuery {
192 pub fn new(property: MetadataProperty, exact_value: Option<MetadataValue>) -> Result<Self> {
198 if exact_value.as_ref().is_some_and(|value| {
199 matches!(
200 value.kind(),
201 MetadataValueKind::List | MetadataValueKind::Struct
202 )
203 }) {
204 return Err(Error::new(
205 ErrorKind::InvalidArgument,
206 "metadata query exact-value predicates must be scalar",
207 ));
208 }
209 Ok(Self {
210 property,
211 exact_value,
212 })
213 }
214
215 #[must_use]
217 pub const fn property(&self) -> &MetadataProperty {
218 &self.property
219 }
220
221 #[must_use]
223 pub const fn exact_value(&self) -> Option<&MetadataValue> {
224 self.exact_value.as_ref()
225 }
226}
227
228#[derive(Clone, Debug, Eq, PartialEq)]
230#[non_exhaustive]
231pub enum ActivityOutputQuery {
232 Kind(ActivityKind),
234 Tool(ToolIdentity),
236}
237
238#[derive(Clone, Copy, Debug, Eq, PartialEq)]
240pub struct ProvenanceQueryLimits {
241 max_depth: u32,
242 max_representations: u32,
243}
244
245impl ProvenanceQueryLimits {
246 pub fn new(max_depth: u32, max_representations: u32) -> Result<Self> {
253 if max_depth == 0 || max_depth > MAX_PROVENANCE_QUERY_DEPTH {
254 return Err(Error::new(
255 ErrorKind::InvalidArgument,
256 format!(
257 "provenance query depth must be between 1 and {MAX_PROVENANCE_QUERY_DEPTH}"
258 ),
259 ));
260 }
261 if max_representations == 0 || max_representations > MAX_PROVENANCE_QUERY_REPRESENTATIONS {
262 return Err(Error::new(
263 ErrorKind::InvalidArgument,
264 format!(
265 "provenance query representation bound must be between 1 and {MAX_PROVENANCE_QUERY_REPRESENTATIONS}"
266 ),
267 ));
268 }
269 Ok(Self {
270 max_depth,
271 max_representations,
272 })
273 }
274
275 #[must_use]
277 pub const fn max_depth(self) -> u32 {
278 self.max_depth
279 }
280
281 #[must_use]
283 pub const fn max_representations(self) -> u32 {
284 self.max_representations
285 }
286}
287
288#[derive(Clone, Copy, Debug, Eq, PartialEq)]
290pub struct ProvenanceQueryMatch {
291 representation_id: RepresentationId,
292 depth: u32,
293}
294
295impl ProvenanceQueryMatch {
296 #[doc(hidden)]
298 #[must_use]
299 pub const fn new(representation_id: RepresentationId, depth: u32) -> Self {
300 Self {
301 representation_id,
302 depth,
303 }
304 }
305
306 #[must_use]
308 pub const fn representation_id(self) -> RepresentationId {
309 self.representation_id
310 }
311
312 #[must_use]
314 pub const fn depth(self) -> u32 {
315 self.depth
316 }
317}
318
319#[derive(Clone, Copy, Debug, Eq, PartialEq)]
321pub struct StaleArtifactQuery {
322 source: Option<RepresentationId>,
323 evaluation_limits: ArtifactEvaluationLimits,
324}
325
326impl StaleArtifactQuery {
327 #[must_use]
329 pub const fn new(
330 source: Option<RepresentationId>,
331 evaluation_limits: ArtifactEvaluationLimits,
332 ) -> Self {
333 Self {
334 source,
335 evaluation_limits,
336 }
337 }
338
339 #[must_use]
341 pub const fn source(self) -> Option<RepresentationId> {
342 self.source
343 }
344
345 #[must_use]
347 pub const fn evaluation_limits(self) -> ArtifactEvaluationLimits {
348 self.evaluation_limits
349 }
350}
351
352#[cfg(test)]
353mod tests {
354 use super::*;
355
356 #[test]
357 fn page_requests_and_cursors_are_bounded() {
358 assert!(QueryPageRequest::new(1, None).is_ok());
359 assert!(QueryPageRequest::new(MAX_QUERY_PAGE_SIZE, None).is_ok());
360 assert!(QueryPageRequest::new(0, None).is_err());
361 assert!(QueryPageRequest::new(MAX_QUERY_PAGE_SIZE + 1, None).is_err());
362 assert!(QueryCursor::new("").is_err());
363 assert!(QueryCursor::new("x".repeat(MAX_QUERY_CURSOR_BYTES + 1)).is_err());
364 }
365
366 #[test]
367 fn dependency_query_limits_are_explicit() {
368 assert!(DependencyQueryLimits::new(1, 1).is_ok());
369 assert!(
370 DependencyQueryLimits::new(
371 MAX_DEPENDENCY_QUERY_DEPTH,
372 MAX_DEPENDENCY_QUERY_REPRESENTATIONS,
373 )
374 .is_ok()
375 );
376 assert!(DependencyQueryLimits::new(0, 1).is_err());
377 assert!(DependencyQueryLimits::new(1, 0).is_err());
378 }
379
380 #[test]
381 fn metadata_predicates_are_scalar() {
382 let property = MetadataProperty::new(
383 crate::VocabularyId::new("test").expect("vocabulary"),
384 crate::PropertyId::new("value").expect("property"),
385 );
386 assert!(MetadataQuery::new(property.clone(), Some(MetadataValue::i64(3))).is_ok());
387 assert!(
388 MetadataQuery::new(
389 property,
390 Some(MetadataValue::list(vec![MetadataValue::i64(3)]).expect("list")),
391 )
392 .is_err()
393 );
394 }
395
396 #[test]
397 fn provenance_query_limits_are_explicit() {
398 assert!(ProvenanceQueryLimits::new(1, 1).is_ok());
399 assert!(ProvenanceQueryLimits::new(0, 1).is_err());
400 assert!(ProvenanceQueryLimits::new(1, 0).is_err());
401 }
402}