zng_unit/
lib.rs

1#![doc(html_favicon_url = "https://zng-ui.github.io/res/zng-logo-icon.png")]
2#![doc(html_logo_url = "https://zng-ui.github.io/res/zng-logo.png")]
3//!
4//! Base unit types.
5//!
6//! # Crate
7//!
8#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
9#![warn(unused_extern_crates)]
10#![warn(missing_docs)]
11
12mod angle;
13mod byte;
14mod color;
15mod corner_radius;
16mod distance_key;
17mod factor;
18mod float_eq;
19mod frequency;
20mod orientation;
21mod px_density;
22mod px_dip;
23mod side_offsets;
24mod time;
25mod transform;
26
27use std::fmt;
28
29#[doc(no_inline)]
30pub use euclid;
31
32pub use angle::*;
33pub use byte::*;
34pub use color::*;
35pub use corner_radius::*;
36pub use distance_key::*;
37pub use factor::*;
38pub use float_eq::*;
39pub use frequency::*;
40pub use orientation::*;
41pub use px_density::*;
42pub use px_dip::*;
43pub use side_offsets::*;
44pub use time::*;
45pub use transform::*;
46
47pub(crate) fn parse_suffix<T: std::str::FromStr>(mut s: &str, suffixes: &[&'static str]) -> Result<T, <T as std::str::FromStr>::Err> {
48    for suffix in suffixes {
49        if let Some(f) = s.strip_suffix(suffix) {
50            s = f;
51            break;
52        }
53    }
54    s.parse()
55}
56
57/// An error which can be returned when parsing an type composed of integers.
58#[derive(Debug)]
59#[non_exhaustive]
60pub enum ParseIntCompositeError {
61    /// Color component parse error.
62    Component(std::num::ParseIntError),
63    /// Missing color component.
64    MissingComponent,
65    /// Extra color component.
66    ExtraComponent,
67    /// Unexpected char.
68    UnknownFormat,
69}
70impl fmt::Display for ParseIntCompositeError {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        match self {
73            ParseIntCompositeError::Component(e) => write!(f, "error parsing component, {e}"),
74            ParseIntCompositeError::MissingComponent => write!(f, "missing component"),
75            ParseIntCompositeError::ExtraComponent => write!(f, "extra component"),
76            ParseIntCompositeError::UnknownFormat => write!(f, "unknown format"),
77        }
78    }
79}
80impl std::error::Error for ParseIntCompositeError {
81    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
82        if let ParseIntCompositeError::Component(e) = self {
83            Some(e)
84        } else {
85            None
86        }
87    }
88}
89impl From<std::num::ParseIntError> for ParseIntCompositeError {
90    fn from(value: std::num::ParseIntError) -> Self {
91        ParseIntCompositeError::Component(value)
92    }
93}