redmine-api 0.11.4

API for the Redmine issue tracker
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Custom Fields Rest API Endpoint definitions
//!
//! [Redmine Documentation](https://www.redmine.org/projects/redmine/wiki/Rest_CustomFields)
//!
//! - [x] all custom fields endpoint

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

use crate::api::issues::RoleFilter;
use crate::api::projects::ProjectEssentials;
use crate::api::roles::RoleEssentials;
use crate::api::trackers::TrackerEssentials;
use crate::api::versions::VersionStatusFilter;
use crate::api::{Endpoint, NoPagination, ReturnsJsonResponse};

/// Represents the types of objects that can be customized with customized types
/// in Redmine
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CustomizedType {
    /// Redmine Issues
    Issue,
    /// Redmine Time Entries
    TimeEntry,
    /// Redmine Projects
    Project,
    /// Redmine Target Versions
    Version,
    /// Redmine Users
    User,
    /// Redmine Groups
    Group,
    /// Redmine Activities (in time tracking)
    Activity,
    /// Redmine Issue Priorities
    IssuePriority,
    /// Redmine Document Categories
    DocumentCategory,
}

/// The data type or format of a custom field.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FieldFormat {
    /// a Redmine user
    User,
    /// a Target version
    Version,
    /// a string
    String,
    /// a text block
    Text,
    /// a link
    Link,
    /// an integer
    Int,
    /// a floating point number
    Float,
    /// a date
    Date,
    /// a list of values
    List,
    /// a boolean
    Bool,
    /// an enumeration
    Enumeration,
    /// an attachment
    Attachment,
    /// a progress bar
    Progressbar,
}

/// The style of the edit tag for a custom field.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EditTagStyle {
    /// Dropdown list style.
    DropDown,
    /// Checkbox style.
    CheckBox,
    /// Radio button style.
    Radio,
}

impl serde::Serialize for EditTagStyle {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match *self {
            EditTagStyle::DropDown => serializer.serialize_str(""),
            EditTagStyle::CheckBox => serializer.serialize_str("check_box"),
            EditTagStyle::Radio => serializer.serialize_str("radio"),
        }
    }
}

impl<'de> serde::Deserialize<'de> for EditTagStyle {
    fn deserialize<D>(deserializer: D) -> Result<EditTagStyle, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        match s.as_str() {
            "" => Ok(EditTagStyle::DropDown),
            "check_box" => Ok(EditTagStyle::CheckBox),
            "radio" => Ok(EditTagStyle::Radio),
            _ => Err(serde::de::Error::unknown_variant(
                &s,
                &["", "check_box", "radio"],
            )),
        }
    }
}

/// Possible values contain a value and a label
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct PossibleValue {
    /// label for the value in a select box
    pub label: String,
    /// actual value
    pub value: String,
}

/// a type for custom fields to use as an API return type
///
/// alternatively you can use your own type limited to the fields you need
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CustomFieldDefinition {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
    /// description
    pub description: Option<String>,
    /// is the field editable
    pub editable: bool,
    /// type of Redmine object this field is customizing
    pub customized_type: CustomizedType,
    /// data type of the field
    pub field_format: FieldFormat,
    /// a regular expression to constrain possible string values
    pub regexp: Option<String>,
    /// a minimum length for the field
    pub min_length: Option<usize>,
    /// a maximum length for the field
    pub max_length: Option<usize>,
    /// is this field required when creating/updating an object of the customized type
    pub is_required: Option<bool>,
    /// can this field be used as a filter
    pub is_filter: Option<bool>,
    /// will this field be indexed for the search
    pub searchable: bool,
    /// can this field be added more than once
    pub multiple: bool,
    /// default value for the field
    pub default_value: Option<String>,
    /// visibility of the custom field
    pub visible: bool,
    /// which roles can see the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub roles: Option<Vec<RoleEssentials>>,
    /// limit possible values to an explicit list of values
    #[serde(skip_serializing_if = "Option::is_none")]
    pub possible_values: Option<Vec<PossibleValue>>,
    /// this field is useable in these trackers
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trackers: Option<Vec<TrackerEssentials>>,
    /// this field is useable in these projects (None means all projects)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub projects: Option<Vec<ProjectEssentials>>,
    /// is the custom field for all projects
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_for_all: Option<bool>,
    /// position of the custom field in the list
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub position: Option<u64>,
    /// url pattern for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_pattern: Option<String>,
    /// text formatting for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub text_formatting: Option<String>,
    /// edit tag style for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub edit_tag_style: Option<EditTagStyle>,
    /// user role for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_role: Option<RoleFilter>,
    /// version status for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version_status: Option<VersionStatusFilter>,
    /// extensions allowed for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extensions_allowed: Option<String>,
    /// full width layout for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub full_width_layout: Option<bool>,
    /// thousands delimiter for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thousands_delimiter: Option<bool>,
    /// ratio interval for the custom field
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ratio_interval: Option<f32>,
}

