river_data_core/models/
annotations.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use uuid::Uuid;
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
14#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
15#[serde(deny_unknown_fields)]
16pub struct AnnotationUpsert {
17 pub source_key: String,
18 pub stream_id: Uuid,
19 pub time: DateTime<Utc>,
22 pub category: String,
23 pub text: String,
24 #[serde(default, skip_serializing_if = "Option::is_none")]
27 pub standard_curve_id: Option<Uuid>,
28}
29
30#[derive(Debug, Clone, Deserialize)]
32pub struct AnnotationMapping {
33 pub source_key: String,
34 pub id: Option<Uuid>,
36 pub status: String,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
49#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
50#[serde(deny_unknown_fields)]
51pub struct NoteUpsert {
52 pub source_key: String,
53 pub site_name: String,
54 pub text: String,
55 pub verified: bool,
56}
57
58#[derive(Debug, Clone, Deserialize)]
60pub struct NoteMapping {
61 pub source_key: String,
62 pub id: Option<Uuid>,
64 pub status: String,
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
74 fn annotation_upsert_round_trips() {
75 let up = AnnotationUpsert {
76 source_key: "corrections:12".into(),
77 stream_id: Uuid::nil(),
78 time: Utc::now(),
79 category: "audit".into(),
80 text: "corrected with the January curve".into(),
81 standard_curve_id: Some(Uuid::nil()),
82 };
83 let json = serde_json::to_value(&up).unwrap();
84 let back: AnnotationUpsert = serde_json::from_value(json).unwrap();
85 assert_eq!(back.source_key, up.source_key);
86 assert_eq!(back.standard_curve_id, up.standard_curve_id);
87 }
88
89 #[test]
90 fn an_annotation_without_a_curve_round_trips() {
91 let up = AnnotationUpsert {
92 source_key: "corrections:13".into(),
93 stream_id: Uuid::nil(),
94 time: Utc::now(),
95 category: "audit".into(),
96 text: "no curve".into(),
97 standard_curve_id: None,
98 };
99 let json = serde_json::to_value(&up).unwrap();
100 assert!(json.get("standard_curve_id").is_none());
101 let back: AnnotationUpsert = serde_json::from_value(json).unwrap();
102 assert!(back.standard_curve_id.is_none());
103 }
104
105 #[test]
106 fn note_upsert_round_trips() {
107 let up = NoteUpsert {
108 source_key: "notes:4".into(),
109 site_name: "FP1".into(),
110 text: "gauge replaced".into(),
111 verified: true,
112 };
113 let back: NoteUpsert = serde_json::from_value(serde_json::to_value(&up).unwrap()).unwrap();
114 assert_eq!(back.site_name, "FP1");
115 assert!(back.verified);
116 }
117}