Skip to content

Commit f46f137

Browse files
committed
fix: rust interpreter ReadBlock StringOrNull cleanup
Signed-off-by: Nick Mitchell <[email protected]>
1 parent 62060f8 commit f46f137

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

pdl-live-react/src-tauri/src/pdl/ast.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,13 @@ pub struct PythonCodeBlock {
443443
pub code: String,
444444
}
445445

446+
#[derive(Serialize, Deserialize, Debug, Clone)]
447+
#[serde(untagged)]
448+
pub enum StringOrNull {
449+
Null,
450+
String(String),
451+
}
452+
446453
/// Read from a file or standard input.
447454
///
448455
/// Example. Read from the standard input with a prompt starting with `> `.
@@ -459,7 +466,7 @@ pub struct PythonCodeBlock {
459466
#[derive(Serialize, Deserialize, Debug, Clone)]
460467
pub struct ReadBlock {
461468
/// Name of the file to read. If `None`, read the standard input.
462-
pub read: Value,
469+
pub read: StringOrNull,
463470

464471
/// Name of the file to read. If `None`, read the standard input.
465472
pub message: Option<String>,

pdl-live-react/src-tauri/src/pdl/interpreter.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::pdl::ast::{
2828
ArrayBlock, CallBlock, Closure, DataBlock, EmptyBlock, FunctionBlock, IfBlock, ImportBlock,
2929
IncludeBlock, ListOrString, MessageBlock, ModelBlock, ObjectBlock, PdlBlock, PdlParser,
3030
PdlResult, PdlUsage, PythonCodeBlock, ReadBlock, RepeatBlock, Role, Scope, SequencingBlock,
31-
StringOrBoolean,
31+
StringOrBoolean, StringOrNull,
3232
};
3333

3434
type Context = Vec<ChatMessage>;
@@ -286,22 +286,18 @@ impl<'a> Interpreter<'a> {
286286
);
287287

288288
let buffer = match &block.read {
289-
Value::String(file_path) => Ok(read_file_to_string(self.path_to(file_path))?),
290-
Value::Null => {
289+
StringOrNull::String(file_path) => read_file_to_string(self.path_to(file_path))?,
290+
StringOrNull::Null => {
291291
let mut buffer = String::new();
292292
let mut bytes_read = ::std::io::stdin().read_line(&mut buffer)?;
293293
if let Some(true) = block.multiline {
294294
while bytes_read > 0 {
295295
bytes_read = ::std::io::stdin().read_line(&mut buffer)?;
296296
}
297297
}
298-
Ok(buffer)
298+
buffer
299299
}
300-
x => Err(Box::<dyn Error + Send + Sync>::from(format!(
301-
"Unsupported value for read field: {:?}",
302-
x
303-
))),
304-
}?;
300+
};
305301

306302
let result = self.def(&block.def, &buffer.clone().into(), &block.parser)?;
307303

0 commit comments

Comments
 (0)