Skip to content

Commit 094a775

Browse files
committed
Add tracing for cg3
1 parent c81ff1a commit 094a775

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ impl Display for Command {
198198
#[serde(untagged)]
199199
pub enum Value {
200200
Int(isize),
201+
Bool(bool),
201202
String(String),
202203
Array(Vec<Value>),
203204
Map(IndexMap<String, Value>),
@@ -215,6 +216,13 @@ impl Value {
215216
format!("{}", x)
216217
}
217218
}
219+
Value::Bool(x) => {
220+
if ansi {
221+
format!("\x1b[1;34m{}\x1b[0m", x)
222+
} else {
223+
format!("{}", x)
224+
}
225+
}
218226
Value::String(x) => {
219227
if ansi {
220228
format!("\x1b[1;31m{:?}\x1b[0m", x)
@@ -295,6 +303,13 @@ impl Value {
295303
}
296304
}
297305

306+
pub fn try_as_bool(&self) -> Option<bool> {
307+
match self {
308+
Value::Bool(x) => Some(*x),
309+
_ => None,
310+
}
311+
}
312+
298313
pub fn try_as_string(&self) -> Option<String> {
299314
match self {
300315
Value::String(x) => Some(x.clone()),
@@ -355,6 +370,7 @@ impl Value {
355370

356371
pub fn try_as_json(&self) -> Result<serde_json::Value, serde_json::Error> {
357372
match self {
373+
Value::Bool(x) => Ok(serde_json::Value::Bool(*x)),
358374
Value::Int(x) => Ok(serde_json::Value::Number(serde_json::Number::from(*x))),
359375
Value::String(x) => Ok(serde_json::Value::String(x.clone())),
360376
Value::Array(x) => Ok(serde_json::Value::Array(

src/modules/cg3.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{collections::HashMap, str::FromStr, sync::Arc, thread::JoinHandle};
22

33
use async_trait::async_trait;
4-
use divvun_runtime_macros::rt_command;
5-
use indexmap::IndexMap;
4+
use divvun_runtime_macros::{rt_command, rt_struct};
65
use once_cell::sync::Lazy;
76
use regex::Regex;
7+
use serde::{Deserialize, Serialize};
88
use tokio::sync::{
99
Mutex,
1010
mpsc::{self, Receiver, Sender},
@@ -460,13 +460,22 @@ pub struct Vislcg3 {
460460
_thread: JoinHandle<()>,
461461
}
462462

463+
#[rt_struct(module = "cg3")]
464+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
465+
struct Vislcg3Config {
466+
trace: bool,
467+
}
468+
463469
#[rt_command(
464470
module = "cg3",
465471
name = "vislcg3",
466472
input = [String],
467473
output = "String",
468474
kind = "cg3",
469-
args = [model_path = "Path"]
475+
args = [
476+
model_path = "Path",
477+
config? = "Vislcg3Config",
478+
]
470479
)]
471480
impl Vislcg3 {
472481
pub fn new(
@@ -482,11 +491,27 @@ impl Vislcg3 {
482491
.ok_or_else(|| Error("model_path missing".to_string()))?;
483492
let model_path = context.extract_to_temp_dir(model_path)?;
484493

494+
let config = match kwargs
495+
.remove("config")
496+
.and_then(|x| x.value)
497+
.map(|x| x.try_as_json())
498+
{
499+
Some(Ok(c)) => {
500+
let config: Vislcg3Config = serde_json::from_value(c)
501+
.map_err(|e| Error(format!("config arg is not valid SpellerConfig: {}", e)))?;
502+
Some(config)
503+
}
504+
Some(Err(e)) => return Err(Error(format!("config arg is not valid JSON: {}", e))),
505+
None => None,
506+
};
507+
let config = config.unwrap_or_default();
508+
485509
let (input_tx, mut input_rx) = mpsc::channel(1);
486510
let (output_tx, output_rx) = mpsc::channel(1);
487511

488512
let thread = std::thread::spawn(move || {
489513
let applicator = cg3::Applicator::new(&model_path);
514+
applicator.set_trace(config.trace);
490515

491516
loop {
492517
let Some(Some(input)): Option<Option<String>> = input_rx.blocking_recv() else {

0 commit comments

Comments
 (0)