Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pdl-live-react/src-tauri/src/pdl/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `> `.
Expand All @@ -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<String>,
Expand Down
14 changes: 5 additions & 9 deletions pdl-live-react/src-tauri/src/pdl/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatMessage>;
Expand Down Expand Up @@ -286,22 +286,18 @@ 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 {
while bytes_read > 0 {
bytes_read = ::std::io::stdin().read_line(&mut buffer)?;
}
}
Ok(buffer)
buffer
}
x => Err(Box::<dyn Error + Send + Sync>::from(format!(
"Unsupported value for read field: {:?}",
x
))),
}?;
};

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

Expand Down