use std::fmt;
use crate::motion::MotionError;
pub use super::artifact::Artifact;
pub use super::lease::PresentationLease;
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Error {
InvalidInput,
BudgetExceeded,
WrongScope,
Stale,
QueueFull,
Revoked,
Motion(MotionError),
IntegerConversion(core::num::TryFromIntError),
}
impl Error {
pub(crate) fn require(valid: bool, error: Self) -> Result<(), Self> {
if valid { Ok(()) } else { Err(error) }
}
}
impl From<MotionError> for Error {
fn from(error: MotionError) -> Self {
Self::Motion(error)
}
}
impl From<core::num::TryFromIntError> for Error {
fn from(error: core::num::TryFromIntError) -> Self {
Self::IntegerConversion(error)
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "Canvas presentation: {self:?}")
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Self::Motion(ref error) => Some(error),
Self::IntegerConversion(ref error) => Some(error),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Limits {
text_bytes: usize,
queued_commands: usize,
pixels: usize,
}
impl Default for Limits {
fn default() -> Self {
Self {
text_bytes: 262_144,
queued_commands: 32,
pixels: 8_847_360,
}
}
}
impl Limits {
pub fn new(text_bytes: usize, queued_commands: usize, pixels: usize) -> Result<Self, Error> {
let envelope = Self {
text_bytes,
queued_commands,
pixels,
};
envelope.validate()?;
Ok(envelope)
}
pub fn validate(self) -> Result<Self, Error> {
Error::require(
self.text_bytes != 0
&& self.text_bytes <= 1_048_576
&& self.queued_commands != 0
&& self.queued_commands <= 256
&& self.pixels != 0
&& self.pixels <= 8_847_360,
Error::InvalidInput,
)?;
Ok(self)
}
#[must_use]
pub const fn text_bytes(&self) -> usize {
self.text_bytes
}
#[must_use]
pub const fn queued_commands(&self) -> usize {
self.queued_commands
}
#[must_use]
pub const fn pixels(&self) -> usize {
self.pixels
}
pub fn pixel_count(self, width: u32, height: u32) -> Result<usize, Error> {
Error::require(width != 0 && height != 0, Error::InvalidInput)?;
let width_count = usize::try_from(width)?;
let height_count = usize::try_from(height)?;
let count = width_count
.checked_mul(height_count)
.ok_or(Error::BudgetExceeded)?;
Error::require(count <= self.pixels, Error::BudgetExceeded)?;
Ok(count)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum View {
Source,
Proposal,
Evidence,
}
impl View {
pub const ALL: [Self; 3] = [Self::Source, Self::Proposal, Self::Evidence];
#[must_use]
pub const fn index(self) -> usize {
match self {
Self::Source => 0,
Self::Proposal => 1,
Self::Evidence => 2,
}
}
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Source => "Source",
Self::Proposal => "Proposal",
Self::Evidence => "Evidence",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum CommitStatus {
#[default]
Proposed,
Running,
Failed,
Committed,
}
impl CommitStatus {
pub const ALL: [Self; 4] = [Self::Proposed, Self::Running, Self::Failed, Self::Committed];
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Proposed => "Proposed",
Self::Running => "Running",
Self::Failed => "Failed",
Self::Committed => "Committed",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Action {
Navigate(View),
Step(i32),
ToggleReducedMotion,
ReducedMotion(bool),
Scroll {
view: View,
lines: i32,
},
Transition(CommitStatus),
OptimisticTransition(CommitStatus),
RevertOptimistic,
}
impl Action {
#[must_use]
pub const fn scroll(view: View, lines: i32) -> Self {
Self::Scroll { view, lines }
}
}