use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::error::RenderError;
use crate::output::Representation;
pub fn serialize_document<T: Serialize>(
data: &T,
representation: Representation,
) -> Result<String, RenderError> {
match representation {
Representation::Json => {
let mut json = serde_json::to_string_pretty(data)?;
json.push('\n');
Ok(json)
}
Representation::Yaml => {
let mut yaml = serde_yaml::to_string(data)?;
if !yaml.ends_with('\n') {
yaml.push('\n');
}
Ok(yaml)
}
Representation::Csv => crate::util::write_csv(&serde_json::to_value(data)?),
Representation::Ndjson => {
let mut line = serde_json::to_string(data)?;
line.push('\n');
Ok(line)
}
mode => Err(RenderError::OperationError(format!(
"{mode:?} is not a document mode"
))),
}
}
pub fn result_record(data: serde_json::Value) -> serde_json::Value {
serde_json::json!({ "type": "result", "data": data })
}
pub fn result_entry<T: Serialize>(data: &T) -> Result<String, RenderError> {
Ok(serde_json::to_string(&result_record(
serde_json::to_value(data)?,
))?)
}
pub fn serialize_record_array(
records: Vec<serde_json::Value>,
representation: Representation,
) -> Result<String, RenderError> {
serialize_structured(&serde_json::Value::Array(records), representation)
}
pub(crate) fn serialize_structured(
data: &serde_json::Value,
representation: Representation,
) -> Result<String, RenderError> {
match representation {
Representation::Json => Ok(serde_json::to_string_pretty(data)?),
Representation::Yaml => Ok(serde_yaml::to_string(data)?),
Representation::Csv => crate::util::write_csv(data),
Representation::Ndjson => result_entry(data),
mode => Err(RenderError::OperationError(format!(
"{mode:?} is not a structured representation"
))),
}
}
pub fn deserialize_document<T: DeserializeOwned>(
representation: Representation,
text: &str,
) -> Result<T, RenderError> {
match representation {
Representation::Json | Representation::Ndjson => Ok(serde_json::from_str(text)?),
Representation::Yaml => Ok(serde_yaml::from_str(text)?),
Representation::Csv => {
let mut reader = csv::Reader::from_reader(text.as_bytes());
let mut rows = reader.deserialize::<T>();
let row = rows.next().ok_or_else(|| {
RenderError::OperationError("the CSV document has no row".into())
})??;
if rows.next().is_some() {
return Err(RenderError::OperationError(
"the CSV document has more than one row".into(),
));
}
Ok(row)
}
mode => Err(RenderError::OperationError(format!(
"{mode:?} is not a document mode"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Record {
name: String,
count: u32,
note: Option<String>,
}
#[test]
fn each_document_mode_round_trips_a_flat_record() {
let record = Record {
name: "a, \"quoted\"".into(),
count: 2,
note: None,
};
for mode in [
Representation::Json,
Representation::Yaml,
Representation::Csv,
] {
let text = serialize_document(&record, mode).unwrap();
assert!(text.ends_with('\n'), "{mode:?}: {text:?}");
let back: Record = deserialize_document(mode, &text).unwrap();
assert_eq!(back, record, "{mode:?}");
}
assert_eq!(
serialize_document(&record, Representation::Csv).unwrap(),
"name,count,note\n\"a, \"\"quoted\"\"\",2,\n"
);
}
#[test]
fn a_record_array_is_the_line_framed_records_as_one_document() {
let records = vec![
serde_json::json!({"type": "apply_start"}),
result_record(serde_json::json!({"add": 1})),
];
for mode in [Representation::Json, Representation::Yaml] {
let text = serialize_record_array(records.clone(), mode).unwrap();
let back: Vec<serde_json::Value> = deserialize_document(mode, &text).unwrap();
assert_eq!(back, records, "{mode:?}");
}
}
#[test]
fn a_human_mode_is_not_a_document_mode() {
let record = Record {
name: "x".into(),
count: 0,
note: None,
};
assert!(serialize_document(&record, Representation::Human).is_err());
assert!(deserialize_document::<Record>(Representation::Human, "").is_err());
assert!(deserialize_document::<Record>(Representation::Csv, "name,count,note\n").is_err());
}
#[test]
fn a_document_is_exactly_one_value() {
let error =
deserialize_document::<Record>(Representation::Csv, "name,count,note\na,1,\nb,2,\n")
.unwrap_err();
assert!(error.to_string().contains("more than one row"), "{error}");
let json = "{\"name\":\"a\",\"count\":1,\"note\":null}\n";
assert!(
deserialize_document::<Record>(Representation::Json, &format!("{json}{json}")).is_err()
);
let yaml = "name: a\ncount: 1\nnote: null\n";
assert!(deserialize_document::<Record>(
Representation::Yaml,
&format!("{yaml}---\n{yaml}")
)
.is_err());
}
}