1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Helpers to write messages.

use flatbuffers::{FlatBufferBuilder, WIPOffset};
use std::io::Write;
use serde::{Deserialize, Serialize};
use crate::zkinterface_generated::zkinterface::{
    Command,
    CommandArgs,
    Message,
    Root,
    RootArgs,
};
use crate::Result;


#[derive(Clone, Default, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct CommandOwned {
    pub constraints_generation: bool,
    pub witness_generation: bool,
    //pub parameters: Option<Vec<KeyValue>>,
}

impl<'a> From<Command<'a>> for CommandOwned {
    /// Convert from Flatbuffers references to owned structure.
    fn from(command_ref: Command) -> CommandOwned {
        CommandOwned {
            constraints_generation: command_ref.constraints_generation(),
            witness_generation: command_ref.witness_generation(),
        }
    }
}

impl CommandOwned {
    /// Add this structure into a Flatbuffers message builder.
    pub fn build<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>(
        &'args self,
        builder: &'mut_bldr mut FlatBufferBuilder<'bldr>,
    ) -> WIPOffset<Root<'bldr>>
    {
        let call = Command::create(builder, &CommandArgs {
            constraints_generation: self.constraints_generation,
            witness_generation: self.witness_generation,
            parameters: None,
        });

        Root::create(builder, &RootArgs {
            message_type: Message::Command,
            message: Some(call.as_union_value()),
        })
    }

    /// Writes this command as a Flatbuffers message into the provided buffer.
    ///
    /// # Examples
    /// ```
    /// let mut buf = Vec::<u8>::new();
    /// let command = zkinterface::CommandOwned::default();
    /// command.write_into(&mut buf).unwrap();
    /// assert!(buf.len() > 0);
    /// ```
    pub fn write_into(&self, writer: &mut impl Write) -> Result<()> {
        let mut builder = FlatBufferBuilder::new();
        let message = self.build(&mut builder);
        builder.finish_size_prefixed(message, None);
        writer.write_all(builder.finished_data())?;
        Ok(())
    }
}