freedom_api/api/post/
sat_config.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use reqwest::Response;
use serde::Serialize;

use crate::{api::Api, error::Error};

#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SatelliteConfiguration {
    name: String,
    doppler: Option<bool>,
    notes: Option<String>,
    band_details: Vec<String>,
}

pub struct NoName;

pub struct SatelliteConfigurationBuilder<'a, C, S> {
    pub(crate) client: &'a C,
    state: S,
}

pub(crate) fn new<C>(client: &C) -> SatelliteConfigurationBuilder<'_, C, NoName> {
    SatelliteConfigurationBuilder {
        client,
        state: NoName,
    }
}

impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoName> {
    pub fn name(self, name: impl Into<String>) -> SatelliteConfigurationBuilder<'a, C, NoBand> {
        SatelliteConfigurationBuilder {
            client: self.client,
            state: NoBand { name: name.into() },
        }
    }
}

pub struct NoBand {
    name: String,
}

impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand> {
    pub fn band_urls(
        self,
        urls: impl IntoIterator<Item = String>,
    ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
        let band_details: Vec<_> = urls.into_iter().collect();

        let state = SatelliteConfiguration {
            name: self.state.name,
            doppler: None,
            notes: None,
            band_details,
        };

        SatelliteConfigurationBuilder {
            client: self.client,
            state,
        }
    }
}

impl<'a, C> SatelliteConfigurationBuilder<'a, C, NoBand>
where
    C: Api,
{
    pub fn band_ids(
        self,
        ids: impl IntoIterator<Item = i32>,
    ) -> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
        let client = self.client;
        let bands = ids.into_iter().map(|id| {
            client
                .path_to_url(format!("satellite_bands/{}", id))
                .to_string()
        });

        self.band_urls(bands)
    }
}

impl<'a, C> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration> {
    pub fn doppler(mut self, doppler: bool) -> Self {
        self.state.doppler = Some(doppler);
        self
    }

    pub fn notes(mut self, notes: impl Into<String>) -> Self {
        self.state.notes = Some(notes.into());
        self
    }
}

impl<'a, C> SatelliteConfigurationBuilder<'a, C, SatelliteConfiguration>
where
    C: Api,
{
    pub async fn send(self) -> Result<Response, Error> {
        let client = self.client;

        let url = client.path_to_url("satellite_configurations");
        client.post(url, self.state).await
    }
}