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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
use crate::{
    error::{Error, Result},
    resources::entity_def::Name as EntityName,
    resources::label_def::Name as LabelName,
    resources::label_group::Name as LabelGroupName,
    resources::label_group::DEFAULT_LABEL_GROUP_NAME,
    SourceId,
};
use chrono::{DateTime, Utc};
use ordered_float::NotNan;
use serde::{
    de::{Deserializer, Error as SerdeError, MapAccess, Visitor},
    ser::{SerializeMap, Serializer},
    Deserialize, Serialize,
};
use serde_json::Value as JsonValue;
use std::{
    collections::HashMap,
    fmt::{Formatter, Result as FmtResult},
    ops::{Deref, DerefMut},
    path::PathBuf,
    result::Result as StdResult,
    str::FromStr,
};

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Id(pub String);

impl FromStr for Id {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self> {
        Ok(Self(string.to_owned()))
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct Uid(pub String);

impl FromStr for Uid {
    type Err = Error;

    fn from_str(string: &str) -> Result<Self> {
        Ok(Self(string.to_owned()))
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct ThreadId(pub String);

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CommentTimestampFilter {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<DateTime<Utc>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewedFilterEnum {
    OnlyReviewed,
    OnlyUnreviewed,
}

type UserPropertyName = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserPropertiesFilter(pub HashMap<UserPropertyName, PropertyFilter>);

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertyFilter {
    #[serde(skip_serializing_if = "<[_]>::is_empty", default)]
    pub one_of: Vec<PropertyValue>,
    #[serde(skip_serializing_if = "<[_]>::is_empty", default)]
    pub not_one_of: Vec<PropertyValue>,
    #[serde(skip_serializing_if = "<[_]>::is_empty", default)]
    pub domain_not_one_of: Vec<PropertyValue>,
}

impl PropertyFilter {
    pub fn new(
        one_of: Vec<PropertyValue>,
        not_one_of: Vec<PropertyValue>,
        domain_not_one_of: Vec<PropertyValue>,
    ) -> Self {
        Self {
            one_of,
            not_one_of,
            domain_not_one_of,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CommentFilter {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reviewed: Option<ReviewedFilterEnum>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub timestamp: Option<CommentTimestampFilter>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_properties: Option<UserPropertiesFilter>,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    #[serde(default)]
    pub sources: Vec<SourceId>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub messages: Option<MessagesFilter>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MessagesFilter {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<PropertyFilter>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<PropertyFilter>,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct GetRecentRequest<'a> {
    pub limit: usize,
    pub filter: &'a CommentFilter,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub continuation: Option<&'a Continuation>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct Continuation(pub String);

#[derive(Debug, Clone, Deserialize)]
pub struct RecentCommentsPage {
    pub results: Vec<AnnotatedComment>,
    pub continuation: Option<Continuation>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, Hash)]
pub struct GetLabellingsAfter(pub String);

#[derive(Debug, Clone, Deserialize)]
pub struct GetAnnotationsResponse {
    pub results: Vec<AnnotatedComment>,
    #[serde(default)]
    pub after: Option<GetLabellingsAfter>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GetPredictionsResponse {
    pub predictions: Vec<Prediction>,
}

#[derive(Debug, Clone, Serialize)]
pub struct UpdateAnnotationsRequest<'a> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub labelling: Option<&'a [NewLabelling]>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entities: Option<&'a NewEntities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub moon_forms: Option<&'a [NewMoonForm]>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct CommentsIterPage {
    pub comments: Vec<Comment>,
    pub continuation: Option<Continuation>,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct PutCommentsRequest<'request> {
    pub comments: &'request [NewComment],
}

#[derive(Debug, Clone, Deserialize)]
pub struct PutCommentsResponse;

#[derive(Debug, Clone, Serialize)]
pub(crate) struct SyncCommentsRequest<'request> {
    pub comments: &'request [NewComment],
}

#[derive(Debug, Clone, Deserialize)]
pub struct SyncCommentsResponse {
    pub new: usize,
    pub updated: usize,
    pub unchanged: usize,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct GetCommentResponse {
    pub comment: Comment,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Comment {
    pub id: Id,
    pub uid: Uid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<ThreadId>,
    pub timestamp: DateTime<Utc>,
    pub messages: Vec<Message>,
    #[serde(skip_serializing_if = "PropertyMap::is_empty", default)]
    pub user_properties: PropertyMap,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub attachments: Vec<AttachmentMetadata>,
    pub created_at: DateTime<Utc>,

    #[serde(default)]
    pub has_annotations: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewComment {
    pub id: Id,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_id: Option<ThreadId>,
    pub timestamp: DateTime<Utc>,
    pub messages: Vec<Message>,
    #[serde(skip_serializing_if = "PropertyMap::is_empty", default)]
    pub user_properties: PropertyMap,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub attachments: Vec<AttachmentMetadata>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Message {
    pub body: MessageBody,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject: Option<MessageSubject>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<MessageSignature>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub cc: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub bcc: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub sent_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MessageBody {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translated_from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_markup: Option<JsonValue>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translated_from_markup: Option<JsonValue>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MessageSubject {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translated_from: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MessageSignature {
    pub text: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translated_from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_markup: Option<JsonValue>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub translated_from_markup: Option<JsonValue>,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
pub enum Sentiment {
    #[serde(rename = "positive")]
    Positive,

    #[serde(rename = "negative")]
    Negative,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct AttachmentMetadata {
    pub name: String,
    pub size: u64,
    pub content_type: String,
}

#[derive(Debug, Clone, PartialEq, Default, Eq)]
pub struct PropertyMap(HashMap<String, PropertyValue>);

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PropertyValue {
    String(String),
    Number(NotNan<f64>),
}

impl Deref for PropertyMap {
    type Target = HashMap<String, PropertyValue>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for PropertyMap {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl PropertyMap {
    #[inline]
    pub fn new() -> Self {
        Default::default()
    }

    #[inline]
    pub fn with_capacity(capacity: usize) -> Self {
        PropertyMap(HashMap::with_capacity(capacity))
    }

    #[inline]
    pub fn insert_number(&mut self, key: String, value: NotNan<f64>) {
        self.0.insert(key, PropertyValue::Number(value));
    }

    #[inline]
    pub fn insert_string(&mut self, key: String, value: String) {
        self.0.insert(key, PropertyValue::String(value));
    }

    // Provided despite deref, for `skip_serializing_if`.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

const STRING_PROPERTY_PREFIX: &str = "string:";
const NUMBER_PROPERTY_PREFIX: &str = "number:";

impl Serialize for PropertyMap {
    fn serialize<S: Serializer>(&self, serializer: S) -> StdResult<S::Ok, S::Error> {
        let mut state = serializer.serialize_map(Some(self.len()))?;
        if self.0.is_empty() {
            return state.end();
        }

        let mut full_name = String::with_capacity(32);
        for (key, value) in &self.0 {
            full_name.clear();
            match value {
                PropertyValue::String(value) => {
                    if !value.trim().is_empty() {
                        full_name.push_str(STRING_PROPERTY_PREFIX);
                        full_name.push_str(key);
                        state.serialize_entry(&full_name, &value)?;
                    }
                }
                PropertyValue::Number(value) => {
                    full_name.push_str(NUMBER_PROPERTY_PREFIX);
                    full_name.push_str(key);
                    state.serialize_entry(&full_name, &value)?;
                }
            }
        }
        state.end()
    }
}

impl<'de> Deserialize<'de> for PropertyMap {
    #[inline]
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
        deserializer.deserialize_any(PropertyMapVisitor)
    }
}

struct PropertyMapVisitor;
impl<'de> Visitor<'de> for PropertyMapVisitor {
    type Value = PropertyMap;

    fn expecting(&self, formatter: &mut Formatter<'_>) -> FmtResult {
        write!(formatter, "a user property map")
    }

    #[inline]
    fn visit_unit<E>(self) -> StdResult<PropertyMap, E> {
        Ok(PropertyMap::new())
    }

    fn visit_map<M>(self, mut access: M) -> StdResult<PropertyMap, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut values = PropertyMap::with_capacity(access.size_hint().unwrap_or(0));

        while let Some(mut key) = access.next_key()? {
            if strip_prefix(&mut key, STRING_PROPERTY_PREFIX) {
                values.insert(key, PropertyValue::String(access.next_value()?));
            } else if strip_prefix(&mut key, NUMBER_PROPERTY_PREFIX) {
                values.insert(key, PropertyValue::Number(access.next_value()?));
            } else {
                return Err(M::Error::custom(format!(
                    "user property full name `{key}` has invalid \
                     type prefix"
                )));
            }
        }

        Ok(values)
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct AnnotatedComment {
    pub comment: Comment,
    #[serde(skip_serializing_if = "should_skip_serializing_labelling")]
    pub labelling: Option<Vec<Labelling>>,
    #[serde(skip_serializing_if = "should_skip_serializing_entities")]
    pub entities: Option<Entities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thread_properties: Option<ThreadProperties>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub moon_forms: Option<Vec<MoonForm>>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub label_properties: Option<Vec<LabelProperty>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Prediction {
    pub uid: Uid,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec")]
    pub labels: Option<Vec<AutoThresholdLabel>>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec")]
    pub entities: Option<Vec<Entity>>,
}

pub fn get_default_labelling_group(labelling: &Option<Vec<Labelling>>) -> Option<&Labelling> {
    labelling
        .iter()
        .flatten()
        .find(|&labelling_group| labelling_group.is_default_group())
}

impl Labelling {
    pub fn is_default_group(&self) -> bool {
        self.group == *DEFAULT_LABEL_GROUP_NAME
    }
}

impl NewLabelling {
    pub fn is_default_group(&self) -> bool {
        self.group == *DEFAULT_LABEL_GROUP_NAME
    }
}

impl HasAnnotations for AnnotatedComment {
    fn has_annotations(&self) -> bool {
        let has_labels = self.labelling.iter().flatten().any(|labelling_group| {
            !labelling_group.assigned.is_empty() || !labelling_group.dismissed.is_empty()
        });
        let has_entities = self
            .entities
            .as_ref()
            .map(|entities| !entities.assigned.is_empty() || !entities.dismissed.is_empty())
            .unwrap_or(false);
        has_labels || has_entities || self.moon_forms.has_annotations()
    }
}

impl AnnotatedComment {
    pub fn without_predictions(mut self) -> Self {
        self.labelling = self.labelling.and_then(|mut labelling| {
            if labelling.iter().all(|labelling_group| {
                labelling_group.assigned.is_empty() && labelling_group.dismissed.is_empty()
            }) {
                None
            } else {
                for comment_labelling in &mut labelling {
                    comment_labelling.predicted = None;
                }
                Some(labelling)
            }
        });
        self.entities = self.entities.and_then(|mut entities| {
            if entities.assigned.is_empty() && entities.dismissed.is_empty() {
                None
            } else {
                entities.predicted = None;
                Some(entities)
            }
        });
        self
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct ThreadProperties {
    duration: Option<NotNan<f64>>,
    response_time: Option<NotNan<f64>>,
    num_messages: u64,
    thread_position: Option<u64>,
    first_sender: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum EitherLabelling {
    Labelling(Vec<NewLabelling>),
    LegacyLabelling(NewLegacyLabelling),
}

impl EitherLabelling {
    fn into_new_labellings(self) -> Vec<NewLabelling> {
        match self {
            EitherLabelling::Labelling(new_labelling_vec) => new_labelling_vec,
            EitherLabelling::LegacyLabelling(new_legacy_labelling) => {
                vec![NewLabelling {
                    group: DEFAULT_LABEL_GROUP_NAME.clone(),
                    assigned: new_legacy_labelling.assigned,
                    dismissed: new_legacy_labelling.dismissed,
                }]
            }
        }
    }
}

impl From<EitherLabelling> for Vec<NewLabelling> {
    fn from(either_labelling: EitherLabelling) -> Vec<NewLabelling> {
        either_labelling.into_new_labellings()
    }
}

impl HasAnnotations for EitherLabelling {
    fn has_annotations(&self) -> bool {
        match self {
            EitherLabelling::Labelling(new_labelling) => {
                new_labelling.iter().any(|labelling_group| {
                    labelling_group.assigned.is_some() || labelling_group.dismissed.is_some()
                })
            }
            EitherLabelling::LegacyLabelling(new_legacy_labelling) => {
                new_legacy_labelling.assigned.is_some() || new_legacy_labelling.dismissed.is_some()
            }
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewAnnotatedComment {
    pub comment: NewComment,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub labelling: Option<EitherLabelling>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entities: Option<NewEntities>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub audio_path: Option<PathBuf>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub moon_forms: Option<Vec<NewMoonForm>>,
}

impl<T> HasAnnotations for Option<T>
where
    T: HasAnnotations,
{
    fn has_annotations(&self) -> bool {
        self.as_ref().map_or(false, HasAnnotations::has_annotations)
    }
}

impl HasAnnotations for Vec<MoonForm> {
    fn has_annotations(&self) -> bool {
        self.iter().any(|form| !form.assigned.is_empty())
    }
}

impl HasAnnotations for Vec<NewMoonForm> {
    fn has_annotations(&self) -> bool {
        self.iter().any(|form| !form.assigned.is_empty())
    }
}

impl NewAnnotatedComment {
    pub fn has_annotations(&self) -> bool {
        self.labelling.has_annotations()
            || self.entities.has_annotations()
            || self.moon_forms.has_annotations()
    }
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Labelling {
    pub group: LabelGroupName,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub assigned: Vec<Label>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub dismissed: Vec<Label>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub predicted: Option<Vec<PredictedLabel>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewLabelling {
    pub group: LabelGroupName,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub assigned: Option<Vec<Label>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub dismissed: Option<Vec<Label>>,
}

/// Old, pre-label group labelling format.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewLegacyLabelling {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub assigned: Option<Vec<Label>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub dismissed: Option<Vec<Label>>,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
pub struct Label {
    pub name: LabelName,
    pub sentiment: Sentiment,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub metadata: Option<HashMap<String, JsonValue>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum PredictedLabelName {
    Parts(Vec<String>),
    String(LabelName),
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct PredictedLabel {
    pub name: PredictedLabelName,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sentiment: Option<NotNan<f64>>,
    pub probability: NotNan<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_thresholds: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct AutoThresholdLabel {
    pub name: Vec<String>,
    pub probability: NotNan<f64>,
    pub auto_thresholds: Vec<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LabelProperty {
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub value: NotNan<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub breakdown: Option<LabelPropertyBreakdown>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LabelPropertyBreakdown {
    pub label_contributions: Vec<LabelPropertyContribution>,
    pub other_group_contributions: Vec<LabelPropertyGroupContribution>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LabelPropertyContribution {
    pub name: LabelName,
    pub value: NotNan<f64>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct LabelPropertyGroupContribution {
    pub name: LabelGroupName,
    pub value: NotNan<f64>,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct Entities {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub assigned: Vec<Entity>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub dismissed: Vec<Entity>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub predicted: Option<Vec<Entity>>,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewEntities {
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub assigned: Vec<NewEntity>,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub dismissed: Vec<NewEntity>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MoonFormCapture {
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub fields: Vec<Entity>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MoonFormLabelCaptures {
    pub label: Label,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub captures: Vec<MoonFormCapture>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct MoonForm {
    pub group: LabelGroupName,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub assigned: Vec<MoonFormLabelCaptures>,
    #[serde(skip_serializing_if = "should_skip_serializing_optional_vec", default)]
    pub predicted: Option<Vec<MoonFormLabelCaptures>>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewMoonFormCapture {
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub fields: Vec<NewEntity>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewMoonFormLabelCaptures {
    pub label: Label,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub captures: Vec<NewMoonFormCapture>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct NewMoonForm {
    pub group: LabelGroupName,
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub assigned: Vec<NewMoonFormLabelCaptures>,
}

pub trait HasAnnotations {
    fn has_annotations(&self) -> bool;
}

impl HasAnnotations for NewEntities {
    fn has_annotations(&self) -> bool {
        !self.assigned.is_empty() || !self.dismissed.is_empty()
    }
}

pub fn should_skip_serializing_optional_vec<T>(maybe_vec: &Option<Vec<T>>) -> bool {
    if let Some(actual_vec) = maybe_vec {
        actual_vec.is_empty()
    } else {
        true
    }
}

fn should_skip_serializing_labelling(maybe_labelling: &Option<Vec<Labelling>>) -> bool {
    if let Some(default_labelling) = get_default_labelling_group(maybe_labelling) {
        default_labelling.assigned.is_empty()
            && default_labelling.dismissed.is_empty()
            && should_skip_serializing_optional_vec(&default_labelling.predicted)
    } else {
        true
    }
}

fn should_skip_serializing_entities(maybe_entities: &Option<Entities>) -> bool {
    if let Some(entities) = maybe_entities {
        entities.assigned.is_empty()
            && entities.dismissed.is_empty()
            && should_skip_serializing_optional_vec(&entities.predicted)
    } else {
        true
    }
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
#[serde(untagged)]
pub enum NewEntity {
    WithSpan(NewEntityWithSpan),
    WithSpans(NewEntityWithSpans),
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
pub struct NewEntityWithSpan {
    pub name: EntityName,
    pub formatted_value: String,
    pub span: NewEntitySpan,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
pub struct NewEntityWithSpans {
    pub name: EntityName,
    pub formatted_value: String,
    pub spans: Vec<NewEntitySpan>,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Eq)]
pub struct NewEntitySpan {
    content_part: String,
    message_index: usize,
    utf16_byte_start: usize,
    utf16_byte_end: usize,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Eq)]
pub struct Entity {
    pub name: EntityName,
    pub formatted_value: String,
    pub spans: Vec<EntitySpan>,
}

#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Eq)]
pub struct EntitySpan {
    content_part: String,
    message_index: usize,
    char_start: usize,
    char_end: usize,
    utf16_byte_start: usize,
    utf16_byte_end: usize,
}

#[inline]
fn strip_prefix(string: &mut String, prefix: &str) -> bool {
    if string.starts_with(prefix) {
        string.drain(..prefix.len());
        true
    } else {
        false
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::{self, json, Value as JsonValue};
    use std::collections::HashMap;

    #[test]
    fn property_map_empty_serialize() {
        assert_eq!(serde_json::to_string(&PropertyMap::new()).expect(""), "{}");
    }

    #[test]
    fn property_map_one_number_serialize() {
        let mut map = PropertyMap::new();
        map.insert_number("nps".to_owned(), NotNan::new(7.0).unwrap());
        assert_eq!(
            serde_json::to_string(&map).expect(""),
            r#"{"number:nps":7.0}"#
        );
    }

    #[test]
    fn property_map_one_string_serialize() {
        let mut map = PropertyMap::new();
        map.insert_string("age".to_owned(), "18-25".to_owned());
        assert_eq!(
            serde_json::to_string(&map).expect(""),
            r#"{"string:age":"18-25"}"#
        );
    }

    #[test]
    fn property_map_mixed_serialize() {
        let mut map = PropertyMap::new();
        map.insert_string("age".to_owned(), "18-25".to_owned());
        map.insert_string("income".to_owned(), "$6000".to_owned());
        map.insert_number("nps".to_owned(), NotNan::new(3.0).unwrap());

        let actual_map: HashMap<String, JsonValue> =
            serde_json::from_str(&serde_json::to_string(&map).expect("ser")).expect("de");

        let mut expected_map = HashMap::new();
        expected_map.insert(
            "string:age".to_owned(),
            JsonValue::String("18-25".to_owned()),
        );
        expected_map.insert(
            "string:income".to_owned(),
            JsonValue::String("$6000".to_owned()),
        );
        expected_map.insert("number:nps".to_owned(), json!(3.0));

        assert_eq!(actual_map, expected_map);
    }

    #[test]
    fn property_map_empty_deserialize() {
        let map: PropertyMap = serde_json::from_str("{}").expect("");
        assert_eq!(map, PropertyMap::new());
    }

    #[test]
    fn property_map_null_deserialize() {
        let map: PropertyMap = serde_json::from_str("null").expect("");
        assert_eq!(map, PropertyMap::new());
    }

    #[test]
    fn property_map_one_number_float_deserialize() {
        let actual: PropertyMap = serde_json::from_str(r#"{"number:nps":7.0}"#).expect("");
        let mut expected = PropertyMap::new();
        expected.insert_number("nps".to_owned(), NotNan::new(7.0).unwrap());
        assert_eq!(actual, expected);
    }

    #[test]
    fn property_map_one_number_unsigned_deserialize() {
        let actual: PropertyMap = serde_json::from_str(r#"{"number:nps":7}"#).expect("");
        let mut expected = PropertyMap::new();
        expected.insert_number("nps".to_owned(), NotNan::new(7.0).unwrap());
        assert_eq!(actual, expected);
    }

    #[test]
    fn property_map_one_number_negative_deserialize() {
        let actual: PropertyMap = serde_json::from_str(r#"{"number:nps":-7}"#).expect("");
        let mut expected = PropertyMap::new();
        expected.insert_number("nps".to_owned(), NotNan::new(-7.0).unwrap());
        assert_eq!(actual, expected);
    }

    #[test]
    fn property_map_one_string_deserialize() {
        let actual: PropertyMap = serde_json::from_str(r#"{"string:age":"18-25"}"#).expect("");
        let mut expected = PropertyMap::new();
        expected.insert_string("age".to_owned(), "18-25".to_owned());
        assert_eq!(actual, expected);
    }

    #[test]
    fn property_map_illegal_prefix_deserialize() {
        let result: StdResult<PropertyMap, _> =
            serde_json::from_str(r#"{"illegal:something":"18-25"}"#);
        assert!(result.is_err());
    }

    #[test]
    fn property_map_illegal_value_for_prefix_deserialize() {
        let result: StdResult<PropertyMap, _> =
            serde_json::from_str(r#"{"string:something":18.0}"#);
        assert!(result.is_err());

        let result: StdResult<PropertyMap, _> = serde_json::from_str(r#"{"number:something":"x"}"#);
        assert!(result.is_err());
    }

    #[test]
    fn property_map_mixed_deserialize() {
        let mut expected = PropertyMap::new();
        expected.insert_string("age".to_owned(), "18-25".to_owned());
        expected.insert_string("income".to_owned(), "$6000".to_owned());
        expected.insert_number("nps".to_owned(), NotNan::new(3.0).unwrap());

        let actual: PropertyMap = serde_json::from_str(
            r#"{"string:age":"18-25","number:nps":3,"string:income":"$6000"}"#,
        )
        .expect("");

        assert_eq!(actual, expected);
    }
}