/// a type for custom field essentials with values used in other Redmine
/// objects (e.g. issues)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomFieldEssentialsWithValue {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
    /// if this is true the value is serialized as an array
    pub multiple: Option<bool>,
    /// value
    pub value: Option<Vec<String>>,
}

/// a type used to list all the custom field ids and names
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct CustomFieldName {
    /// numeric id
    pub id: u64,
    /// display name
    pub name: String,
}

impl serde::Serialize for CustomFieldEssentialsWithValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut len = 2;
        if self.multiple.is_some() {
            len += 1;
        };
        if self.value.is_some() {
            len += 1;
        }
        let mut state = serializer.serialize_struct("CustomFieldEssentialsWithValue", len)?;
        state.serialize_field("id", &self.id)?;
        state.serialize_field("name", &self.name)?;
        if let Some(ref multiple) = self.multiple {
            state.serialize_field("multiple", &multiple)?;
            if let Some(ref value) = self.value {
                state.serialize_field("value", &value)?;
            } else {
                let s: Option<Vec<String>> = None;
                state.serialize_field("value", &s)?;
            }
        } else if let Some(ref value) = self.value {
            match value.as_slice() {
                [] => {
                    let s: Option<String> = None;
                    state.serialize_field("value", &s)?;
                }
                [s] => {
                    state.serialize_field("value", &s)?;
                }
                values => {
                    return Err(serde::ser::Error::custom(format!(
                        "CustomFieldEssentialsWithValue multiple was set to false but value contained more than one value: {values:?}"
                    )));
                }
            }
        } else {
            let s: Option<String> = None;
            state.serialize_field("value", &s)?;
        }
        state.end()
    }
}

