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(message: String, crate_name: &OriginalName, crate_version: &Version) -> Self {
17        Self {
18            message,
19            crate_name: crate_name.to_string(),
20            crate_version: crate_version.to_string(),
21            url: compute_doc_url(&crate_name.to_string(), crate_version),
22        }
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use std::convert::TryFrom;
29
30    use super::*;
31
32    #[test]
33    fn create_new_doc_upload_response_works() {
34        let name = OriginalName::try_from("mycrate").unwrap();
35        let version = Version::try_from("1.0.0-beta2").unwrap();
36        let msg = "Hello, this is the message.".to_string();
37
38        let dur = DocUploadResponse::new(msg, &name, &version);
39
40        assert_eq!(
41            DocUploadResponse {
42                message: "Hello, this is the message.".to_string(),
43                url: "/docs/mycrate/1.0.0-beta2/doc/mycrate/index.html".to_string(),
44                crate_name: "mycrate".to_string(),
45                crate_version: "1.0.0-beta2".to_string()
46            },
47            dur
48        );
49    }
50
51    #[test]
52    fn create_new_doc_upload_replace_hyphen_with_underscore() {
53        let name = OriginalName::try_from("my-crate").unwrap();
54        let version = Version::try_from("1.0.0").unwrap();
55        let msg = "Hello, this is the message.".to_string();
56
57        let dur = DocUploadResponse::new(msg, &name, &version);
58
59        assert_eq!(
60            DocUploadResponse {
61                message: "Hello, this is the message.".to_string(),
62                url: "/docs/my-crate/1.0.0/doc/my_crate/index.html".to_string(),
63                crate_name: "my-crate".to_string(),
64                crate_version: "1.0.0".to_string()
65            },
66            dur
67        );
68    }
69}