From 837e4af12e683a3d006179823e352b7a5b276e5a Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Fri, 15 May 2026 20:12:42 -0400 Subject: Update pit --- Cargo.toml | 2 +- crates/teleia/src/script.rs | 1 - crates/teleia/src/script/nrepl.rs | 66 +++++++++++++++++++++++++++++++++++++-- flake.lock | 6 ++-- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98dfb24..5276f7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = ["crates/*"] [workspace.package] version = "0.1.0" -edition = "2021" +edition = "2024" authors = ["LLLL Colonq "] [workspace.metadata.crane] diff --git a/crates/teleia/src/script.rs b/crates/teleia/src/script.rs index 96e8f6b..a7e3584 100644 --- a/crates/teleia/src/script.rs +++ b/crates/teleia/src/script.rs @@ -138,7 +138,6 @@ impl Runtime { let e = unsafe { pit_get_error(self.rt) }; let ve = Value { val: e }; if self.eq(ve, NIL) { return Ok(()) }; - eprintln!("ve: {}", ve.val.data); Err(Error { msg: self.dump(ve).unwrap_or("".to_owned()) }.into()) } pub fn parse(&mut self, s: &str) -> utils::Erm { diff --git a/crates/teleia/src/script/nrepl.rs b/crates/teleia/src/script/nrepl.rs index 633fa0a..b35fdf6 100644 --- a/crates/teleia/src/script/nrepl.rs +++ b/crates/teleia/src/script/nrepl.rs @@ -1,8 +1,8 @@ use super::bencode; -use crate::{utils, Erm, WrapErr}; +use crate::{Erm, WrapErr, script, utils}; use std::io::{BufReader, Read, Write}; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::net::{TcpListener, TcpStream}; use std::sync::mpsc::{Sender, Receiver, channel}; use std::sync::{Arc, Mutex}; @@ -139,4 +139,66 @@ impl Server { log::warn!("tried to send message to client {}, but nREPL is not connected", client); } } + pub fn reply<'a, I>(&self, client: ClientId, id: bencode::Value, dict: I) + where I: Iterator { + self.send(client, bencode::Value::Dictionary(BTreeMap::from_iter(dict.map(|(k, v)| { + (Vec::from(k), v) + }).chain([ + (Vec::from(b"id"), id), + (Vec::from(b"session"), bencode::Value::Bytestring(Vec::from(b"global"))), + (Vec::from(b"status"), bencode::Value::List(vec![ + bencode::Value::Bytestring(Vec::from(b"done")) + ])), + ])))); + } + pub fn update(&mut self, rt: &mut script::Runtime) { + while let Some((cid, msg)) = self.poll() { + let d = if let bencode::Value::Dictionary(d) = &msg { d } else { + log::warn!("non-dictionary message from nREPL client: {}", msg); + return; + }; + let (id, op) = if let Some(id) = d.get(&b"id"[..]) + && let Some(vop) = d.get(&b"op"[..]) + && let bencode::Value::Bytestring(op) = vop { + (id, op) + } else { + log::warn!("message with no id/op from nREPL client: {}", msg); + return; + }; + let res: Erm<()> = try { + match &op[..] { + b"clone" => self.reply(cid, id.clone(), [ + (&b"new-session"[..], bencode::Value::Bytestring(Vec::from(b"global"))), + ].into_iter()), + b"describe" => self.reply(cid, id.clone(), [ + (&b"aux"[..], bencode::Value::Dictionary(BTreeMap::new())), + (&b"middleware"[..], bencode::Value::List(Vec::new())), + (&b"ops"[..], bencode::Value::Dictionary(BTreeMap::new())), + (&b"versions"[..], bencode::Value::Dictionary(BTreeMap::new())), + ].into_iter()), + b"eval" => { + if let Some(bencode::Value::Bytestring(code)) = d.get(&b"code"[..]) { + let scode = str::from_utf8(&code) + .or(utils::erm_msg("failed to decode utf-8"))?; + let expr = rt.parse(scode)?; + let v = rt.eval(expr)?; + let res = rt.dump(v)?; + self.reply(cid, id.clone(), [ + (&b"value"[..], bencode::Value::Bytestring(res.into_bytes())), + ].into_iter()) + } else { + utils::erm_msg(&format!("eval with no code: {}", msg))?; + } + }, + _ => utils::erm_msg(&format!("message with unknown op: {}", msg))?, + } + }; + if let Err(e) = res { + self.reply(cid, id.clone(), [ + (&b"ex"[..], bencode::Value::Bytestring(Vec::from(b"teleia"))), + (&b"err"[..], bencode::Value::Bytestring(format!("{}", e).into_bytes())), + ].into_iter()); + } + } + } } diff --git a/flake.lock b/flake.lock index 4922d53..67e21a5 100644 --- a/flake.lock +++ b/flake.lock @@ -75,11 +75,11 @@ ] }, "locked": { - "lastModified": 1778196621, - "narHash": "sha256-W/6tiwKZfNaDxLldohLjvqZdoQn1TFfoz4XQCrqy38Y=", + "lastModified": 1778890311, + "narHash": "sha256-JZx/jh/E16SlAATrzQvB7kZcbBBfXX0zCqpwgw+Qlfc=", "owner": "lcolonq", "repo": "pit", - "rev": "2e42e30b6df2b744c45f82d2028a627b3aca7d3d", + "rev": "48ab2cb8e3de88dbd0722337bd4617ad22de1a12", "type": "github" }, "original": { -- cgit v1.2.3