Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ab86776
began updating math for safety concerns
RPG-Alex Apr 23, 2026
8fc1b21
began updating math for safety concerns
RPG-Alex Apr 23, 2026
ef39b56
created module for doing safe math
RPG-Alex Apr 23, 2026
716e644
working on ab_glyph draw method
RPG-Alex Apr 23, 2026
4d1eb13
Implemented unified `Ranged1DError` for refactoring for errors in ran…
RPG-Alex Apr 24, 2026
ac5b8ac
Continued method updates
RPG-Alex Apr 24, 2026
94f078d
Made error enum top level as it will apply broadly
RPG-Alex Apr 26, 2026
9f18660
Reworking map implementations throughout `ranged1d`
RPG-Alex Apr 27, 2026
91626fb
updated `discrete` mod's `map` function
RPG-Alex Apr 27, 2026
1c531c0
`PlotError` is now `MathError` to better fit error enum role
RPG-Alex Apr 27, 2026
1912604
updated `ranged2d/cartesian` to work with new error type
RPG-Alex Apr 28, 2026
77eae2d
updated `cartesian3d` to work with new error type
RPG-Alex Apr 28, 2026
2f67a85
continued updating generics for new error type
RPG-Alex Apr 28, 2026
91e5581
Keeping math_errors and math_guard but reverting all implementation
RPG-Alex Apr 29, 2026
b76c46f
Starting from plotters-backend, reworking math safety
RPG-Alex Apr 29, 2026
1973dc9
Rewrote the FontError to be unified enum for all fonts
RPG-Alex Apr 29, 2026
3cbef2f
Added unit tests for the FontError
RPG-Alex Apr 29, 2026
06abce9
backend `polygon` module is now math safe
RPG-Alex Apr 30, 2026
f53b764
backend `path` module is now math safe
RPG-Alex Apr 30, 2026
30fc80c
backend `line` module is now math safe
RPG-Alex Apr 30, 2026
80e3a6a
backend began working on `circle`
RPG-Alex Apr 30, 2026
9c9911b
backend `cicle` maths safety progress
RPG-Alex May 1, 2026
e063303
Made mathguard concrete, no more generics.
RPG-Alex May 4, 2026
79cd3fb
unit test coverage increased for backend
RPG-Alex May 5, 2026
b8df46d
`plotters-backend` is now passing for arithmetic
RPG-Alex May 5, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plotters-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
num-traits = "0.2.19"
136 changes: 136 additions & 0 deletions plotters-backend/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::MathError;
use std::error::Error;
/// The error produced by a drawing backend.
#[derive(Debug)]
pub enum DrawingErrorKind<E: Error + Send + Sync> {
/// A drawing backend error
DrawingError(E),
/// A font rendering error
FontError(Box<dyn Error + Send + Sync + 'static>),
/// A mathematical operation has failed
MathError(MathError),
}

impl<E: Error + Send + Sync> From<MathError> for DrawingErrorKind<E> {
fn from(err: MathError) -> Self {
DrawingErrorKind::MathError(err)
}
}

impl<E: Error + Send + Sync> std::fmt::Display for DrawingErrorKind<E> {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
DrawingErrorKind::DrawingError(e) => write!(fmt, "Drawing backend error: {}", e),
DrawingErrorKind::FontError(e) => write!(fmt, "Font loading error: {}", e),
DrawingErrorKind::MathError(e) => write!(fmt, "Math error: {}", e),
}
}
}

impl<E: Error + Send + Sync> Error for DrawingErrorKind<E> {}

#[cfg(test)]
mod tests {
use super::*;
use std::fmt;

#[derive(Debug)]
struct TestBackendError;

impl fmt::Display for TestBackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "backend exploded")
}
}

impl std::error::Error for TestBackendError {}

#[derive(Debug)]
struct TestFontError;

impl fmt::Display for TestFontError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "font exploded")
}
}

impl std::error::Error for TestFontError {}

#[test]
fn from_math_error_creates_math_error_variant() {
let err: DrawingErrorKind<TestBackendError> = MathError::ValueOutOfRange.into();

assert!(matches!(
err,
DrawingErrorKind::MathError(MathError::ValueOutOfRange)
));
}

#[test]
fn display_formats_drawing_backend_error() {
let err: DrawingErrorKind<TestBackendError> =
DrawingErrorKind::DrawingError(TestBackendError);

assert_eq!(err.to_string(), "Drawing backend error: backend exploded");
}

#[test]
fn display_formats_font_error() {
let err: DrawingErrorKind<TestBackendError> =
DrawingErrorKind::FontError(Box::new(TestFontError));

assert_eq!(err.to_string(), "Font loading error: font exploded");
}

#[test]
fn display_formats_math_error() {
let math_error = MathError::ValueOutOfRange;
let err: DrawingErrorKind<TestBackendError> = DrawingErrorKind::MathError(math_error);

assert_eq!(err.to_string(), format!("Math error: {}", math_error));
}

#[test]
fn drawing_error_kind_implements_error() {
fn assert_error<E: std::error::Error + Send + Sync>() {}

assert_error::<DrawingErrorKind<TestBackendError>>();
}

#[test]
fn drawing_error_variant_can_be_matched() {
let err: DrawingErrorKind<TestBackendError> =
DrawingErrorKind::DrawingError(TestBackendError);

match err {
DrawingErrorKind::DrawingError(e) => {
assert_eq!(e.to_string(), "backend exploded");
}
_ => panic!("expected DrawingError variant"),
}
}

#[test]
fn font_error_variant_can_be_matched() {
let err: DrawingErrorKind<TestBackendError> =
DrawingErrorKind::FontError(Box::new(TestFontError));

match err {
DrawingErrorKind::FontError(e) => {
assert_eq!(e.to_string(), "font exploded");
}
_ => panic!("expected FontError variant"),
}
}

#[test]
fn math_error_variant_can_be_matched() {
let err: DrawingErrorKind<TestBackendError> =
DrawingErrorKind::MathError(MathError::ZeroDivision);

match err {
DrawingErrorKind::MathError(MathError::ZeroDivision) => {}
_ => panic!("expected MathError::ZeroDivision variant"),
}
}
}
Loading