diff --git a/pdl-live-react/src-tauri/src/pdl/ast.rs b/pdl-live-react/src-tauri/src/pdl/ast.rs index d655efe21..5fd8d24ad 100644 --- a/pdl-live-react/src-tauri/src/pdl/ast.rs +++ b/pdl-live-react/src-tauri/src/pdl/ast.rs @@ -443,6 +443,13 @@ pub struct PythonCodeBlock { pub code: String, } +#[derive(Serialize, Deserialize, Debug, Clone)] +#[serde(untagged)] +pub enum StringOrNull { + Null, + String(String), +} + /// Read from a file or standard input. /// /// Example. Read from the standard input with a prompt starting with `> `. @@ -459,7 +466,7 @@ pub struct PythonCodeBlock { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ReadBlock { /// Name of the file to read. If `None`, read the standard input. - pub read: Value, + pub read: StringOrNull, /// Name of the file to read. If `None`, read the standard input. pub message: Option, diff --git a/pdl-live-react/src-tauri/src/pdl/interpreter.rs b/pdl-live-react/src-tauri/src/pdl/interpreter.rs index 7caaadbed..e695f87fd 100644 --- a/pdl-live-react/src-tauri/src/pdl/interpreter.rs +++ b/pdl-live-react/src-tauri/src/pdl/interpreter.rs @@ -28,7 +28,7 @@ use crate::pdl::ast::{ ArrayBlock, CallBlock, Closure, DataBlock, EmptyBlock, FunctionBlock, IfBlock, ImportBlock, IncludeBlock, ListOrString, MessageBlock, ModelBlock, ObjectBlock, PdlBlock, PdlParser, PdlResult, PdlUsage, PythonCodeBlock, ReadBlock, RepeatBlock, Role, Scope, SequencingBlock, - StringOrBoolean, + StringOrBoolean, StringOrNull, }; type Context = Vec; @@ -286,8 +286,8 @@ impl<'a> Interpreter<'a> { ); let buffer = match &block.read { - Value::String(file_path) => Ok(read_file_to_string(self.path_to(file_path))?), - Value::Null => { + StringOrNull::String(file_path) => read_file_to_string(self.path_to(file_path))?, + StringOrNull::Null => { let mut buffer = String::new(); let mut bytes_read = ::std::io::stdin().read_line(&mut buffer)?; if let Some(true) = block.multiline { @@ -295,13 +295,9 @@ impl<'a> Interpreter<'a> { bytes_read = ::std::io::stdin().read_line(&mut buffer)?; } } - Ok(buffer) + buffer } - x => Err(Box::::from(format!( - "Unsupported value for read field: {:?}", - x - ))), - }?; + }; let result = self.def(&block.def, &buffer.clone().into(), &block.parser)?;