windjammer_ui/components/generated/
rating.rs

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