Skip to main content

parse_bulk

Function parse_bulk 

Source
pub fn parse_bulk(body: &[u8]) -> Result<Vec<BulkItem>, RewriteError>
Expand description

Parses an NDJSON _bulk body into its ordered operations.

§Errors

Returns RewriteError::InvalidJson if an action or source line is not valid JSON, or RewriteError::MalformedBulkAction if an action line is not a single-key {verb: {…}} object, names an unknown verb, or a source line is missing for an action that requires one.

§Examples

use osproxy_rewrite::{parse_bulk, BulkAction};

let body = b"{\"index\":{\"_id\":\"1\"}}\n{\"msg\":\"hi\"}\n";
let items = parse_bulk(body).unwrap();
assert_eq!(items.len(), 1);
assert_eq!(items[0].action, BulkAction::Index);
assert_eq!(items[0].id.as_deref(), Some("1"));
assert_eq!(items[0].source.as_deref(), Some(&b"{\"msg\":\"hi\"}"[..]));