rewrite: readability

This commit is contained in:
2024-12-13 00:02:50 -05:00
parent ab006db973
commit e99912465b

View File

@@ -1,27 +1,17 @@
use crate::common::{self, Context, Error}; use crate::common::{self, Context, Error};
use std::io::Cursor; use std::io::Cursor;
/// Evaluates a Lamm program /// Evaluate a program written in the Lamm programming language
#[poise::command(prefix_command, aliases("lamm"))] #[poise::command(prefix_command, aliases("lamm"))]
pub async fn eval(ctx: Context<'_>, expr: poise::CodeBlock) -> Result<(), Error> { pub async fn eval(ctx: Context<'_>, expr: poise::CodeBlock) -> Result<(), Error> {
let runtime = lamm::Runtime::new(Cursor::new(expr.code), "<eval>"); let runtime = lamm::Runtime::new(Cursor::new(expr.code), "<eval>");
let values = runtime.values().fold(Ok(String::new()), |acc, v| { let values = runtime.values()
if acc.is_err() { .map(|res| res.map(|value| value.to_string()))
return acc; .collect::<Result<Vec<_>, _>>();
};
let x = acc.unwrap();
match v {
Ok(lamm::Value::Nil) => Ok(x),
Ok(v) => Ok(format!("{x}\n{v}")),
Err(e) => Err(e),
}
});
match values { match values {
Ok(values) => common::no_ping_reply(&ctx, format!("{values}")).await?, Ok(values) => common::no_ping_reply(&ctx, format!("{}", values.join("\n"))).await?,
Err(e) => common::no_ping_reply(&ctx, format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await?, Err(e) => common::no_ping_reply(&ctx, format!("```ansi\n\x1b[31;1merror\x1b[0m: {e}\n```")).await?,
}; };