etcd_txn_parser/
lib.rs

1#![doc = include_str!("../Readme.md")]
2use crate::compare::Compare;
3use crate::operation::Operation;
4use elyze::bytes::matchers::match_pattern;
5use elyze::bytes::primitives::whitespace::OptionalWhitespaces;
6use elyze::bytes::token::Token;
7use elyze::errors::{ParseError, ParseResult};
8use elyze::matcher::Match;
9use elyze::peek::{peek, Until, UntilEnd};
10use elyze::recognizer::recognize;
11use elyze::scanner::Scanner;
12use elyze::separated_list::SeparatedList;
13use elyze::visitor::Visitor;
14
15pub mod compare;
16pub mod operation;
17
18/// Parse a transactional data structure from a byte slice.
19///
20/// # Errors
21///
22/// If the parser encounters an unexpected token, a `ParseError` is returned.
23///
24/// # Examples
25///
26///
27pub fn parse(data: &[u8]) -> ParseResult<TxnData> {
28    TxnData::accept(&mut Scanner::new(data))
29}
30
31/// A transactional data structure.
32#[derive(Debug, PartialEq)]
33pub struct TxnData<'a> {
34    /// A list of operations to compare against the current state.
35    pub compares: Vec<Compare<'a>>,
36    /// A list of operations to apply if the compare operations pass.
37    pub success: Vec<Operation<'a>>,
38    /// A list of operations to apply if the compare operations fail.
39    pub failure: Vec<Operation<'a>>,
40}
41
42struct LineFeed;
43
44impl<'a> Visitor<'a, u8> for LineFeed {
45    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
46        recognize(Token::Ln, scanner)?;
47        Ok(LineFeed)
48    }
49}
50
51#[derive(Clone, Default)]
52struct SectionEnd;
53
54impl Match<u8> for SectionEnd {
55    fn is_matching(&self, data: &[u8]) -> (bool, usize) {
56        match_pattern(b"\n\n", data)
57    }
58
59    fn size(&self) -> usize {
60        2
61    }
62}
63
64impl<'a> Visitor<'a, u8> for TxnData<'a> {
65    fn accept(scanner: &mut Scanner<'a, u8>) -> ParseResult<Self> {
66        OptionalWhitespaces::accept(scanner)?;
67
68        // Read the compare section
69        let section_compare =
70            peek(Until::new(SectionEnd), scanner)?.ok_or(ParseError::UnexpectedToken)?;
71        let mut section_compare_scanner = Scanner::new(section_compare.peeked_slice());
72        let compares =
73            SeparatedList::<u8, Compare, LineFeed>::accept(&mut section_compare_scanner)?.data;
74        scanner.bump_by(section_compare.end_slice);
75
76        // Read the success section
77        let section_success =
78            peek(Until::new(SectionEnd), scanner)?.ok_or(ParseError::UnexpectedToken)?;
79
80        let mut section_success_scanner = Scanner::new(section_success.peeked_slice());
81        let success =
82            SeparatedList::<u8, Operation, LineFeed>::accept(&mut section_success_scanner)?.data;
83        scanner.bump_by(section_success.end_slice);
84
85        // Read the failure section
86        let section_failure =
87            peek(UntilEnd::default(), scanner)?.ok_or(ParseError::UnexpectedToken)?;
88
89        let mut section_failure_scanner = Scanner::new(section_failure.peeked_slice());
90        let failure =
91            SeparatedList::<u8, Operation, LineFeed>::accept(&mut section_failure_scanner)?.data;
92
93        scanner.bump_by(section_failure.end_slice);
94
95        Ok(TxnData {
96            compares,
97            success,
98            failure,
99        })
100    }
101}