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    fn min_byte_len(&self) -> usize {
20        0
21    }
22
23    open spec fn exec_inv(&self) -> bool {
24        self.1.exec_inv()
25    }
26
27    fn parse(&self, ibuf: &I) -> PResult<Self::PT> {
28        if self.0 {
29            self.1.parse(ibuf)
30        } else {
31            Err(ParseError::cond_rejected())
32        }
33    }
34}
35
36impl<Output: OutputBuf, Inner, T> Serializer<Output, T> for super::Cond<Inner> where
37    T: DeepView,
38    Inner: Serializer<Output, T>,
39 {
40    #[verifier::prophetic]
41    open spec fn exec_inv(&self) -> bool {
42        self.1.exec_inv()
43    }
44
45    fn serialize_into(&self, v: &T, obuf: &mut Output) {
46        self.1.serialize_into(v, obuf);
47    }
48}
49
50impl<T, Inner> ByteLen<T> for super::Cond<Inner> where T: DeepView, Inner: ByteLen<T> {
51    open spec fn exec_inv(&self) -> bool {
52        self.1.exec_inv()
53    }
54
55    fn length(&self, v: &T) -> (len: usize) {
56        self.1.length(v)
57    }
58}
59
60impl<T, Inner> Prepare<T> for super::Cond<Inner> where T: DeepView, Inner: Prepare<T> {
61    open spec fn exec_inv(&self) -> bool {
62        self.1.exec_inv()
63    }
64
65    fn prepare(&self, v: &T) -> (checked: Result<usize, PreSerializeError>) {
66        if self.0 {
67            self.1.prepare(v)
68        } else {
69            Err(PreSerializeError::not_compliant(ComplianceErrorKind::CondRejected))
70        }
71    }
72}
73
74} // verus!