kellnr_docs/
doc_queue_response.rs

1use kellnr_db::DocQueueEntry;
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
5pub struct DocQueueResponse {
6    pub(crate) queue: Vec<DocQueueEntryResponse>,
7}
8
9#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
10pub struct DocQueueEntryResponse {
11    pub(crate) name: String,
12    pub(crate) version: String,
13}
14
15impl From<Vec<DocQueueEntry>> for DocQueueResponse {
16    fn from(entries: Vec<DocQueueEntry>) -> Self {
17        Self {
18            queue: entries
19                .into_iter()
20                .map(|e| DocQueueEntryResponse {
21                    name: e.normalized_name.to_string(),
22                    version: e.version,
23                })
24                .collect(),
25        }
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use std::path::PathBuf;
32
33    use kellnr_common::normalized_name::NormalizedName;
34
35    use super::*;
36
37    #[test]
38    fn doc_queue_response_from_doc_queue_entry() {
39        let doc_queue = vec![
40            DocQueueEntry {
41                id: 0,
42                normalized_name: NormalizedName::from_unchecked("crate1".to_string()),
43                version: "0.0.1".to_string(),
44                path: PathBuf::default(),
45            },
46            DocQueueEntry {
47                id: 1,
48                normalized_name: NormalizedName::from_unchecked("crate2".to_string()),
49                version: "0.0.2".to_string(),
50                path: PathBuf::default(),
51            },
52        ];
53
54        let doc_queue_response = DocQueueResponse::from(doc_queue);
55
56        assert_eq!(
57            DocQueueResponse {
58                queue: vec![
59                    DocQueueEntryResponse {
60                        name: "crate1".to_string(),
61                        version: "0.0.1".to_string()
62                    },
63                    DocQueueEntryResponse {
64                        name: "crate2".to_string(),
65                        version: "0.0.2".to_string()
66                    }
67                ]
68            },
69            doc_queue_response
70        );
71    }
72}