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
//! 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 std::borrow::Cow;
use crate::api::projects::ProjectEssentials;
use crate::api::roles::RoleEssentials;
use crate::api::trackers::TrackerEssentials;
use crate::api::{Endpoint, ReturnsJsonResponse};
/// Represents the types of objects that can be customized with customized types
/// in Redmine
#[derive(Debug, 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,
}
/// Describes the format (data type) of a field
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FieldFormat {
/// true or false
Bool,
/// a calendar date
Date,
/// an uploaded file
File,
/// a floating point number
Float,
/// a whole number
Integer,
/// a list of key/value pairs
KeyValueList,
/// a hyperlink
Link,
/// a list of strings
List,
/// a long text (multi-line)
Text,
/// a short text
String,
/// a Redmine user
User,
/// a Target version
Version,
}
/// Possible values contain a value and a label
#[derive(Debug, 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, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct CustomField {
/// numeric id
pub id: u64,
/// display name
pub name: String,
/// 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
pub roles: 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
pub trackers: 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>>,
}
/// a type for custom field essentials with values used in other Redmine
/// objects (e.g. issues)
#[derive(Debug, PartialEq, Eq, Clone)]
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>>,
}
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, Builder)]
#[builder(setter(strip_option))]
pub struct ListCustomFields {}
impl ReturnsJsonResponse 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()
}
}
/// helper struct for outer layers with a custom_fields field holding the inner data
#[derive(Debug, 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()?;
let endpoint = ListCustomFields::builder().build()?;
redmine.json_response_body::<_, CustomFieldsWrapper<CustomField>>(&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()?;
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: CustomField = serde_json::from_value(value.clone())?;
let reserialized = serde_json::to_value(o)?;
assert_eq!(value, reserialized);
}
Ok(())
}
}