rutmf 0.3.0

Ergonomic, v5-first Rust types and clients for the TM Forum Open API domain model (unofficial)
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
//! `TroubleTicket` and its specification — TMF621.

use serde::{Deserialize, Serialize};

use crate::core::macros::{tmf_entity, tmf_patch_body, tmf_struct};
use crate::core::{
    AnyEntity, Attachment, Channel, Characteristic, CharacteristicSpecification,
    ExternalIdentifier, Note, Ref, RelatedParty, TimePeriod, Timestamp,
};

/// Where a [`TroubleTicket`] has got to.
///
/// [`TroubleTicketStatus::Other`] preserves a value outside the v5 enumeration
/// rather than failing the parse.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub enum TroubleTicketStatus {
    /// Received and validated.
    #[serde(rename = "acknowledged")]
    Acknowledged,
    /// Refused at validation.
    #[serde(rename = "rejected")]
    Rejected,
    /// Waiting on something before work can start.
    #[serde(rename = "pending")]
    Pending,
    /// Deliberately paused.
    #[serde(rename = "held")]
    Held,
    /// Being worked on.
    #[serde(rename = "inProgress")]
    InProgress,
    /// Withdrawn before resolution.
    #[serde(rename = "cancelled")]
    Cancelled,
    /// Closed out; no further work will happen.
    #[serde(rename = "closed")]
    Closed,
    /// The reported trouble has been fixed.
    #[serde(rename = "resolved")]
    Resolved,
    /// A value outside the v5 enumeration, preserved verbatim.
    #[serde(untagged)]
    Other(String),
}

impl TroubleTicketStatus {
    /// Whether the ticket has stopped moving.
    ///
    /// An unknown status is reported as **not** terminal: a client that polls
    /// until a ticket finishes should keep polling rather than stop on a state
    /// this crate does not recognise.
    #[must_use]
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Closed | Self::Cancelled | Self::Rejected)
    }
}

tmf_struct! {
    @name = "TroubleTicket", @ref = "TroubleTicketRef";
    /// A reported problem, and the record of what is being done about it.
    ///
    /// ```
    /// use rutmf::ticket::{TroubleTicket, TroubleTicketStatus};
    ///
    /// let json = r#"{"@type":"TroubleTicket","status":"inProgress"}"#;
    /// let ticket: TroubleTicket = serde_json::from_str(json).unwrap();
    ///
    /// assert_eq!(ticket.status, Some(TroubleTicketStatus::InProgress));
    /// assert!(!ticket.status.unwrap().is_terminal());
    /// ```
    pub struct TroubleTicket {
        /// Server-assigned identifier.
        id: String,
        /// Canonical URI of this ticket.
        href: String,
        /// Short name for the ticket.
        name: String,
        /// What the trouble is.
        description: String,
        /// The kind of ticket, as the provider classifies it.
        ticket_type: String,
        /// How badly the trouble affects the customer.
        severity: String,
        /// How urgently it should be worked.
        priority: String,
        /// Where the ticket has got to.
        status: TroubleTicketStatus,
        /// When the status last changed.
        status_change_date: Timestamp,
        /// Why the status last changed.
        status_change_reason: String,
        /// Every status this ticket has passed through.
        status_change_history: Vec<StatusChange>,
        /// When the ticket was raised.
        creation_date: Timestamp,
        /// When the ticket was last changed.
        last_update: Timestamp,
        /// When the provider expects to resolve it.
        expected_resolution_date: Timestamp,
        /// When the requester asked for it to be resolved.
        requested_resolution_date: Timestamp,
        /// When it was actually resolved.
        resolution_date: Timestamp,
        /// The channel the ticket arrived through.
        channel: Ref<Channel>,
        /// Files supporting the report — logs, screenshots, photographs.
        attachment: Vec<Attachment>,
        /// Identifiers this ticket is known by in other systems.
        external_identifier: Vec<ExternalIdentifier>,
        /// Free-text notes added as the ticket is worked.
        note: Vec<Note>,
        /// What the trouble is about — a product, a service, a resource.
        related_entity: Vec<RelatedEntity>,
        /// Parties involved: who reported it, who owns it.
        related_party: Vec<RelatedParty>,
        /// Provider-defined attributes of this ticket.
        trouble_ticket_characteristic: Vec<Characteristic>,
        /// Links to other tickets — duplicates, dependencies, parents.
        trouble_ticket_relationship: Vec<TroubleTicketRelationship>,
        /// The specification this ticket was raised against.
        trouble_ticket_specification: Ref<TroubleTicketSpecification>,
        @renamed {
            /// The concrete class a `TroubleTicketRef` points at.
            "@referredType" referred_type: String,
        }
    }
}

