forest/lotus_json/
vec_u8.rs

1// Copyright 2019-2025 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use super::*;
5
6// This code looks odd so we can
7// - use #[serde(with = "...")]
8// - de/ser empty vecs as null
9#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
10pub struct VecU8LotusJson(Option<Inner>);
11
12impl JsonSchema for VecU8LotusJson {
13    fn schema_name() -> std::borrow::Cow<'static, str> {
14        "Base64String".into()
15    }
16
17    fn json_schema(_: &mut schemars::SchemaGenerator) -> Schema {
18        schemars::json_schema!({
19            "type": ["string", "null"]
20        })
21    }
22}
23
24#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
25struct Inner(#[serde(with = "base64_standard")] Vec<u8>);
26
27impl HasLotusJson for Vec<u8> {
28    type LotusJson = VecU8LotusJson;
29
30    #[cfg(test)]
31    fn snapshots() -> Vec<(serde_json::Value, Self)> {
32        vec![
33            (json!("aGVsbG8gd29ybGQh"), Vec::from_iter(*b"hello world!")),
34            (json!(null), Vec::new()),
35        ]
36    }
37
38    fn into_lotus_json(self) -> Self::LotusJson {
39        match self.is_empty() {
40            true => VecU8LotusJson(None),
41            false => VecU8LotusJson(Some(Inner(self))),
42        }
43    }
44
45    fn from_lotus_json(value: Self::LotusJson) -> Self {
46        match value {
47            VecU8LotusJson(Some(Inner(vec))) => vec,
48            VecU8LotusJson(None) => Vec::new(),
49        }
50    }
51}