More idiomatic constructors.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-06-27 10:20:21 -07:00
parent f24a90b77b
commit 78a360ae89
3 changed files with 38 additions and 36 deletions

View File

@@ -5,7 +5,7 @@ use std::path::Path;
use png;
use thiserror::Error;
use crate::tuples::{color, Color};
use crate::tuples::Color;
#[derive(Error, Debug)]
pub enum CanvasError {
@@ -20,16 +20,16 @@ pub struct Canvas {
pub pixels: Vec<Color>,
}
pub fn canvas(width: usize, height: usize) -> Canvas {
let pixels = vec![color(0., 0., 0.); width * height];
Canvas {
width,
height,
pixels,
}
}
impl Canvas {
pub fn new(width: usize, height: usize) -> Canvas {
let pixels = vec![Color::new(0., 0., 0.); width * height];
Canvas {
width,
height,
pixels,
}
}
pub fn set(&mut self, x: usize, y: usize, c: Color) {
if x > self.width {
return;
@@ -74,14 +74,14 @@ impl Canvas {
mod tests {
use super::canvas;
use crate::tuples::color;
use crate::tuples::Color;
#[test]
fn create_canvas() {
let c = canvas(10, 20);
assert_eq!(c.width, 10);
assert_eq!(c.height, 20);
let black = color(0., 0., 0.);
let black = Color::new(0., 0., 0.);
for (i, p) in c.pixels.iter().enumerate() {
assert_eq!(p, &black, "pixel {} not {:?}: {:?}", i, &black, p);
}
@@ -90,7 +90,7 @@ mod tests {
#[test]
fn write_to_canvas() {
let mut c = canvas(10, 20);
let red = color(1., 0., 0.);
let red = Color::new(1., 0., 0.);
c.set(2, 3, red);
assert_eq!(c.get(2, 3), red);
}