impl<'de> serde::Deserialize<'de> for CustomFieldEssentialsWithValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        /// the fields in the CustomFieldEssentialsWithValue type
        #[derive(serde::Deserialize)]
        #[serde(field_identifier, rename_all = "lowercase")]
        enum Field {
            /// the id field
            Id,
            /// the name field
            Name,
            /// the multiple field
            Multiple,
            /// the value field
            Value,
        }

        /// visitor to deserialize CustomFieldEssentialsWithValue
        struct CustomFieldVisitor;

        impl<'de> serde::de::Visitor<'de> for CustomFieldVisitor {
            type Value = CustomFieldEssentialsWithValue;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("struct CustomFieldEssentialsWithValue")
            }

            fn visit_map<V>(self, mut map: V) -> Result<CustomFieldEssentialsWithValue, V::Error>
            where
                V: serde::de::MapAccess<'de>,
            {
                let mut id = None;
                let mut name = None;
                let mut multiple = None;
                let mut string_value: Option<String> = None;
                let mut vec_string_value: Option<Vec<String>> = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Id => {
                            if id.is_some() {
                                return Err(serde::de::Error::duplicate_field("id"));
                            }
                            id = Some(map.next_value()?);
                        }
                        Field::Name => {
                            if name.is_some() {
                                return Err(serde::de::Error::duplicate_field("name"));
                            }
                            name = Some(map.next_value()?);
                        }
                        Field::Multiple => {
                            if multiple.is_some() {
                                return Err(serde::de::Error::duplicate_field("multiple"));
                            }
                            multiple = Some(map.next_value()?);
                        }
                        Field::Value => {
                            if string_value.is_some() {
                                return Err(serde::de::Error::duplicate_field("value"));
                            }
                            if vec_string_value.is_some() {
                                return Err(serde::de::Error::duplicate_field("value"));
                            }
                            if let Some(true) = multiple {
                                vec_string_value = Some(map.next_value()?);
                            } else {
                                string_value = map.next_value()?;
                            }
                        }
                    }
                }
                let id = id.ok_or_else(|| serde::de::Error::missing_field("id"))?;
                let name = name.ok_or_else(|| serde::de::Error::missing_field("name"))?;
                match (multiple, string_value, vec_string_value) {
                    (None, None, None) => Ok(CustomFieldEssentialsWithValue {
                        id,
                        name,
                        multiple: None,
                        value: None,
                    }),
                    (None, Some(s), None) => Ok(CustomFieldEssentialsWithValue {
                        id,
                        name,
                        multiple: None,
                        value: Some(vec![s]),
                    }),
                    (Some(true), None, Some(v)) => Ok(CustomFieldEssentialsWithValue {
                        id,
                        name,
                        multiple: Some(true),
                        value: Some(v),
                    }),
                    _ => Err(serde::de::Error::custom(
                        "invalid combination of multiple and value",
                    )),
                }
            }
        }

        /// list of fields of CustomFieldEssentialsWithValue to pass to deserialize_struct
        const FIELDS: &[&str] = &["id", "name", "multiple", "value"];
        deserializer.deserialize_struct(
            "CustomFieldEssentialsWithValue",
            FIELDS,
            CustomFieldVisitor,
        )
    }
}

/// The endpoint for all custom fields
#[derive(Debug, Clone, Builder)]
#[builder(setter(strip_option))]
pub struct ListCustomFields {}

impl ReturnsJsonResponse for ListCustomFields {}
impl NoPagination for ListCustomFields {}

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

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

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

/// a custom field
#[derive(Debug, Clone, Serialize, serde::Deserialize)]
pub struct CustomField<'a> {
    /// the custom field's id
    pub id: u64,
    /// is usually present in contexts where it is returned by Redmine but can be omitted when it is sent by the client
    pub name: Option<Cow<'a, str>>,
    /// the custom field's value
    pub value: Cow<'a, str>,
}

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

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

    #[traced_test]
    #[test]
    fn test_list_custom_fields_no_pagination() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let redmine = crate::api::Redmine::from_env(
            reqwest::blocking::Client::builder()
                .tls_backend_rustls()
                .build()?,
        )?;
        let endpoint = ListCustomFields::builder().build()?;
        redmine.json_response_body::<_, CustomFieldsWrapper<CustomFieldDefinition>>(&endpoint)?;
        Ok(())
    }

    /// this tests if any of the results contain a field we are not deserializing
    ///
    /// this will only catch fields we missed if they are part of the response but
    /// it is better than nothing
    #[traced_test]
    #[test]
    fn test_completeness_custom_fields_type() -> Result<(), Box<dyn Error>> {
        dotenvy::dotenv()?;
        let redmine = crate::api::Redmine::from_env(
            reqwest::blocking::Client::builder()
                .tls_backend_rustls()
                .build()?,
        )?;
        let endpoint = ListCustomFields::builder().build()?;
        let CustomFieldsWrapper {
            custom_fields: values,
        } = redmine.json_response_body::<_, CustomFieldsWrapper<serde_json::Value>>(&endpoint)?;
        for value in values {
            let o: CustomFieldDefinition = serde_json::from_value(value.clone())?;
            let reserialized = serde_json::to_value(o)?;
            assert_eq!(value, reserialized);
        }
        Ok(())
    }
}