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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! Enumerations Rest API Endpoint definitions
//!
//! [Redmine Documentation](https://www.redmine.org/projects/redmine/wiki/Rest_Enumerations)
//!
//! - [x] all issue priorities endpoint
//! - [x] all time entry activities endpoint
//! - [x] all document categories endpoint

use derive_builder::Builder;
use reqwest::Method;
use std::borrow::Cow;

use crate::api::{Endpoint, ReturnsJsonResponse};

/// a minimal type for Redmine issue priorities included in
/// other Redmine objects
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IssuePriorityEssentials {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
}

impl From<IssuePriority> for IssuePriorityEssentials {
    fn from(v: IssuePriority) -> Self {
        IssuePriorityEssentials {
            id: v.id,
            name: v.name,
        }
    }
}

impl From<&IssuePriority> for IssuePriorityEssentials {
    fn from(v: &IssuePriority) -> Self {
        IssuePriorityEssentials {
            id: v.id,
            name: v.name.to_owned(),
        }
    }
}

/// a type for issue priority to use as an API return type
///
/// alternatively you can use your own type limited to the fields you need
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IssuePriority {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
    /// whether this value is the default value
    pub is_default: bool,
}

/// The endpoint for all issue priorities
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct ListIssuePriorities {}

impl ReturnsJsonResponse for ListIssuePriorities {}

impl ListIssuePriorities {
    /// Create a builder for the endpoint.
    #[must_use]
    pub fn builder() -> ListIssuePrioritiesBuilder {
        ListIssuePrioritiesBuilder::default()
    }
}

impl Endpoint for ListIssuePriorities {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "enumerations/issue_priorities.json".into()
    }
}

/// helper struct for outer layers with a issue_priorities field holding the inner data
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct IssuePrioritiesWrapper<T> {
    /// to parse JSON with issue_priorities key
    pub issue_priorities: Vec<T>,
}

/// a minimal type for Redmine time entry activities included in
/// other Redmine objects
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TimeEntryActivityEssentials {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
}

impl From<TimeEntryActivity> for TimeEntryActivityEssentials {
    fn from(v: TimeEntryActivity) -> Self {
        TimeEntryActivityEssentials {
            id: v.id,
            name: v.name,
        }
    }
}

impl From<&TimeEntryActivity> for TimeEntryActivityEssentials {
    fn from(v: &TimeEntryActivity) -> Self {
        TimeEntryActivityEssentials {
            id: v.id,
            name: v.name.to_owned(),
        }
    }
}

/// a type for time entry activity to use as an API return type
///
/// alternatively you can use your own type limited to the fields you need
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TimeEntryActivity {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
    /// whether this value is the default value
    pub is_default: bool,
}

/// The endpoint for all time entry activities
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct ListTimeEntryActivities {}

impl ReturnsJsonResponse for ListTimeEntryActivities {}

impl ListTimeEntryActivities {
    /// Create a builder for the endpoint.
    #[must_use]
    pub fn builder() -> ListTimeEntryActivitiesBuilder {
        ListTimeEntryActivitiesBuilder::default()
    }
}

impl Endpoint for ListTimeEntryActivities {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "enumerations/time_entry_activities.json".into()
    }
}

/// helper struct for outer layers with a time_entry_activities field holding the inner data
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct TimeEntryActivitiesWrapper<T> {
    /// to parse JSON with time_entry_activities key
    pub time_entry_activities: Vec<T>,
}

/// a minimal type for Redmine document categories included in
/// other Redmine objects
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DocumentCategoryEssentials {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
}

impl From<DocumentCategory> for DocumentCategoryEssentials {
    fn from(v: DocumentCategory) -> Self {
        DocumentCategoryEssentials {
            id: v.id,
            name: v.name,
        }
    }
}

impl From<&DocumentCategory> for DocumentCategoryEssentials {
    fn from(v: &DocumentCategory) -> Self {
        DocumentCategoryEssentials {
            id: v.id,
            name: v.name.to_owned(),
        }
    }
}

/// a type for document category to use as an API return type
///
/// alternatively you can use your own type limited to the fields you need
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DocumentCategory {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
    /// whether this value is the default value
    pub is_default: bool,
}

/// The endpoint for all document categories
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct ListDocumentCategories {}

impl ReturnsJsonResponse for ListDocumentCategories {}

impl ListDocumentCategories {
    /// Create a builder for the endpoint.
    #[must_use]
    pub fn builder() -> ListDocumentCategoriesBuilder {
        ListDocumentCategoriesBuilder::default()
    }
}

impl Endpoint for ListDocumentCategories {
    fn method(&self) -> Method {
        Method::GET
    }

    fn endpoint(&self) -> Cow<'static, str> {
        "enumerations/document_categories.json".into()
    }
}

/// helper struct for outer layers with a document_categories field holding the inner data
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct DocumentCategoriesWrapper<T> {
    /// to parse JSON with document_categories key
    pub document_categories: Vec<T>,
}

#[cfg(test)]
mod test {
    use super::*;
    use std::error::Error;
    use tracing_test::traced_test;

    #[traced_test]
    #[test]
    fn test_list_issue_priorities_no_pagination() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let redmine = crate::api::Redmine::from_env()?;
        let endpoint = ListIssuePriorities::builder().build()?;
        redmine.json_response_body::<_, IssuePrioritiesWrapper<IssuePriority>>(&endpoint)?;
        Ok(())
    }

    #[traced_test]
    #[test]
    fn test_list_time_entry_activities_no_pagination() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let redmine = crate::api::Redmine::from_env()?;
        let endpoint = ListTimeEntryActivities::builder().build()?;
        redmine
            .json_response_body::<_, TimeEntryActivitiesWrapper<TimeEntryActivity>>(&endpoint)?;
        Ok(())
    }

    #[traced_test]
    #[test]
    fn test_list_document_categories_no_pagination() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let redmine = crate::api::Redmine::from_env()?;
        let endpoint = ListDocumentCategories::builder().build()?;
        redmine.json_response_body::<_, DocumentCategoriesWrapper<DocumentCategory>>(&endpoint)?;
        Ok(())
    }
}