ip2location_bin_format/index/querier/
builder.rs

1use crate::index::INDEX_LEN;
2
3//
4pub struct Builder {
5    bytes: Vec<u8>,
6}
7
8impl Default for Builder {
9    fn default() -> Self {
10        Self::new()
11    }
12}
13
14impl Builder {
15    pub fn new() -> Self {
16        Self {
17            bytes: Vec::with_capacity(INDEX_LEN as usize),
18        }
19    }
20
21    pub fn append(&mut self, slice: &[u8]) {
22        self.bytes.extend_from_slice(slice);
23    }
24
25    pub fn finish<T>(self) -> Result<T, BuildError>
26    where
27        T: From<Vec<u8>>,
28    {
29        if self.bytes.len() != INDEX_LEN as usize {
30            return Err(BuildError::LenMismatch);
31        }
32
33        Ok(T::from(self.bytes))
34    }
35}
36
37//
38#[derive(Debug)]
39pub enum BuildError {
40    LenMismatch,
41}
42
43impl core::fmt::Display for BuildError {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        write!(f, "{self:?}")
46    }
47}
48
49impl std::error::Error for BuildError {}