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
use hotg_rune_runtime::{Capability, ParameterError};
use crate::run::multi::{Builder, SourceBackedCapability};

#[derive(Debug, Clone, PartialEq)]
pub struct Raw {
    bytes: Vec<u8>,
}

impl Raw {
    pub const fn new(bytes: Vec<u8>) -> Self { Raw { bytes } }
}

impl Capability for Raw {
    fn generate(&mut self, buffer: &mut [u8]) -> Result<usize, anyhow::Error> {
        let len = std::cmp::min(buffer.len(), self.bytes.len());
        buffer[..len].copy_from_slice(&self.bytes[..len]);
        Ok(len)
    }

    fn set_parameter(
        &mut self,
        _name: &str,
        _value: hotg_rune_core::Value,
    ) -> Result<(), ParameterError> {
        Ok(())
    }
}

impl SourceBackedCapability for Raw {
    type Builder = NullBuilder;
    type Source = Vec<u8>;

    fn generate(&mut self, buffer: &mut [u8]) -> Result<usize, anyhow::Error> {
        let len = std::cmp::min(buffer.len(), self.bytes.len());
        buffer[..len].copy_from_slice(&self.bytes[..len]);
        Ok(len)
    }

    fn from_builder(
        _builder: NullBuilder,
        source: &Vec<u8>,
    ) -> Result<Self, anyhow::Error> {
        Ok(Raw::new(source.clone()))
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct NullBuilder;

impl Builder for NullBuilder {
    fn set_parameter(
        &mut self,
        _key: &str,
        _value: hotg_rune_core::Value,
    ) -> Result<(), ParameterError> {
        Err(ParameterError::UnsupportedParameter)
    }
}