1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#[cfg(any(feature = "std", test))]
use displaydoc::Display;
#[cfg(any(feature = "std", test))]
use std::prelude::v1::*;
#[cfg(any(feature = "std", test))]
use thiserror::Error;

/// Error while parsing a memory layout.
#[cfg(any(feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[derive(Debug, Display, Error)]
pub enum Error {
    /// invalid page format: {0}
    InvalidPageFormat(String),
    /// could not parse page count: {0}
    ParseErrorPageCount(String),
    /// could not parse page size: {0}
    ParseErrorPageSize(String),
    /// invalid prefix: {0}
    InvalidPrefix(String),
}

/// A memory page size.
pub type MemoryPage = u32;

/// A slice of memory pages.
#[allow(non_camel_case_types)]
pub type mem = [MemoryPage];

/// Memory layout.
#[cfg(any(feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub struct MemoryLayout(Vec<MemoryPage>);

#[cfg(any(feature = "std", test))]
impl AsRef<mem> for MemoryLayout {
    fn as_ref(&self) -> &mem {
        self.0.as_slice()
    }
}

#[cfg(any(feature = "std", test))]
impl MemoryLayout {
    /// Create a new empty instance of [`MemoryLayout`].
    pub fn new() -> Self {
        Self(Vec::new())
    }
}

#[cfg(any(feature = "std", test))]
impl Default for MemoryLayout {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(any(feature = "std", test))]
impl From<Vec<MemoryPage>> for MemoryLayout {
    fn from(vec: Vec<MemoryPage>) -> Self {
        Self(vec)
    }
}

#[cfg(any(feature = "std", test))]
impl core::ops::Deref for MemoryLayout {
    type Target = Vec<MemoryPage>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(any(feature = "std", test))]
impl core::ops::DerefMut for MemoryLayout {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[cfg(any(feature = "std", test))]
impl core::convert::TryFrom<&str> for MemoryLayout {
    type Error = Error;

    fn try_from(src: &str) -> Result<Self, Self::Error> {
        use core::str::FromStr;

        let mut pages = Vec::new();

        for s in src.split(',') {
            let (count, size) = s
                .split_once('*')
                .ok_or_else(|| Error::InvalidPageFormat(s.into()))?;
            let (size, prefix) = size.split_at(
                size.len()
                    .checked_sub(2)
                    .ok_or_else(|| Error::ParseErrorPageSize(size.into()))?,
            );

            let count =
                u32::from_str(count).map_err(|_| Error::ParseErrorPageCount(count.into()))?;
            let size = u32::from_str(size).map_err(|_| Error::ParseErrorPageSize(size.into()))?;
            let prefix = match &prefix[..1] {
                "K" => 1024,
                "M" => 1024 * 1024,
                " " => 1,
                other => return Err(Error::InvalidPrefix(other.into())),
            };

            let size = size * prefix;
            for _ in 0..count {
                pages.push(size);
            }
        }

        Ok(Self(pages))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::convert::TryFrom;

    #[test]
    fn parsing() {
        let s = "04*032Kg,01*128Kg,07*256Kg";
        let m = MemoryLayout::try_from(s).unwrap();
        assert_eq!(
            m.as_slice(),
            &[
                32768, 32768, 32768, 32768, 131072, 262144, 262144, 262144, 262144, 262144, 262144,
                262144
            ]
        );
    }

    #[test]
    fn parsing_stm32_defuse_extensions() {
        let s = "4*32Kg,1*128Kg";
        let m = MemoryLayout::try_from(s).unwrap();
        assert_eq!(m.as_slice(), &[32768, 32768, 32768, 32768, 131072]);
    }
}