fuels_core/
lib.rs

1pub mod codec;
2pub mod traits;
3pub mod types;
4mod utils;
5
6pub use utils::*;
7
8use crate::types::errors::Result;
9
10#[derive(Debug, Clone, Default, PartialEq)]
11pub struct Configurables {
12    offsets_with_data: Vec<(u64, Vec<u8>)>,
13}
14
15impl Configurables {
16    pub fn new(offsets_with_data: Vec<(u64, Vec<u8>)>) -> Self {
17        Self { offsets_with_data }
18    }
19
20    pub fn with_shifted_offsets(self, shift: i64) -> Result<Self> {
21        let new_offsets_with_data = self
22            .offsets_with_data
23            .into_iter()
24            .map(|(offset, data)| {
25                let new_offset = if shift.is_negative() {
26                    offset.checked_sub(shift.unsigned_abs())
27                } else {
28                    offset.checked_add(shift.unsigned_abs())
29                };
30
31                let new_offset = new_offset.ok_or_else(|| {
32                    crate::error!(
33                        Other,
34                        "Overflow occurred while shifting offset: {offset} + {shift}"
35                    )
36                })?;
37
38                Ok((new_offset, data.clone()))
39            })
40            .collect::<Result<Vec<_>>>()?;
41
42        Ok(Self {
43            offsets_with_data: new_offsets_with_data,
44        })
45    }
46
47    pub fn update_constants_in(&self, binary: &mut [u8]) {
48        for (offset, data) in &self.offsets_with_data {
49            let offset = *offset as usize;
50            binary[offset..offset + data.len()].copy_from_slice(data)
51        }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_with_shifted_offsets_positive_shift() {
61        let offsets_with_data = vec![(10u64, vec![1, 2, 3])];
62        let configurables = Configurables::new(offsets_with_data.clone());
63        let shifted_configurables = configurables.with_shifted_offsets(5).unwrap();
64        let expected_offsets_with_data = vec![(15u64, vec![1, 2, 3])];
65        assert_eq!(
66            shifted_configurables.offsets_with_data,
67            expected_offsets_with_data
68        );
69    }
70
71    #[test]
72    fn test_with_shifted_offsets_negative_shift() {
73        let offsets_with_data = vec![(10u64, vec![4, 5, 6])];
74        let configurables = Configurables::new(offsets_with_data.clone());
75        let shifted_configurables = configurables.with_shifted_offsets(-5).unwrap();
76        let expected_offsets_with_data = vec![(5u64, vec![4, 5, 6])];
77        assert_eq!(
78            shifted_configurables.offsets_with_data,
79            expected_offsets_with_data
80        );
81    }
82
83    #[test]
84    fn test_with_shifted_offsets_zero_shift() {
85        let offsets_with_data = vec![(20u64, vec![7, 8, 9])];
86        let configurables = Configurables::new(offsets_with_data.clone());
87        let shifted_configurables = configurables.with_shifted_offsets(0).unwrap();
88        let expected_offsets_with_data = offsets_with_data;
89        assert_eq!(
90            shifted_configurables.offsets_with_data,
91            expected_offsets_with_data
92        );
93    }
94
95    #[test]
96    fn test_with_shifted_offsets_overflow() {
97        let offsets_with_data = vec![(u64::MAX - 1, vec![1, 2, 3])];
98        let configurables = Configurables::new(offsets_with_data);
99        let result = configurables.with_shifted_offsets(10);
100        assert!(result.is_err());
101        if let Err(e) = result {
102            assert!(e
103                .to_string()
104                .contains("Overflow occurred while shifting offset"));
105        }
106    }
107
108    #[test]
109    fn test_with_shifted_offsets_underflow() {
110        let offsets_with_data = vec![(5, vec![4, 5, 6])];
111        let configurables = Configurables::new(offsets_with_data);
112        let result = configurables.with_shifted_offsets(-10);
113        assert!(result.is_err());
114        if let Err(e) = result {
115            assert!(e
116                .to_string()
117                .contains("Overflow occurred while shifting offset"));
118        }
119    }
120}