ddw_utils/
lib.rs

1//! # ddw_utils
2//!
3//! `ddw_utils` is a collection of utilities to make performing certain
4//! calculations more convenient.
5
6//! # ddw_utils(どきどきどわーふ_ユーティリティークレート)
7//!
8//! `ddw_utils`は、特定の計算をより便利に行うためのユーティリティの集まりです。
9
10pub use crate::kinds::PrimaryColor;
11pub use crate::kinds::SecondaryColor;
12pub use crate::utils::mix;
13
14/// Adds one to the number given.
15/// 与えられた数値に1を加える。
16///
17/// # Examples
18///
19/// ```
20/// let arg = 5;
21/// let answer = ddw_utils::add_one(arg);
22///
23/// assert_eq!(6, answer);
24/// ```
25pub fn add_one(x: isize) -> isize {
26    x + 1
27}
28
29/// double the given number.
30/// 与えられた数値を2倍にする。
31///
32/// # Examples
33///
34/// ```
35/// let arg = 5;
36/// let answer = ddw_utils::x2(arg);
37///
38/// assert_eq!(10, answer);
39/// ```
40pub fn x2(x: isize) -> isize {
41    x * 2
42}
43
44// kindsモジュールの定義
45pub mod kinds {
46    /// The primary colors according to the RYB color model.
47    /// RYBカラーモデルによる主要色。
48    pub enum PrimaryColor {
49        Red,
50        Yellow,
51        Blue,
52    }
53
54    /// The secondary colors according to the RYB color model.
55    /// RYBカラーモデルによる二次色。
56    pub enum SecondaryColor {
57        Orange,
58        Green,
59        Purple,
60    }
61}
62
63// utilsモジュールの定義
64pub mod utils {
65    use crate::kinds::PrimaryColor;
66    use crate::kinds::SecondaryColor;
67    /// Combines two primary colors in equal amounts to create
68    /// a secondary color.
69    /// 二つの主要色を同量混ぜ合わせて、二次色を作成する。
70    pub fn mix(c1: PrimaryColor, c2: PrimaryColor) -> Result<SecondaryColor, &'static str> {
71        let result = match (c1, c2) {
72            (PrimaryColor::Red, PrimaryColor::Yellow) => SecondaryColor::Orange,
73            (PrimaryColor::Red, PrimaryColor::Blue) => SecondaryColor::Purple,
74            (PrimaryColor::Yellow, PrimaryColor::Blue) => SecondaryColor::Green,
75            (PrimaryColor::Yellow, PrimaryColor::Red) => SecondaryColor::Orange,
76            (PrimaryColor::Blue, PrimaryColor::Red) => SecondaryColor::Purple,
77            (PrimaryColor::Blue, PrimaryColor::Yellow) => SecondaryColor::Green,
78            _ => return Err("該当色がありません")
79        };
80        Ok(result)
81    }
82}