Skip to main content

made_core/ports/
agentic_system_query.rs

1use crate::value_objects::{AgenticSystemId, AgenticSystemLifecycle, AgenticSystemPageLimit};
2
3/// Which designs a listing wants, and how many of them.
4///
5/// The cursor is the last identifier the previous page returned rather
6/// than an opaque token: the catalogue is ordered by identity, the
7/// order is public, and a token that only re-encoded it would be one
8/// more thing to keep true.
9#[derive(Debug, Clone, Default, PartialEq, Eq)]
10pub struct AgenticSystemQuery {
11    lifecycle: Option<AgenticSystemLifecycle>,
12    limit: AgenticSystemPageLimit,
13    after: Option<AgenticSystemId>,
14}
15
16impl AgenticSystemQuery {
17    #[must_use]
18    pub const fn new(
19        lifecycle: Option<AgenticSystemLifecycle>,
20        limit: AgenticSystemPageLimit,
21        after: Option<AgenticSystemId>,
22    ) -> Self {
23        Self {
24            lifecycle,
25            limit,
26            after,
27        }
28    }
29
30    #[must_use]
31    pub const fn lifecycle(&self) -> Option<AgenticSystemLifecycle> {
32        self.lifecycle
33    }
34
35    #[must_use]
36    pub const fn limit(&self) -> AgenticSystemPageLimit {
37        self.limit
38    }
39
40    #[must_use]
41    pub const fn after(&self) -> Option<&AgenticSystemId> {
42        self.after.as_ref()
43    }
44
45    /// Whether this design belongs in the answer.
46    #[must_use]
47    pub fn admits(&self, id: &AgenticSystemId, lifecycle: AgenticSystemLifecycle) -> bool {
48        self.lifecycle.is_none_or(|wanted| wanted == lifecycle)
49            && self.after.as_ref().is_none_or(|after| id > after)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    fn id(raw: &str) -> AgenticSystemId {
58        AgenticSystemId::new(raw).unwrap()
59    }
60
61    #[test]
62    fn a_cursor_excludes_the_page_already_read() {
63        let query = AgenticSystemQuery::new(
64            Some(AgenticSystemLifecycle::Published),
65            AgenticSystemPageLimit::default(),
66            Some(id("b")),
67        );
68
69        assert!(!query.admits(&id("a"), AgenticSystemLifecycle::Published));
70        assert!(!query.admits(&id("b"), AgenticSystemLifecycle::Published));
71        assert!(query.admits(&id("c"), AgenticSystemLifecycle::Published));
72        assert!(!query.admits(&id("c"), AgenticSystemLifecycle::Draft));
73    }
74}