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
use super::flash_properties::FlashProperties;
use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct RawFlashAlgorithm {
pub name: String,
pub description: String,
pub default: bool,
#[serde(deserialize_with = "deserialize")]
#[serde(serialize_with = "serialize")]
pub instructions: Vec<u8>,
pub load_address: Option<u64>,
pub pc_init: Option<u64>,
pub pc_uninit: Option<u64>,
pub pc_program_page: u64,
pub pc_erase_sector: u64,
pub pc_erase_all: Option<u64>,
pub data_section_offset: u64,
pub flash_properties: FlashProperties,
pub cores: Vec<String>,
}
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&base64::encode(bytes))
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Base64Visitor;
impl<'de> serde::de::Visitor<'de> for Base64Visitor {
type Value = Vec<u8>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "base64 ASCII text")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
base64::decode(v).map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_str(Base64Visitor)
}