use serde::Serialize;
use crate::convert::directive_to_json;
use crate::types::{DirectiveJson, Error, InputEntry, input_entry_to_directive};
use crate::{API_VERSION, output_json, parse_json_error};
#[derive(Serialize)]
pub struct CreateEntryOutput {
pub api_version: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub entry: Option<DirectiveJson>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub errors: Vec<Error>,
}
#[derive(Serialize)]
pub struct CreateEntriesOutput {
pub api_version: &'static str,
pub entries: Vec<DirectiveJson>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub errors: Vec<Error>,
}
pub fn cmd_create_entry(json_str: &str) -> i32 {
let input_entry: InputEntry = match serde_json::from_str(json_str) {
Ok(e) => e,
Err(e) => {
let output = CreateEntryOutput {
api_version: API_VERSION,
entry: None,
errors: vec![parse_json_error(&e)],
};
return output_json(&output);
}
};
let directive = match input_entry_to_directive(&input_entry) {
Ok(d) => d,
Err(e) => {
let output = CreateEntryOutput {
api_version: API_VERSION,
entry: None,
errors: vec![Error::new(e)],
};
return output_json(&output);
}
};
let entry_json = directive_to_json(&directive, 0, "<created>");
let output = CreateEntryOutput {
api_version: API_VERSION,
entry: Some(entry_json),
errors: vec![],
};
output_json(&output)
}
pub fn cmd_create_entries(json_str: &str) -> i32 {
let input_entries: Vec<InputEntry> = match serde_json::from_str(json_str) {
Ok(e) => e,
Err(e) => {
let output = CreateEntriesOutput {
api_version: API_VERSION,
entries: vec![],
errors: vec![parse_json_error(&e)],
};
return output_json(&output);
}
};
let mut entries = Vec::new();
let mut errors = Vec::new();
for (i, input_entry) in input_entries.iter().enumerate() {
match input_entry_to_directive(input_entry) {
Ok(directive) => {
entries.push(directive_to_json(&directive, i as u32, "<created>"));
}
Err(e) => {
errors.push(Error::new(format!("Entry {i}: {e}")).with_entry_index(i));
}
}
}
let output = CreateEntriesOutput {
api_version: API_VERSION,
entries,
errors,
};
output_json(&output)
}