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
use clap::Parser;

struct SimpleTriangle {
    base: f32,
    height: f32,
}

impl SimpleTriangle {
    fn get_area(&self) -> f32 {
        (&self.base * &self.height) / 2.0
    }
}

struct SidesTriangle {
    side_a: f32,
    side_b: f32,
    side_c: f32,
}

impl SidesTriangle {
    fn get_semi_perimeter(&self) -> f32 {
        (&self.side_a + &self.side_b + &self.side_c) / 2.0
    }

    fn get_area(&self) -> f32 {
        let p: f32 = self.get_semi_perimeter();
        let sides_and_p: f32 = p * (p - &self.side_a) * (p - &self.side_b) * (p - &self.side_c);

        return sides_and_p.sqrt();
    }
}

pub fn triangle_actions(
    base: Option<f32>,
    height: Option<f32>,

    side_a: Option<f32>,
    side_b: Option<f32>,
    side_c: Option<f32>,

    area: bool,
    perimeter: bool,
) {
    if let (Some(base), Some(height)) = (base, height) {
        let triangle = SimpleTriangle { base, height };

        if area {
            println!("Area: {}cm", triangle.get_area())
        }
    } else if let (Some(side_a), Some(side_b), Some(side_c)) = (side_a, side_b, side_c) {
        let triangle = SidesTriangle {
            side_a,
            side_b,
            side_c,
        };

        if area {
            println!("Area: {}cm", triangle.get_area())
        }

        if perimeter {
            println!("Perimeter: {}cm", triangle.get_semi_perimeter())
        }
    }
}

#[derive(Parser)]
#[command(about="Mathematical operations with triangles", long_about = None)]
pub struct Command {
    #[arg(short, long, help = "Sets the Base of the triangle | e.g. -b 5")]
    pub base: Option<f32>,
    #[arg(
        short = 'e',
        long,
        help = "Sets the hEight of the triangle | e.g. -e 5"
    )]
    pub height: Option<f32>,

    #[arg(short = None, long="side-a", help = "Sets the side A of the triangle | e.g. --side-a 5")]
    pub side_a: Option<f32>,
    #[arg(short = None, long="side-b", help = "Sets the side B of the triangle | e.g. --side-b 5")]
    pub side_b: Option<f32>,
    #[arg(short = None, long="side-c", help = "Sets the side c of the triangle | e.g. --side-c 5")]
    pub side_c: Option<f32>,

    #[arg(short, long, help = "Get the Area of the triangle")]
    pub area: bool,
    #[arg(short, long, help = "Get the Perimeter of the triangle")]
    pub perimeter: bool,
}

#[cfg(test)]
mod simple_triangle {
    #[test]
    fn get_area() {
        let triangle = super::SimpleTriangle {
            base: 42.0,
            height: 42.0,
        };

        assert_eq!(triangle.get_area(), 882.0);
    }
}

#[cfg(test)]
mod sides_triangle {
    #[test]
    fn get_semi_perimeter() {
        let triangle = super::SidesTriangle {
            side_a: 42.0,
            side_b: 42.0,
            side_c: 42.0,
        };

        assert_eq!(triangle.get_semi_perimeter(), 63.0);
    }

    #[test]
    fn get_area() {
        let triangle = super::SidesTriangle {
            side_a: 42.0,
            side_b: 42.0,
            side_c: 42.0,
        };

        assert_eq!(triangle.get_area(), 763.8344);
    }
}