Skip to main content

kellnr_docs/
upload_response.rs

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