openapi_merge/input/
dispute.rs

1use super::*;
2
3#[derive(Debug, Deserialize)]
4#[serde(rename_all = "camelCase")]
5pub struct Dispute {
6    pub always_apply: Option<bool>,
7    #[serde(flatten)]
8    pub dispute: PrefixOrSuffix,
9}
10
11#[derive(Debug, PartialEq, Deserialize)]
12#[serde(untagged)]
13pub enum PrefixOrSuffix {
14    Prefix { prefix: String },
15    Suffix { suffix: String },
16}
17
18impl Dispute {
19    pub fn need_prefix(&self) -> Option<&str> {
20        self.always_apply
21            .unwrap_or_default()
22            .then(|| self.dispute.get_prefix())
23            .flatten()
24    }
25
26    pub fn need_suffix(&self) -> Option<&str> {
27        self.always_apply
28            .unwrap_or_default()
29            .then(|| self.dispute.get_suffix())
30            .flatten()
31    }
32}
33
34impl PrefixOrSuffix {
35    fn get_prefix(&self) -> Option<&str> {
36        match self {
37            Self::Prefix { prefix } => Some(prefix.as_str()),
38            Self::Suffix { .. } => None,
39        }
40    }
41
42    fn get_suffix(&self) -> Option<&str> {
43        match self {
44            Self::Prefix { .. } => None,
45            Self::Suffix { suffix } => Some(suffix.as_str()),
46        }
47    }
48
49    #[cfg(test)]
50    fn prefix(prefix: impl ToString) -> Self {
51        let prefix = prefix.to_string();
52        Self::Prefix { prefix }
53    }
54
55    #[cfg(test)]
56    fn suffix(suffix: impl ToString) -> Self {
57        let suffix = suffix.to_string();
58        Self::Suffix { suffix }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    use pretty_assertions::assert_eq;
67
68    fn dispute(text: &str) -> json::Result<Dispute> {
69        json::from_str(text)
70    }
71
72    #[test]
73    fn prefix() {
74        let dispute = dispute(r#"{"prefix":"SomePrefix"}"#).unwrap();
75        assert!(dispute.always_apply.is_none());
76        assert_eq!(dispute.dispute, PrefixOrSuffix::prefix("SomePrefix"));
77    }
78
79    #[test]
80    fn suffix() {
81        let dispute = dispute(r#"{"suffix":"SomeSuffix"}"#).unwrap();
82        assert!(dispute.always_apply.is_none());
83        assert_eq!(dispute.dispute, PrefixOrSuffix::suffix("SomeSuffix"));
84    }
85}