use crate::{Error, Result};
pub(crate) mod function;
mod recipe;
pub use function::{TickContext, TickPositions};
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TickGeneration {
primary: Primary,
maximum: Option<usize>,
}
impl Eq for TickGeneration {}
#[derive(Debug, Clone, Default, PartialEq)]
enum Primary {
#[default]
Automatic,
Interval {
step: f64,
origin: f64,
space: TickSpace,
requirement: TickRequirement,
binding: IntervalBinding,
},
Target(usize),
ExactCount {
count: usize,
space: TickSpace,
singleton: TickSingleton,
},
Multiplicative {
anchor: f64,
ratio: f64,
requirement: TickRequirement,
},
Function(function::Function),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TickSpace {
Data,
Scale,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TickRequirement {
Fixed,
Preferred,
}
#[derive(Debug, Clone, Copy)]
pub enum TickSingleton {
Lower,
Midpoint,
Upper,
Position(f64),
}
impl PartialEq for TickSingleton {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Lower, Self::Lower)
| (Self::Midpoint, Self::Midpoint)
| (Self::Upper, Self::Upper) => true,
(Self::Position(a), Self::Position(b)) => a.to_bits() == b.to_bits(),
_ => false,
}
}
}
impl Eq for TickSingleton {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IntervalBinding {
Linear,
Log10,
Context,
}
impl TickGeneration {
pub fn function<F>(function: F) -> Self
where
F: for<'a> Fn(&TickContext<'a>) -> Result<TickPositions> + Send + Sync + 'static,
{
Self {
primary: Primary::Function(function::Function::new(function)),
maximum: None,
}
}
pub(crate) fn position_function(&self) -> Option<&function::Function> {
if let Primary::Function(function) = &self.primary {
Some(function)
} else {
None
}
}
pub const fn automatic() -> Self {
Self {
primary: Primary::Automatic,
maximum: None,
}
}
pub const fn is_automatic(&self) -> bool {
matches!(self.primary, Primary::Automatic) && self.maximum.is_none()
}
pub fn additive(step: f64) -> Result<Self> {
if !step.is_finite() || step <= 0.0 {
return Err(Error::Data(
"tick interval must be finite and strictly positive".into(),
));
}
Ok(Self {
primary: Primary::Interval {
step,
origin: 0.0,
space: TickSpace::Data,
requirement: TickRequirement::Preferred,
binding: IntervalBinding::Linear,
},
maximum: None,
})
}
pub fn log10_decades(decades: u32) -> Result<Self> {
if decades == 0 {
return Err(Error::Data(
"tick interval decades must be strictly positive".into(),
));
}
Ok(Self {
primary: Primary::Interval {
step: f64::from(decades),
origin: 0.0,
space: TickSpace::Scale,
requirement: TickRequirement::Preferred,
binding: IntervalBinding::Log10,
},
maximum: None,
})
}
pub fn interval(
step: f64,
origin: f64,
space: TickSpace,
requirement: TickRequirement,
) -> Result<Self> {
if !step.is_finite() || step <= 0.0 || !origin.is_finite() {
return Err(Error::Data(
"tick interval needs a finite positive step and finite origin".into(),
));
}
Ok(Self {
primary: Primary::Interval {
step,
origin: if origin == 0.0 { 0.0 } else { origin },
space,
requirement,
binding: IntervalBinding::Context,
},
maximum: None,
})
}
pub const fn interval_parameters(&self) -> Option<(f64, f64, TickSpace, TickRequirement)> {
if let Primary::Interval {
step,
origin,
space,
requirement,
..
} = self.primary
{
Some((step, origin, space, requirement))
} else {
None
}
}
pub fn multiplicative(anchor: f64, ratio: f64, requirement: TickRequirement) -> Result<Self> {
if !anchor.is_finite()
|| anchor <= 0.0
|| !ratio.is_finite()
|| ratio <= 0.0
|| ratio.to_bits() == 1.0_f64.to_bits()
{
return Err(Error::Data("multiplicative ticks require a finite positive anchor and positive ratio other than one".into()));
}
Ok(Self {
primary: Primary::Multiplicative {
anchor,
ratio,
requirement,
},
maximum: None,
})
}
pub const fn multiplicative_parameters(&self) -> Option<(f64, f64, TickRequirement)> {
if let Primary::Multiplicative {
anchor,
ratio,
requirement,
} = self.primary
{
Some((anchor, ratio, requirement))
} else {
None
}
}
pub(crate) fn validate_scale(&self, log: bool) -> Result<()> {
if let Primary::Interval { binding, .. } = self.primary {
if (binding == IntervalBinding::Linear && log)
|| (binding == IntervalBinding::Log10 && !log)
{
return Err(Error::Data(
"tick interval does not match the axis scale".into(),
));
}
}
Ok(())
}
pub fn target(count: usize) -> Result<Self> {
Self::validate_count(count)?;
Ok(Self {
primary: Primary::Target(count),
maximum: None,
})
}
pub fn maximum(count: usize) -> Result<Self> {
Self::validate_count(count)?;
Ok(Self {
primary: Primary::Automatic,
maximum: Some(count),
})
}
fn validate_count(count: usize) -> Result<()> {
if count == 0 {
return Err(Error::Data("generated tick count must be positive".into()));
}
Ok(())
}
pub const fn target_count(&self) -> Option<usize> {
match self.primary {
Primary::Target(count) => Some(count),
_ => None,
}
}
pub const fn get_maximum_count(&self) -> Option<usize> {
self.maximum
}
pub fn maximum_count(mut self, maximum: Option<usize>) -> Result<Self> {
if let Some(maximum) = maximum {
Self::validate_count(maximum)?;
if self
.exact_parameters()
.is_some_and(|(count, _, _)| count > maximum)
{
return Err(Error::Data(
"exact tick count exceeds the generated maximum".into(),
));
}
}
self.maximum = maximum;
Ok(self)
}
pub const fn exact_count(count: usize, space: TickSpace) -> Self {
Self {
primary: Primary::ExactCount {
count,
space,
singleton: TickSingleton::Lower,
},
maximum: None,
}
}
pub fn singleton(mut self, position: TickSingleton) -> Result<Self> {
if let TickSingleton::Position(value) = position {
if !value.is_finite() {
return Err(Error::Data("tick singleton must be finite".into()));
}
}
let Primary::ExactCount { singleton, .. } = &mut self.primary else {
return Err(Error::Data(
"singleton placement requires an exact-count locator".into(),
));
};
*singleton = position;
Ok(self)
}
pub(crate) const fn exact_parameters(&self) -> Option<(usize, TickSpace, TickSingleton)> {
if let Primary::ExactCount {
count,
space,
singleton,
} = self.primary
{
Some((count, space, singleton))
} else {
None
}
}
pub(crate) const fn is_interval(&self) -> bool {
matches!(self.primary, Primary::Interval { .. })
}
pub(crate) fn observe_feedback(
&self,
round: &mut crate::layout::kernel::FeedbackRound,
) -> Result<()> {
round.observe_count(self.maximum.unwrap_or(0))?;
match &self.primary {
Primary::Automatic => round.observe_count(0),
Primary::Function(function) => {
round.observe_count(6)?;
round.observe_position_function(function)
}
Primary::Interval {
step,
origin,
space,
requirement,
binding,
} => {
round.observe_count(1)?;
round.observe_count(*binding as usize)?;
round.observe_number(*origin)?;
round.observe_count(*space as usize)?;
round.observe_count(*requirement as usize)?;
round.observe_number(*step)
}
Primary::Target(count) => {
round.observe_count(3)?;
round.observe_count(*count)
}
Primary::Multiplicative {
anchor,
ratio,
requirement,
} => {
round.observe_count(5)?;
round.observe_number(*anchor)?;
round.observe_number(*ratio)?;
round.observe_count(*requirement as usize)
}
Primary::ExactCount {
count,
space,
singleton,
} => {
round.observe_count(4)?;
round.observe_count(*count)?;
round.observe_count(*space as usize)?;
match singleton {
TickSingleton::Lower => round.observe_count(0),
TickSingleton::Midpoint => round.observe_count(1),
TickSingleton::Upper => round.observe_count(2),
TickSingleton::Position(value) => {
round.observe_count(3)?;
round.observe_count(usize::from(value.is_sign_negative()))?;
round.observe_number(*value)
}
}
}
}
}
pub const fn additive_step(&self) -> Option<f64> {
match self.primary {
Primary::Interval {
step,
binding: IntervalBinding::Linear,
..
} => Some(step),
_ => None,
}
}
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "Log10 binding is constructed only from a nonzero exactly represented u32"
)]
pub const fn decade_step(&self) -> Option<u32> {
match self.primary {
Primary::Interval {
step,
binding: IntervalBinding::Log10,
..
} => Some(step as u32),
_ => None,
}
}
}