#[derive(Debug, Default, Clone, PartialEq)]
pub struct StructuredOutput {
pub lines: String,
pub json: serde_json::Value,
}
impl StructuredOutput {
pub fn new(lines: impl Into<String>, json: impl Into<serde_json::Value>) -> StructuredOutput {
StructuredOutput {
lines: lines.into(),
json: json.into(),
}
}
pub fn lines(&self) -> &str {
&self.lines
}
pub fn json(&self) -> &serde_json::Value {
&self.json
}
pub fn to_json_string(&self) -> String {
serde_json::to_string(&self.json).unwrap()
}
pub fn to_json_pretty(&self) -> String {
serde_json::to_string_pretty(&self.json).unwrap()
}
}
impl std::fmt::Display for StructuredOutput {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.lines)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let lines = "Hello world!";
let json = serde_json::json!({ "message": lines });
let output = StructuredOutput::new(lines, json.clone());
assert_eq!(output.lines(), "Hello world!");
assert_eq!(output.json(), &json);
}
}