use crate::render::Color;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Text {
pub(crate) x: f64,
pub(crate) y: f64,
pub(crate) text: String,
pub(crate) color: Option<Color>,
}
impl Text {
pub fn at(x: f64, y: f64, text: impl Into<String>) -> Text {
let text = Text {
x,
y,
text: text.into(),
color: None,
};
text.validate().expect("Text::at requires a finite anchor");
text
}
#[must_use]
pub fn color(mut self, color: Color) -> Text {
self.color = Some(color);
self
}
pub(crate) fn validate(&self) -> crate::Result<()> {
if self.x.is_finite() && self.y.is_finite() {
Ok(())
} else {
Err(crate::Error::InvalidParameter {
detail: "a Text anchor must be finite",
})
}
}
}