Skip to main content

glory_render/
progress.rs

1use crate::{Error, RangeValue};
2/// Read-only progress. `None` is indeterminate, never an invented zero value.
3#[derive(Clone, Debug, PartialEq)]
4pub struct ProgressValue {
5    pub min: f64,
6    pub max: f64,
7    pub current: Option<f64>,
8    pub value_text: Option<String>,
9}
10impl ProgressValue {
11    pub fn validate(&self) -> Result<(), Error> {
12        RangeValue {
13            min: self.min,
14            max: self.max,
15            value: self.current.unwrap_or(self.min),
16        }
17        .validate()?;
18        if self
19            .value_text
20            .as_ref()
21            .is_some_and(|text| text.trim().is_empty() || text.len() > 1024)
22        {
23            return Err(Error::InvalidProps(
24                "progress text must be nonempty and at most 1024 UTF-8 bytes",
25            ));
26        }
27        Ok(())
28    }
29    pub fn fraction(&self) -> Result<Option<f64>, Error> {
30        self.validate()?;
31        self.current
32            .map(|value| {
33                RangeValue {
34                    min: self.min,
35                    max: self.max,
36                    value,
37                }
38                .fraction()
39            })
40            .transpose()
41    }
42}