Skip to main content

kellnr_docs/
upload_response.rs

1use kellnr_common::original_name::OriginalName;
2use kellnr_common::version::Version;
3use serde::{Deserialize, Serialize};
4use utoipa::ToSchema;
5
6use crate::compute_doc_url;
7
8/// Response after successfully uploading documentation
9#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, ToSchema)]
10pub struct DocUploadResponse {
11    /// Status message
12    pub message: String,
13    /// URL to access the uploaded documentation
14    pub url: String,
15    /// Name of the crate
16    pub crate_name: String,
17    /// Version of the crate
18    pub crate_version: String,
19}
20
21impl DocUploadResponse {
22    pub fn new(
23        message: String,
24        crate_name: &OriginalName,
25        crate_version: &Version,
26        path_prefix: &str,
27    ) -> Self {
28        Self {
29            message,
30            crate_name: crate_name.to_string(),
31            crate_version: crate_version.to_string(),
32            url: compute_doc_url(&crate_name.to_string(), crate_version, path_prefix),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use std::convert::TryFrom;
40
41    use super::*;
42
43    #[test]
44    fn create_new_doc_upload_response_works() {
45        let name = OriginalName::try_from("mycrate").unwrap();
46        let version = Version::try_from("1.0.0-beta2").unwrap();
47        let msg = "Hello, this is the message.".to_string();
48
49        let dur = DocUploadResponse::new(msg, &name, &version, "");
50
51        assert_eq!(
52            DocUploadResponse {
53                message: "Hello, this is the message.".to_string(),
54                url: "/docs/mycrate/1.0.0-beta2/doc/mycrate/index.html".to_string(),
55                crate_name: "mycrate".to_string(),
56                crate_version: "1.0.0-beta2".to_string()
57            },
58            dur
59        );
60    }
61
62    #[test]
63    fn create_new_doc_upload_replace_hyphen_with_underscore() {
64        let name = OriginalName::try_from("my-crate").unwrap();
65        let version = Version::try_from("1.0.0").unwrap();
66        let msg = "Hello, this is the message.".to_string();
67
68        let dur = DocUploadResponse::new(msg, &name, &version, "");
69
70        assert_eq!(
71            DocUploadResponse {
72                message: "Hello, this is the message.".to_string(),
73                url: "/docs/my-crate/1.0.0/doc/my_crate/index.html".to_string(),
74                crate_name: "my-crate".to_string(),
75                crate_version: "1.0.0".to_string()
76            },
77            dur
78        );
79    }
80
81    #[test]
82    fn create_new_doc_upload_with_path_prefix() {
83        let name = OriginalName::try_from("mycrate").unwrap();
84        let version = Version::try_from("1.0.0").unwrap();
85        let msg = "Hello, this is the message.".to_string();
86
87        let dur = DocUploadResponse::new(msg, &name, &version, "/kellnr");
88
89        assert_eq!(
90            DocUploadResponse {
91                message: "Hello, this is the message.".to_string(),
92                url: "/kellnr/docs/mycrate/1.0.0/doc/mycrate/index.html".to_string(),
93                crate_name: "mycrate".to_string(),
94                crate_version: "1.0.0".to_string()
95            },
96            dur
97        );
98    }
99}