1#![deny(clippy::disallowed_methods)]
4
5use crate::ids::UnknownId;
6use crate::native::NativeRecord;
7use base64::{engine::general_purpose::STANDARD, Engine as _};
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use serde_json::{Map, Number, Value};
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
14pub struct NativeUnknownRecord {
15 pub id: UnknownId,
17 #[serde(default, skip_serializing_if = "Vec::is_empty")]
19 pub links: Vec<String>,
20}
21
22impl From<&UnknownRecord> for NativeUnknownRecord {
23 fn from(record: &UnknownRecord) -> Self {
24 Self {
25 id: record.id.clone(),
26 links: record.links.clone(),
27 }
28 }
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
34pub struct UnknownRecord {
35 pub id: UnknownId,
37 pub offset: u64,
39 pub byte_len: u64,
41 pub sha256: String,
43 #[serde(
45 default,
46 skip_serializing_if = "Option::is_none",
47 with = "crate::bytes::option"
48 )]
49 #[schemars(with = "Option<String>")]
50 pub data: Option<Vec<u8>>,
51 #[serde(default, skip_serializing_if = "Vec::is_empty")]
53 pub links: Vec<String>,
54}
55
56impl UnknownRecord {
57 pub(crate) fn into_native_record(self) -> NativeRecord {
58 let mut fields = Map::new();
59 fields.insert("offset".into(), Value::Number(Number::from(self.offset)));
60 fields.insert(
61 "byte_len".into(),
62 Value::Number(Number::from(self.byte_len)),
63 );
64 fields.insert("sha256".into(), Value::String(self.sha256));
65 if let Some(data) = self.data {
66 fields.insert("data".into(), Value::String(STANDARD.encode(data)));
67 }
68 if !self.links.is_empty() {
69 fields.insert(
70 "links".into(),
71 Value::Array(self.links.into_iter().map(Value::String).collect()),
72 );
73 }
74 NativeRecord {
75 id: self.id.0,
76 fields,
77 }
78 }
79}