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
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Links {
  pub(crate) left: Option<String>,
  pub(crate) right: Option<String>,
}

impl Links {
  pub fn is_single(&self) -> bool {
    (self.left.is_some() && self.right.is_none()) || self.is_same_link()
  }

  fn is_same_link(&self) -> bool {
    self.left.is_some() && self.left == self.right
  }

  pub fn single(&self) -> Option<&str> {
    if self.is_single() {
      Some(self.left.as_ref().unwrap())
    } else {
      None
    }
  }
  pub fn any(&self) -> bool {
    self.left.is_some() || self.right.is_some()
  }

  pub fn left(&self) -> &Option<String> {
    &self.left
  }
  pub fn right(&self) -> &Option<String> {
    &self.right
  }
}

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

  #[test]
  fn links_equal_test() {
    let l = Links {
      left: Some("example".to_string()),
      right: Some("example".to_string()),
    };
    assert!(l.is_single());
  }
}