word_cloud/words/
word.rs

1use std::fmt::Display;
2
3use crate::geometry::{Orientation, Vector};
4use image::Rgb;
5
6#[derive(Debug, Clone)]
7pub struct Word {
8    pub pos: Vector,
9    pub text: String,
10    pub orientation: Orientation,
11    pub color: Rgb<u8>,
12    pub font_name: String,
13    pub size: u32,
14    pub count: u32,
15}
16
17impl Display for Word {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(f, "Word[{}]", self.text)
20    }
21}
22
23impl Word {
24    pub fn new(
25        text: &str,
26        orientation: Orientation,
27        color: Rgb<u8>,
28        font_name: &str,
29        size: u32,
30    ) -> Word {
31        Word {
32            text: text.to_string(),
33            orientation,
34            color,
35            font_name: font_name.to_string(),
36            size,
37            count: 1,
38            pos: (0, 0),
39        }
40    }
41
42    pub fn displace(&self, pos: Vector) -> Word {
43        let mut word = self.clone();
44        word.pos = pos;
45
46        word
47    }
48}
49
50#[cfg(test)]
51mod test {
52    use super::*;
53
54    #[test]
55    fn word_construction() {
56        let red = Rgb([255, 0, 0]);
57
58        let word = Word::new("Hello", Orientation::Horizontal, red, "dejavu", 12);
59
60        assert_eq!("Hello", word.text);
61        assert_eq!(Orientation::Horizontal, word.orientation);
62        assert_eq!(red, word.color);
63        assert_eq!("dejavu", word.font_name);
64        assert_eq!(12, word.size);
65    }
66}