fugue_db/
segment.rs

1use crate::error::Error;
2use crate::schema;
3use crate::Id;
4
5use fugue_bytes::Endian;
6
7#[derive(
8    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize,
9)]
10pub struct Segment {
11    id: Id<Self>,
12    name: String,
13    address: u64,
14    length: usize,
15    alignment: usize,
16    address_size: usize,
17    endian: Endian,
18    bits: usize,
19    code: bool,
20    data: bool,
21    external: bool,
22    executable: bool,
23    readable: bool,
24    writable: bool,
25    bytes: Vec<u8>,
26}
27
28impl Segment {
29    pub fn id(&self) -> Id<Self> {
30        self.id.clone()
31    }
32
33    pub fn name(&self) -> &str {
34        &self.name
35    }
36
37    pub fn address(&self) -> u64 {
38        self.address
39    }
40
41    pub fn len(&self) -> usize {
42        self.length
43    }
44
45    pub fn alignment(&self) -> usize {
46        self.alignment
47    }
48
49    pub fn address_size(&self) -> usize {
50        self.address_size
51    }
52
53    pub fn endian(&self) -> Endian {
54        self.endian
55    }
56
57    pub fn bits(&self) -> usize {
58        self.bits
59    }
60
61    pub fn is_code(&self) -> bool {
62        self.code
63    }
64
65    pub fn is_data(&self) -> bool {
66        self.data
67    }
68
69    pub fn is_external(&self) -> bool {
70        self.external
71    }
72
73    pub fn is_executable(&self) -> bool {
74        self.executable
75    }
76
77    pub fn is_readable(&self) -> bool {
78        self.readable
79    }
80
81    pub fn is_writable(&self) -> bool {
82        self.writable
83    }
84
85    pub fn bytes(&self) -> &[u8] {
86        &self.bytes
87    }
88
89    pub(crate) fn from_reader(id: Id<Self>, reader: &schema::Segment) -> Result<Self, Error> {
90        Ok(Self {
91            id,
92            name: reader
93                .name()
94                .ok_or(Error::DeserialiseField("name"))?
95                .to_string(),
96            address: reader.address(),
97            length: reader.size_() as usize,
98            alignment: reader.alignment_() as usize,
99            endian: if reader.endian() {
100                Endian::Big
101            } else {
102                Endian::Little
103            },
104            bits: reader.bits() as usize,
105            address_size: reader.address_size() as usize,
106            code: reader.code(),
107            data: reader.data(),
108            external: reader.external(),
109            executable: reader.executable(),
110            readable: reader.readable(),
111            writable: reader.writable(),
112            bytes: reader
113                .bytes()
114                .ok_or(Error::DeserialiseField("bytes"))?
115                .bytes()
116                .to_vec(),
117        })
118    }
119
120    pub(crate) fn to_builder<'a: 'b, 'b>(
121        &self,
122        builder: &'b mut flatbuffers::FlatBufferBuilder<'a>,
123    ) -> Result<flatbuffers::WIPOffset<schema::Segment<'a>>, Error> {
124        let name = builder.create_string(self.name());
125        let bytes = builder.create_vector(&self.bytes);
126
127        let mut sbuilder = schema::SegmentBuilder::new(builder);
128
129        sbuilder.add_name(name);
130        sbuilder.add_address(self.address());
131        sbuilder.add_size_(self.len() as u32);
132        sbuilder.add_alignment_(self.alignment() as u32);
133        sbuilder.add_address_size(self.address_size() as u32);
134        sbuilder.add_endian(self.endian.is_big());
135        sbuilder.add_bits(self.bits as u32);
136        sbuilder.add_code(self.is_code());
137        sbuilder.add_data(self.is_data());
138        sbuilder.add_external(self.is_external());
139        sbuilder.add_executable(self.is_executable());
140        sbuilder.add_readable(self.is_readable());
141        sbuilder.add_writable(self.is_writable());
142        sbuilder.add_bytes(bytes);
143
144        Ok(sbuilder.finish())
145    }
146}