libtad_rs/service/astronomy/request/
astroposition.rs

1use crate::models::{astronomy::AstronomyObjectType, time::DateTime};
2use crate::service::{ProvidedArgument, RequiredArgument};
3use serde::Serialize;
4
5macro_rules! return_type {
6    ($self:ident) => {
7        AstroPositionRequest {
8            object: $self.object,
9            placeid: $self.placeid,
10            interval: $self.interval,
11            localtime: $self.localtime,
12            utctime: $self.utctime,
13            isotime: $self.isotime,
14            lang: $self.lang,
15            radius: $self.radius,
16            _a: Default::default(),
17            _b: Default::default(),
18            _c: Default::default(),
19        }
20    };
21}
22
23#[derive(Default, Serialize)]
24/// Astro Position API request.
25///
26/// Request is validated at compile time when supplied to the client.
27///
28/// Example:
29/// ```
30/// use libtad_rs::{
31///     ServiceClient,
32///     service::astronomy::AstroPositionRequest,
33///     models::{
34///         astronomy::AstronomyObjectType,
35///         time::DateTime,
36///     },
37/// };
38///
39/// let client = ServiceClient::new("access_key".into(), "secret_key".into());
40/// let request = AstroPositionRequest::new()
41///     .with_object(AstronomyObjectType::Sun)
42///     .with_placeid("3")
43///     .with_interval(DateTime::from("2021-08-18"));
44///
45/// let response = client.get_astro_position(&request);
46/// ```
47pub struct AstroPositionRequest<A = ProvidedArgument, B = ProvidedArgument, C = ProvidedArgument> {
48    object: Vec<AstronomyObjectType>,
49    placeid: Vec<String>,
50    interval: Vec<DateTime>,
51    localtime: Option<u8>,
52    utctime: Option<u8>,
53    isotime: Option<u8>,
54    lang: Option<String>,
55    radius: Option<i32>,
56    _a: std::marker::PhantomData<A>,
57    _b: std::marker::PhantomData<B>,
58    _c: std::marker::PhantomData<C>,
59}
60
61impl AstroPositionRequest {
62    /// Start building a new request.
63    pub fn new() -> AstroPositionRequest<RequiredArgument, RequiredArgument, RequiredArgument> {
64        Default::default()
65    }
66}
67
68impl<A, B, C> AstroPositionRequest<A, B, C> {
69    /// Set a list of objects for the request.
70    pub fn set_object(
71        mut self,
72        object: Vec<AstronomyObjectType>,
73    ) -> AstroPositionRequest<ProvidedArgument, B, C> {
74        self.object = object;
75
76        return_type!(self)
77    }
78
79    /// Add an object to the request.
80    pub fn with_object(
81        mut self,
82        object: AstronomyObjectType,
83    ) -> AstroPositionRequest<ProvidedArgument, B, C> {
84        self.object.push(object);
85
86        return_type!(self)
87    }
88
89    /// Set a list of placeids for the request.
90    pub fn set_placeid(
91        mut self,
92        placeid: Vec<impl Into<String>>,
93    ) -> AstroPositionRequest<A, ProvidedArgument, C> {
94        self.placeid = placeid.into_iter().map(Into::into).collect();
95
96        return_type!(self)
97    }
98
99    /// Add a placeid to the request.
100    pub fn with_placeid(
101        mut self,
102        placeid: impl Into<String>,
103    ) -> AstroPositionRequest<A, ProvidedArgument, C> {
104        self.placeid.push(placeid.into());
105
106        return_type!(self)
107    }
108
109    /// Set a list of intervals for the request.
110    pub fn set_interval(
111        mut self,
112        interval: Vec<DateTime>,
113    ) -> AstroPositionRequest<A, B, ProvidedArgument> {
114        self.interval = interval;
115
116        return_type!(self)
117    }
118
119    /// Add an interval to the request.
120    pub fn with_interval(
121        mut self,
122        interval: DateTime,
123    ) -> AstroPositionRequest<A, B, ProvidedArgument> {
124        self.interval.push(interval);
125
126        return_type!(self)
127    }
128
129    /// Toggle whether intervals should be considered local time or UTC time.
130    pub fn set_localtime(mut self, enable: bool) -> Self {
131        self.localtime.insert(enable.into());
132
133        self
134    }
135
136    /// Toggle whether to add time stamps in UTC to all events.
137    pub fn set_utctime(mut self, enable: bool) -> Self {
138        self.utctime.insert(enable.into());
139
140        self
141    }
142
143    /// Toggle whether to add time stamps in local time to all events.
144    pub fn set_isotime(mut self, enable: bool) -> Self {
145        self.isotime.insert(enable.into());
146
147        self
148    }
149
150    /// Set the request language for the request.
151    pub fn set_lang(mut self, lang: impl Into<String>) -> Self {
152        self.lang.insert(lang.into());
153
154        self
155    }
156
157    /// Set search radius for translating coordinates to locations.
158    pub fn set_radius(mut self, radius: i32) -> Self {
159        self.radius.insert(radius);
160
161        self
162    }
163}