bnr_xfs/cash_unit/logical_cash_unit/
list.rs

1use std::{cmp, fmt};
2
3use crate::{impl_xfs_array, impl_xfs_struct, MaxSize, Size};
4
5use super::*;
6
7/// Maximum number of [LogicalCashUnit]s that can be present on a device.
8pub const LCU_LIST_LEN: usize = 83;
9
10#[repr(C)]
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct LogicalCashUnitItems {
13    size: Size,
14    items: [LogicalCashUnit; LCU_LIST_LEN],
15}
16
17impl LogicalCashUnitItems {
18    /// Creates a new [LogicalCashUnitItems] list.
19    pub const fn new() -> Self {
20        Self {
21            size: Size::new(),
22            items: [LogicalCashUnit::new(); LCU_LIST_LEN],
23        }
24    }
25
26    /// Creates a new [LogicalCashUnitItems] list from the provided parameter.
27    pub fn create(items: [LogicalCashUnit; LCU_LIST_LEN]) -> Self {
28        Self {
29            size: Size::create(LCU_LIST_LEN as u32),
30            items,
31        }
32    }
33
34    /// Gets the max size.
35    pub const fn max_size() -> usize {
36        LCU_LIST_LEN
37    }
38
39    /// Gets the size.
40    pub const fn size(&self) -> u32 {
41        self.size.inner()
42    }
43
44    /// Sets the size.
45    pub fn set_size(&mut self, size: u32) {
46        if size as usize <= Self::max_size() {
47            self.size.set_inner(size);
48        }
49    }
50
51    /// Builder function that sets the size.
52    pub fn with_size(mut self, size: u32) -> Self {
53        self.set_size(size);
54        self
55    }
56
57    /// Gets a list of the [LogicalCashUnit]s.
58    pub fn items(&self) -> &[LogicalCashUnit] {
59        let size = self.size() as usize;
60
61        if (0..self.items.len()).contains(&size) {
62            self.items[..size].as_ref()
63        } else {
64            self.items.as_ref()
65        }
66    }
67
68    /// Gets a mutable list of the [LogicalCashUnit]s.
69    pub fn items_mut(&mut self) -> &mut [LogicalCashUnit] {
70        let size = self.size() as usize;
71
72        if (0..self.items.len()).contains(&size) {
73            self.items[..size].as_mut()
74        } else {
75            self.items.as_mut()
76        }
77    }
78
79    /// Sets a list of the [LogicalCashUnit]s.
80    pub fn set_items(&mut self, items: &[LogicalCashUnit]) {
81        let len = cmp::min(items.len(), LCU_LIST_LEN);
82
83        self.set_size(len as u32);
84        self.items[..len].copy_from_slice(items[..len].as_ref());
85    }
86
87    /// Builder function that sets a list of the [LogicalCashUnit]s.
88    pub fn with_items(mut self, items: &[LogicalCashUnit]) -> Self {
89        self.set_items(items);
90        self
91    }
92}
93
94impl Default for LogicalCashUnitItems {
95    fn default() -> Self {
96        Self::new()
97    }
98}
99
100impl_xfs_array!(LogicalCashUnitItems, "items");
101
102/// Represents a list of [LogicalCashUnit]s.
103#[repr(C)]
104#[derive(Clone, Copy, Debug, PartialEq)]
105pub struct LogicalCashUnitList {
106    max_size: MaxSize,
107    size: Size,
108    items: LogicalCashUnitItems,
109}
110
111impl LogicalCashUnitList {
112    /// Creates a new [LogicalCashUnitList].
113    pub const fn new() -> Self {
114        Self {
115            max_size: MaxSize::create(LCU_LIST_LEN as u32),
116            size: Size::new(),
117            items: LogicalCashUnitItems::new(),
118        }
119    }
120
121    /// Gets the max size of the items list.
122    pub const fn max_size(&self) -> u32 {
123        self.max_size.inner()
124    }
125
126    /// Gets the size of the items list.
127    pub const fn size(&self) -> u32 {
128        self.size.inner()
129    }
130
131    /// Sets the size of the items list.
132    pub fn set_size(&mut self, size: u32) {
133        self.size.set_inner(size);
134        self.items.set_size(size);
135    }
136
137    /// Builder function that sets the size of the items list.
138    pub fn with_size(mut self, size: u32) -> Self {
139        self.set_size(size);
140        self
141    }
142
143    /// Gets a list of the [LogicalCashUnit]s.
144    pub fn items(&self) -> &[LogicalCashUnit] {
145        self.items.items()
146    }
147
148    /// Gets a mutable list of the [LogicalCashUnit]s.
149    pub fn items_mut(&mut self) -> &mut [LogicalCashUnit] {
150        self.items.items_mut()
151    }
152
153    /// Sets a list of [LogicalCashUnit]s.
154    pub fn set_items(&mut self, items: &[LogicalCashUnit]) {
155        self.items.set_items(items);
156        self.size.set_inner(self.items.size());
157    }
158
159    /// Builder function that sets a list of [LogicalCashUnit]s.
160    pub fn with_items(mut self, items: &[LogicalCashUnit]) -> Self {
161        self.set_items(items);
162        self
163    }
164}
165
166impl Default for LogicalCashUnitList {
167    fn default() -> Self {
168        Self::new()
169    }
170}
171
172impl fmt::Display for LogicalCashUnitList {
173    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174        write!(f, "{{")?;
175        write!(f, r#""max_size": {},"#, self.max_size)?;
176        write!(f, r#""size": {},"#, self.size)?;
177
178        write!(f, r#""items": ["#)?;
179        for (i, item) in self.items().iter().enumerate() {
180            if i != 0 {
181                write!(f, ", ")?;
182            }
183
184            write!(f, "{item}")?;
185        }
186        write!(f, "]}}")
187    }
188}
189
190impl_xfs_struct!(LogicalCashUnitList, "logicalCashUnits", [size: Size, items: LogicalCashUnitItems]);