windjammer_ui/components/generated/
rating.rs

1#![allow(clippy::all)]
2#![allow(noop_method_call)]
3use super::traits::Renderable;
4
5#[derive(Clone, Debug, PartialEq, Copy)]
6pub enum RatingSize {
7    Small,
8    Medium,
9    Large,
10}
11
12#[derive(Debug, Clone, PartialEq)]
13pub struct Rating {
14    pub value: f32,
15    pub max: i32,
16    pub size: RatingSize,
17    pub readonly: bool,
18    pub color: String,
19}
20
21impl Rating {
22    #[inline]
23    pub fn new(value: f32) -> Rating {
24        Rating {
25            value,
26            max: 5,
27            size: RatingSize::Medium,
28            readonly: true,
29            color: "#fbbf24".to_string(),
30        }
31    }
32    #[inline]
33    pub fn max(mut self, max: i32) -> Rating {
34        self.max = max;
35        self
36    }
37    #[inline]
38    pub fn size(mut self, size: RatingSize) -> Rating {
39        self.size = size;
40        self
41    }
42    #[inline]
43    pub fn readonly(mut self, readonly: bool) -> Rating {
44        self.readonly = readonly;
45        self
46    }
47    #[inline]
48    pub fn color(mut self, color: String) -> Rating {
49        self.color = color;
50        self
51    }
52}
53
54impl Renderable for Rating {
55    #[inline]
56    fn render(self) -> String {
57        let star_size = match self.size {
58            RatingSize::Small => "16px".to_string(),
59            RatingSize::Medium => "24px".to_string(),
60            RatingSize::Large => "32px".to_string(),
61        };
62        let mut html = String::new();
63        html.push_str("<div style='display: inline-flex; gap: 4px;'>");
64        let mut i = 1;
65        while i <= self.max {
66            let filled = i as f32 <= self.value;
67            let half_filled = i as f32 - 0.5 <= self.value && i as f32 > self.value;
68            let star_color = {
69                if filled || half_filled {
70                    self.color.as_str()
71                } else {
72                    "#e2e8f0"
73                }
74            };
75            let cursor = {
76                if self.readonly {
77                    "default".to_string()
78                } else {
79                    "pointer".to_string()
80                }
81            };
82            html.push_str("<span style='font-size: ");
83            html.push_str(&star_size);
84            html.push_str("; color: ");
85            html.push_str(&star_color);
86            html.push_str("; cursor: ");
87            html.push_str(&cursor);
88            html.push_str(";'>");
89            if half_filled {
90                html.push('⯨')
91            } else {
92                html.push('★')
93            }
94            html.push_str("</span>");
95            i += 1;
96        }
97        html.push_str("</div>");
98        html
99    }
100}