use super::invoke::{Identity, Invoke};
use crate::elements;
use alloc::vec::Vec;
pub struct DataSegmentBuilder<F = Identity> {
callback: F,
mem_index: u32,
offset: elements::InitExpr,
value: Vec<u8>,
}
impl DataSegmentBuilder {
pub fn new() -> Self {
DataSegmentBuilder::with_callback(Identity)
}
}
impl Default for DataSegmentBuilder {
fn default() -> Self {
Self::new()
}
}
impl<F> DataSegmentBuilder<F> {
pub fn with_callback(callback: F) -> Self {
DataSegmentBuilder {
callback,
mem_index: 0,
offset: elements::InitExpr::empty(),
value: Vec::new(),
}
}
pub fn offset(mut self, instruction: elements::Instruction) -> Self {
self.offset = elements::InitExpr::new(vec![instruction, elements::Instruction::End]);
self
}
pub fn value(mut self, value: Vec<u8>) -> Self {
self.value = value;
self
}
}
impl<F> DataSegmentBuilder<F>
where
F: Invoke<elements::DataSegment>,
{
pub fn build(self) -> F::Result {
self.callback.invoke(elements::DataSegment::new(
self.mem_index,
Some(self.offset),
self.value,
))
}
}