use crate::error::ChartonError;
use crate::scale::{Expansion, ResolvedScale, Scale, ScaleDomain};
#[derive(Debug, Clone)]
pub struct Size {
pub(crate) field: String,
pub(crate) scale_type: Option<Scale>,
pub(crate) domain: Option<ScaleDomain>,
pub(crate) expansion: Option<Expansion>,
pub(crate) resolved_scale: ResolvedScale,
}
impl Size {
pub fn new(field: &str) -> Self {
Self {
field: field.to_string(),
scale_type: Some(Scale::Linear),
domain: None,
expansion: None,
resolved_scale: ResolvedScale::none(),
}
}
pub fn with_scale(mut self, scale_type: Scale) -> Result<Self, ChartonError> {
if matches!(scale_type, Scale::Discrete) {
return Err(ChartonError::Scale(
"Size encoding cannot use Scale::Discrete as size requires continuous data"
.to_string(),
));
}
self.scale_type = Some(scale_type);
Ok(self)
}
pub fn with_domain(mut self, domain: ScaleDomain) -> Self {
self.domain = Some(domain);
self
}
pub fn with_expansion(mut self, expansion: Expansion) -> Self {
self.expansion = Some(expansion);
self
}
}
pub fn size(field: &str) -> Size {
Size::new(field)
}