use crate::entity::EntityId;
use crate::ops::{BatchEntry, BatchError, BatchResult, WarningHint};
use super::super::Engine;
use super::super::EngineError;
pub(crate) fn batch_empty() -> BatchResult {
BatchResult {
warnings: Vec::new(),
orphan_stubs_removed: Vec::new(),
errors_suppressed: 0,
applied: true,
results: Vec::new(),
succeeded: 0,
failed: 0,
write_id: String::new(),
}
}
pub(crate) fn batch_refusal(
ids_in_order: Vec<EntityId>,
errors: Vec<(usize, EngineError)>,
) -> BatchResult {
let failed = errors.len();
let mut error_map: std::collections::HashMap<usize, EngineError> = errors.into_iter().collect();
let mut reported = 0usize;
let mut suppressed = 0usize;
let results: Vec<BatchEntry> = ids_in_order
.into_iter()
.enumerate()
.map(|(i, id)| match error_map.remove(&i) {
Some(e) => {
if reported < Engine::BATCH_ERROR_REPORT_CAP {
reported += 1;
BatchEntry {
id,
action: "error".to_string(),
error: Some(batch_error_envelope(&e)),
}
} else {
suppressed += 1;
BatchEntry {
id,
action: "error".to_string(),
error: None,
}
}
}
None => BatchEntry {
id,
action: "not_applied".to_string(),
error: None,
},
})
.collect();
BatchResult {
warnings: Vec::new(),
orphan_stubs_removed: Vec::new(),
errors_suppressed: suppressed,
applied: false,
results,
succeeded: 0,
failed,
write_id: String::new(),
}
}
pub(crate) fn batch_receipt(
actions: Vec<(EntityId, String)>,
warnings: Vec<WarningHint>,
orphan_stubs_removed: Vec<EntityId>,
write_id: String,
) -> BatchResult {
let succeeded = actions.len();
let results: Vec<BatchEntry> = actions
.into_iter()
.map(|(id, action)| BatchEntry {
id,
action,
error: None,
})
.collect();
BatchResult {
warnings,
orphan_stubs_removed,
errors_suppressed: 0,
applied: true,
results,
succeeded,
failed: 0,
write_id,
}
}
pub(crate) fn batch_error_envelope(err: &EngineError) -> BatchError {
let code = err.code().to_string();
let message = err.to_string();
let details = err.details();
BatchError {
code,
message,
details,
}
}