Skip to main content

sparrow/core/
sparrow.rs

1// Copyright [2020] [Donatien Criaud]
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::egg::Egg;
16use super::errors;
17use super::nest::Nest;
18
19pub struct Sparrow {
20  nest: Nest,
21}
22
23impl Sparrow {
24  pub fn new() -> Sparrow {
25    Sparrow { nest: Nest::new() }
26  }
27}
28
29impl Sparrow {
30  pub fn insert(&mut self, key: &str, value: &str) -> Option<Egg> {
31    self.nest.insert(Egg::new(key, value))
32  }
33  pub fn get(&self, key: &str) -> Result<&Egg, errors::EggNotInNestError> {
34    self.nest.get(key)
35  }
36  pub fn pop(&mut self, key: &str) -> Result<Egg, errors::EggNotInNestError> {
37    self.nest.pop(key)
38  }
39}
40
41#[cfg(test)]
42mod tests {
43  use super::*;
44  use rstest::*;
45
46  const TEST_EGG_KEY: &str = "test";
47  const TEST_EGG_VALUE: &str = "This is a test value!";
48
49  #[test]
50  fn test_sparrow_new() {
51    Sparrow::new();
52  }
53
54  #[fixture]
55  fn sparrow() -> Sparrow {
56    Sparrow::new()
57  }
58
59  #[fixture]
60  fn egg() -> Egg {
61    Egg::new(TEST_EGG_KEY, TEST_EGG_VALUE)
62  }
63
64  #[rstest]
65  fn test_sparrow_insert(mut sparrow: Sparrow, egg: Egg) {
66    // Egg is inserted into sparrow's nest and its key wasn't found
67    assert_eq!(sparrow.insert(egg.key(), egg.value()), None);
68    // Egg is inserted into sparrow's nest and the egg previously associated to its key is returned
69    assert_eq!(sparrow.insert(egg.key(), egg.value()), Some(egg));
70  }
71
72  #[rstest]
73  fn test_sparrow_get(mut sparrow: Sparrow, egg: Egg) {
74    // Egg is not in sparrow's nest
75    assert_eq!(
76      sparrow.get(egg.key()),
77      Err(errors::EggNotInNestError::new(egg.key()))
78    );
79    // Egg is inserted into sparrow's nest and its key wasn't found
80    assert_eq!(sparrow.insert(egg.key(), egg.value()), None);
81    // Egg is in sparrow's nest and its value is returned
82    assert_eq!(sparrow.get(egg.key()), Ok(&egg))
83  }
84
85  #[rstest]
86  fn test_sparrow_pop(mut sparrow: Sparrow, egg: Egg) {
87    // Egg is inserted into sparrow's nest and its key wasn't found
88    assert_eq!(sparrow.insert(egg.key(), egg.value()), None);
89    // Egg is popped from sparrow's nest and returned
90    assert_eq!(sparrow.pop(egg.key()), Ok(egg.clone()));
91    // Egg is not in sparrow's nest
92    assert_eq!(
93      sparrow.pop(egg.key()),
94      Err(errors::EggNotInNestError::new(egg.key()))
95    );
96  }
97}