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