durs_common_tools/lib.rs
1// Copyright (C) 2019 Éloïs SANCHEZ
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! Common rust tools for DURS project.
17
18#![deny(
19 missing_docs,
20 missing_debug_implementations,
21 missing_copy_implementations,
22 trivial_casts,
23 trivial_numeric_casts,
24 unsafe_code,
25 unstable_features,
26 unused_import_braces
27)]
28
29pub mod fns;
30pub mod macros;
31pub mod traits;
32
33/// Percent
34#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
35pub struct Percent(u8);
36
37/// Percent error
38#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
39pub enum PercentError {
40 /// Integer too large (greater than 100)
41 TooLarge(u8),
42}
43
44impl Percent {
45 /// New percent
46 pub fn new(percent: u8) -> Result<Percent, PercentError> {
47 if percent <= 100 {
48 Ok(Percent(percent))
49 } else {
50 Err(PercentError::TooLarge(percent))
51 }
52 }
53}
54
55impl Into<u8> for Percent {
56 fn into(self) -> u8 {
57 self.0
58 }
59}