use core::fmt;
use embedded_graphics::{geometry::Size, primitives::Rectangle};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyPosition {
pub page: usize,
pub row: usize,
pub col: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ConfigError {
EmptyLayout,
InvalidPage(KeyPosition),
LabelTooLarge {
position: KeyPosition,
size: Size,
max: Size,
},
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyLayout => f.write_str("the layout has an empty page or row"),
Self::InvalidPage(p) => write!(
f,
"key at page {}, row {}, column {} points to a missing page",
p.page, p.row, p.col
),
Self::LabelTooLarge {
position: p,
size,
max,
} => write!(
f,
"label of the key at page {}, row {}, column {} is {}x{} px, \
but at most {}x{} px fit inside the key",
p.page, p.row, p.col, size.width, size.height, max.width, max.height
),
}
}
}
impl core::error::Error for ConfigError {}
pub(crate) fn max_label_size(bounds: Rectangle, padding: u32) -> Size {
let margin = padding.saturating_mul(2);
Size::new(
bounds.size.width.saturating_sub(margin),
bounds.size.height.saturating_sub(margin),
)
}