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
use serde::Deserialize;

const MAX_NUMBER: u64 = 6;
/// Example system under test (SUT).
/// Allows to modify the two variables, a and b,
/// if they do not exceed the `MAX_NUMBER`.
/// Maintains also the sum and product of the variables.
#[allow(missing_docs)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Deserialize)]
pub struct NumberSystem {
    pub a: u64,
    pub b: u64,
    pub sum: u64,
    pub prod: u64,
}

#[allow(missing_docs)]
impl NumberSystem {
    pub fn recalculate(&mut self) {
        self.sum = self.a + self.b;
        self.prod = self.a * self.b;
    }
    pub fn increase_a(&mut self, n: u64) -> Result<(), String> {
        if self.a + n <= MAX_NUMBER {
            self.a += n;
            self.recalculate();
            Ok(())
        } else {
            Err("FAIL".to_string())
        }
    }
    pub fn increase_b(&mut self, n: u64) -> Result<(), String> {
        if self.b + n <= MAX_NUMBER {
            self.b += n;
            self.recalculate();
            Ok(())
        } else {
            Err("FAIL".to_string())
        }
    }
}