From c322b0ffd7a0baa31b200cec82aa386c26188b53 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 25 Apr 2021 09:45:17 +0530 Subject: add `export-png` primitive --- src/app.rs | 3 +-- src/lisp/error.rs | 2 ++ src/lisp/prelude.rs | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index d0089e9..66a27e3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -904,8 +904,7 @@ impl<'ctx> AppState<'ctx> { }); } } - Brush::Circle(CircleBrush { size }) - | Brush::Line(LineBrush { size, .. }) => { + Brush::Circle(CircleBrush { size }) => { let pt = (x, y); let val = self.active_color; if let Ok(o) = self.paint_point(pt, val, size) { diff --git a/src/lisp/error.rs b/src/lisp/error.rs index f323bb8..9e9dc90 100644 --- a/src/lisp/error.rs +++ b/src/lisp/error.rs @@ -98,6 +98,7 @@ pub enum EvalError { DivByZero, TypeMismatch, NoFileName, + FileError(io::Error), AccessEmptyList, InvalidCoordinates((LispNumber, LispNumber)), AssertionError { expected: LispExpr, got: LispExpr }, @@ -126,6 +127,7 @@ impl fmt::Display for EvalError { Self::TypeMismatch => write!(f, "mismatched types"), Self::DivByZero => write!(f, "attempt to divide by zero"), Self::NoFileName => write!(f, "no file name specified"), + Self::FileError(e) => write!(f, "file error: {}", e), Self::AccessEmptyList => write!(f, "attempted to access empty list"), Self::InvalidCoordinates((x, y)) => write!(f, "invalid coordinates: {} {}", x, y), Self::AssertionError { expected, got } => { diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 7cb46ff..8088510 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -14,7 +14,7 @@ use crate::{ utils::{load_script, rect_coords}, }; -use std::convert::TryInto; +use std::{convert::TryInto, fs::File, io::BufWriter, path::Path}; #[macro_export] macro_rules! primitive { @@ -260,6 +260,18 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(1), "export-png", |args, app| { + if let LispExpr::StringLit(s) = &args[0] { + let export_path = Path::new(&s); + let file = File::create(export_path).map_err(EvalError::FileError)?; + let w = BufWriter::new(file); + app.export().write_png(w); + Ok(LispExpr::Unit) + } else { + Err(EvalError::TypeMismatch.into()) + } + }); + primitive!(env, Arity::Exact(0), "grid-enabled?", |_, app| { Ok(LispExpr::BoolLit(app.grid.enabled)) }); @@ -524,5 +536,6 @@ pub fn new_env() -> Result { primitive!(env, Arity::Exact(1), "id", |args, _| { Ok(args[0].clone()) }); + Ok(env) } -- cgit v1.2.3