melvin_ob/http_handler/http_request/
daily_map_post.rs

1use super::daily_map::DailyMapResponse;
2use super::request_common::{
3    HTTPRequestMethod, HTTPRequestType, MultipartBodyHTTPRequestType,
4};
5use std::{io, path::Path};
6use std::path::PathBuf;
7
8/// Request type for the /dailyMap endpoint.
9#[derive(Debug)]
10pub(crate) struct DailyMapRequest {
11    /// File path to the current daily map png image file.
12    image_path: PathBuf,
13}
14
15impl MultipartBodyHTTPRequestType for DailyMapRequest {
16    /// returns the path for the multipart image file.
17    fn image_path(&self) -> &PathBuf { &self.image_path }
18}
19
20impl HTTPRequestType for DailyMapRequest {
21    /// Type of the expected response.
22    type Response = DailyMapResponse;
23    /// `str` object representing the specific endpoint.
24    fn endpoint(&self) -> &'static str { "/dailyMap" }
25    /// The corresponding HTTP Request Method.
26    fn request_method(&self) -> HTTPRequestMethod { HTTPRequestMethod::Post }
27}
28
29impl DailyMapRequest {
30    /// Constructs a new `DailyMapRequest` from an image path to the full snapshot.
31    pub(crate) fn new<P: AsRef<Path>>(image_path: P) -> Result<Self, io::Error> {
32        let path = image_path.as_ref();
33        if !path.exists() {
34            return Err(io::Error::new(
35                io::ErrorKind::NotFound,
36                "File path does not exist",
37            ));
38        }
39        if !path.is_file() {
40            return Err(io::Error::new(
41                io::ErrorKind::InvalidInput,
42                "Path is not a valid file",
43            ));
44        }
45        Ok(Self { image_path: path.to_path_buf() })
46    }
47}