Skip to main content

vest_lib/combinators/cond/
exec.rs

1//! Executable implementation for boolean-gated formats.
2use crate::core::exec::output::*;
3use crate::core::{
4    exec::{
5        parser::{PResult, Parser},
6        serializer::{ByteLen, ComplianceErrorKind, PreSerializeError, Prepare, Serializer},
7        ParseError,
8    },
9    spec::SpecParser,
10};
11use vstd::prelude::*;
12use OutputBuf;
13
14verus! {
15
16impl<I, Inner> Parser<I> for super::Cond<Inner> where I: View<V = Seq<u8>>, Inner: Parser<I> {
17    type PT = Inner::PT;
18
19    open spec fn exec_inv(&self) -> bool {
20        self.1.exec_inv()
21    }
22
23    fn parse(&self, ibuf: &I) -> PResult<Self::PT> {
24        if self.0 {
25            self.1.parse(ibuf)
26        } else {
27            Err(ParseError::cond_rejected())
28        }
29    }
30}
31
32impl<Output: OutputBuf, Inner, T> Serializer<Output, T> for super::Cond<Inner> where
33    T: DeepView,
34    Inner: Serializer<Output, T>,
35 {
36    #[verifier::prophetic]
37    open spec fn exec_inv(&self) -> bool {
38        self.1.exec_inv()
39    }
40
41    fn serialize_into(&self, v: &T, obuf: &mut Output) {
42        self.1.serialize_into(v, obuf);
43    }
44}
45
46impl<T, Inner> ByteLen<T> for super::Cond<Inner> where T: DeepView, Inner: ByteLen<T> {
47    open spec fn exec_inv(&self) -> bool {
48        self.1.exec_inv()
49    }
50
51    fn length(&self, v: &T) -> (len: usize) {
52        self.1.length(v)
53    }
54}
55
56impl<T, Inner> Prepare<T> for super::Cond<Inner> where T: DeepView, Inner: Prepare<T> {
57    open spec fn exec_inv(&self) -> bool {
58        self.1.exec_inv()
59    }
60
61    fn prepare(&self, v: &T) -> (checked: Result<usize, PreSerializeError>) {
62        if self.0 {
63            self.1.prepare(v)
64        } else {
65            Err(PreSerializeError::not_compliant(ComplianceErrorKind::CondRejected))
66        }
67    }
68}
69
70} // verus!