tmf_struct! {
    @name = "TroubleTicket";
    /// Body of a `POST /troubleTicket` — the v5 `TroubleTicket_FVO`.
    pub struct TroubleTicketCreate {
        @required {
            /// What the trouble is. **Required on create.**
            description: String,
            /// How badly it affects the customer. **Required on create.**
            severity: String,
            /// The kind of ticket. **Required on create.**
            ticket_type: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// Short name for the ticket.
        name: String,
        /// How urgently it should be worked.
        priority: String,
        /// The status to open the ticket in, where the provider allows a choice.
        status: TroubleTicketStatus,
        /// When the status last changed.
        status_change_date: Timestamp,
        /// Why the status last changed.
        status_change_reason: String,
        /// When the provider expects to resolve it.
        expected_resolution_date: Timestamp,
        /// When the requester asks for it to be resolved.
        requested_resolution_date: Timestamp,
        /// When it was resolved.
        resolution_date: Timestamp,
        /// The channel the ticket arrived through.
        channel: Ref<Channel>,
        /// Files supporting the report.
        attachment: Vec<Attachment>,
        /// Identifiers this ticket is known by in other systems.
        external_identifier: Vec<ExternalIdentifier>,
        /// Free-text notes.
        note: Vec<Note>,
        /// What the trouble is about.
        related_entity: Vec<RelatedEntity>,
        /// Parties involved.
        related_party: Vec<RelatedParty>,
        /// Provider-defined attributes.
        trouble_ticket_characteristic: Vec<Characteristic>,
        /// Links to other tickets.
        trouble_ticket_relationship: Vec<TroubleTicketRelationship>,
        /// The specification to raise this ticket against.
        trouble_ticket_specification: Ref<TroubleTicketSpecification>,
    }
}

tmf_struct! {
    @name = "TroubleTicket";
    /// Body of a `PATCH /troubleTicket/{id}` — the v5 `TroubleTicket_MVO`.
    ///
    /// Unusually, this carries `id` and `href`. Every other `_MVO` in this
    /// crate drops the server-owned members; TMF621's declares them, and the
    /// model follows the schema rather than the house style.
    pub struct TroubleTicketUpdate {
        /// Identifier, which TMF621 leaves on the patch body.
        id: String,
        /// Canonical URI, likewise.
        href: String,
        /// New name.
        name: String,
        /// New description.
        description: String,
        /// New ticket type.
        ticket_type: String,
        /// New severity.
        severity: String,
        /// New priority.
        priority: String,
        /// New status.
        status: TroubleTicketStatus,
        /// Why the status changed.
        status_change_reason: String,
        /// Replacement status history.
        status_change_history: Vec<StatusChange>,
        /// New expected resolution date.
        expected_resolution_date: Timestamp,
        /// New requested resolution date.
        requested_resolution_date: Timestamp,
        /// New resolution date.
        resolution_date: Timestamp,
        /// New channel.
        channel: Ref<Channel>,
        /// Replacement attachments.
        attachment: Vec<Attachment>,
        /// Replacement external identifiers.
        external_identifier: Vec<ExternalIdentifier>,
        /// Replacement notes.
        note: Vec<Note>,
        /// Replacement related entities.
        related_entity: Vec<RelatedEntity>,
        /// Replacement related parties.
        related_party: Vec<RelatedParty>,
        /// Replacement characteristics.
        trouble_ticket_characteristic: Vec<Characteristic>,
        /// Replacement relationships.
        trouble_ticket_relationship: Vec<TroubleTicketRelationship>,
        /// New specification reference.
        trouble_ticket_specification: Ref<TroubleTicketSpecification>,
    }
}

tmf_struct! {
    @name = "TroubleTicketSpecification", @ref = "TroubleTicketSpecificationRef";
    /// The template a ticket is raised against: what a class of trouble means.
    pub struct TroubleTicketSpecification {
        /// Server-assigned identifier.
        id: String,
        /// Canonical URI of this specification.
        href: String,
        /// Name of the specification.
        name: String,
        /// Narrative description.
        description: String,
        /// Version of the specification.
        version: String,
        /// When it was created.
        creation_date: Timestamp,
        /// When it was last changed.
        last_update: Timestamp,
        /// Lifecycle status.
        lifecycle_status: String,
        /// Period during which the specification is valid.
        valid_for: TimePeriod,
        /// Parties related to the specification.
        related_party: Vec<RelatedParty>,
        /// Characteristics a ticket of this class carries.
        spec_characteristic: Vec<CharacteristicSpecification>,
        @renamed {
            /// The concrete class a `TroubleTicketSpecificationRef` points at.
            "@referredType" referred_type: String,
        }
    }
}

tmf_struct! {
    @name = "TroubleTicketSpecification";
    /// Body of a `POST /troubleTicketSpecification` — the v5 `_FVO` schema.
    pub struct TroubleTicketSpecificationCreate {
        @required {
            /// Name of the specification. **Required on create.**
            name: String,
        }
        /// Client-supplied identifier, where the server permits one.
        id: String,
        /// Canonical URI, where the client supplies one.
        href: String,
        /// Narrative description.
        description: String,
        /// Version of the specification.
        version: String,
        /// Lifecycle status.
        lifecycle_status: String,
        /// Period during which the specification is valid.
        valid_for: TimePeriod,
        /// Parties related to the specification.
        related_party: Vec<RelatedParty>,
        /// Characteristics a ticket of this class carries.
        spec_characteristic: Vec<CharacteristicSpecification>,
    }
}

tmf_struct! {
    @name = "TroubleTicketSpecification";
    /// Body of a `PATCH /troubleTicketSpecification/{id}` — the v5 `_MVO`.
    pub struct TroubleTicketSpecificationUpdate {
        /// Identifier, which TMF621 leaves on the patch body.
        id: String,
        /// Canonical URI, likewise.
        href: String,
        /// New name.
        name: String,
        /// New description.
        description: String,
        /// New version.
        version: String,
        /// New lifecycle status.
        lifecycle_status: String,
        /// New validity period.
        valid_for: TimePeriod,
        /// Replacement related parties.
        related_party: Vec<RelatedParty>,
        /// Replacement characteristics.
        spec_characteristic: Vec<CharacteristicSpecification>,
    }
}

tmf_struct! {
    @name = "TroubleTicketRelationship";
    /// A typed link from one trouble ticket to another.
    pub struct TroubleTicketRelationship {
        /// Identifier of the referenced ticket.
        id: String,
        /// URI of the referenced ticket.
        href: String,
        /// Name of the referenced ticket.
        name: String,
        /// What kind of link this is, e.g. `duplicate`, `dependent`.
        relationship_type: String,
        @renamed {
            /// The concrete class of the referenced ticket.
            "@referredType" referred_type: String,
        }
    }
}

tmf_struct! {
    @name = "RelatedEntity";
    /// Whatever the trouble is about, in a named role.
    ///
    /// Deliberately untyped in the specification: a ticket may be raised
    /// against a product, a service, a resource or something this crate has no
    /// model for, so the target is an `EntityRef` rather than a typed
    /// reference.
    pub struct RelatedEntity {
        /// The entity the ticket concerns.
        entity: Ref<AnyEntity>,
        /// The role it plays — what it is to this ticket.
        role: String,
    }
}

tmf_struct! {
    @name = "StatusChange";
    /// One step in a ticket's status history.
    pub struct StatusChange {
        /// The status moved to.
        status: TroubleTicketStatus,
        /// When the move happened.
        status_change_date: Timestamp,
        /// Why it happened.
        status_change_reason: String,
    }
}

tmf_entity!(TroubleTicket, TroubleTicketSpecification);
tmf_patch_body!(TroubleTicketUpdate, TroubleTicketSpecificationUpdate);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_unknown_status_is_preserved_and_not_terminal() {
        let json = r#"{"@type":"TroubleTicket","status":"awaitingFieldVisit"}"#;
        let ticket: TroubleTicket = serde_json::from_str(json).unwrap();

        assert_eq!(
            ticket.status,
            Some(TroubleTicketStatus::Other("awaitingFieldVisit".into()))
        );
        assert!(
            !ticket.status.as_ref().unwrap().is_terminal(),
            "a poller must not stop on a status it does not recognise"
        );
        assert_eq!(
            serde_json::to_value(&ticket).unwrap(),
            serde_json::from_str::<serde_json::Value>(json).unwrap()
        );
    }

    #[test]
    fn resolved_is_not_terminal_but_closed_is() {
        // A resolved ticket can still be reopened or disputed; a closed one is
        // the end of the road. Conflating them would stop a poller too early.
        assert!(!TroubleTicketStatus::Resolved.is_terminal());
        assert!(TroubleTicketStatus::Closed.is_terminal());
        assert!(TroubleTicketStatus::Cancelled.is_terminal());
    }

    #[test]
    fn the_patch_body_keeps_the_members_tmf621_leaves_on_it() {
        // Unlike every other `_MVO` in this crate.
        let patch = TroubleTicketUpdate::builder()
            .id("42")
            .status(TroubleTicketStatus::Resolved)
            .build();
        let json = serde_json::to_value(&patch).unwrap();
        assert_eq!(json["id"], "42");
        assert_eq!(json["status"], "resolved");
    }
}