1
use crate::entity::{EntityRef, Split, Tag, Transaction};
2
use crate::error::Result;
3
use crate::input::InputReader;
4
use crate::output::OutputWriter;
5
use scripting_format::{ContextType, EntityType};
6

            
7
const DEFAULT_OUTPUT_SIZE: u32 = 4096;
8

            
9
pub struct Context {
10
    input: InputReader,
11
    output: OutputWriter,
12
}
13

            
14
impl Context {
15
    pub fn new() -> Result<Self> {
16
        Self::with_output_size(DEFAULT_OUTPUT_SIZE)
17
    }
18

            
19
    pub fn with_output_size(output_size: u32) -> Result<Self> {
20
        let input = InputReader::new()?;
21
        let output = OutputWriter::new(input.entity_count(), output_size);
22
        Ok(Self { input, output })
23
    }
24

            
25
    pub fn context_type(&self) -> Result<ContextType> {
26
        self.input.context_type()
27
    }
28

            
29
    pub fn primary_entity_type(&self) -> Result<EntityType> {
30
        self.input.primary_entity_type()
31
    }
32

            
33
    #[must_use]
34
    pub fn entity_count(&self) -> u32 {
35
        self.input.entity_count()
36
    }
37

            
38
    #[must_use]
39
    pub fn primary_entity_idx(&self) -> u32 {
40
        self.input.primary_entity_idx()
41
    }
42

            
43
    pub fn entity(&self, idx: u32) -> Result<EntityRef<'_>> {
44
        self.input.entity(idx)
45
    }
46

            
47
    pub fn primary(&self) -> Result<EntityRef<'_>> {
48
        self.input.primary()
49
    }
50

            
51
    pub fn primary_transaction(&self) -> Result<Transaction> {
52
        self.input.primary_transaction()
53
    }
54

            
55
    pub fn transaction(&self, idx: u32) -> Result<Transaction> {
56
        self.input.transaction(idx)
57
    }
58

            
59
    pub fn split(&self, idx: u32) -> Result<Split> {
60
        self.input.split(idx)
61
    }
62

            
63
    pub fn tag(&self, idx: u32) -> Result<Tag<'_>> {
64
        self.input.tag(idx)
65
    }
66

            
67
    pub fn splits_for(&self, parent_idx: u32) -> impl Iterator<Item = Result<Split>> + '_ {
68
        self.input.splits_for(parent_idx)
69
    }
70

            
71
    pub fn tags_for(&self, parent_idx: u32) -> impl Iterator<Item = Result<Tag<'_>>> + '_ {
72
        self.input.tags_for(parent_idx)
73
    }
74

            
75
    pub fn create_tag(&mut self, parent_idx: i32, name: &str, value: &str) -> Result<u32> {
76
        self.output.create_tag(parent_idx, name, value)
77
    }
78

            
79
    pub fn tag_primary(&mut self, name: &str, value: &str) -> Result<u32> {
80
        let primary_idx = self.input.primary_entity_idx() as i32;
81
        self.output.create_tag(primary_idx, name, value)
82
    }
83

            
84
    pub fn delete_entity(&mut self, entity_id: [u8; 16], entity_type: EntityType) -> Result<u32> {
85
        self.output.delete_entity(entity_id, entity_type)
86
    }
87

            
88
    pub fn finalize(self) -> Result<()> {
89
        self.output.finalize()
90
    }
91

            
92
    #[must_use]
93
    pub fn output_entity_count(&self) -> u32 {
94
        self.output.entity_count()
95
    }
96
}