attackerkb_api_rs/
pagination.rs

1//! Paging management for unified API response
2use crate::error::ErrorResponse;
3use crate::v1::assessment::Assessment;
4use crate::v1::contributor::Contributor;
5use crate::v1::topic::Topic;
6
7use serde::{Deserialize, Serialize};
8
9pub(crate) fn default_size() -> i32 {
10  10
11}
12
13/// The API returns seven primary objects in the body of the response: [KBResponse]
14#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
15#[serde(untagged)]
16#[allow(clippy::large_enum_variant)]
17pub enum KBResponse {
18  Topics(ListResponse<Topic>),
19  Topic(SingleResponse<Topic>),
20  Assessments(ListResponse<Assessment>),
21  Assessment(SingleResponse<Assessment>),
22  Contributors(ListResponse<Contributor>),
23  Contributor(SingleResponse<Contributor>),
24  Error(ErrorResponse),
25}
26/// There are multiple pages of data
27#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
28pub struct ListResponse<T> {
29  /// List
30  pub data: Vec<T>,
31  /// Pagination
32  pub links: Option<Links>,
33}
34/// Single instance object
35#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
36pub struct SingleResponse<T> {
37  /// Single
38  pub data: T,
39}
40
41/// Pagination links objects
42#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
43#[serde(rename_all = "camelCase")]
44pub struct Links {
45  /// resultsPerPage
46  pub next: Option<Link>,
47  /// startIndex
48  pub prev: Option<Link>,
49  /// totalResults
50  #[serde(rename = "self")]
51  pub self_field: Link,
52}
53
54/// Pagination link object
55#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
56#[serde(rename_all = "camelCase")]
57pub struct Link {
58  /// URL for paginated resource
59  href: String,
60}