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
#![allow(clippy::field_reassign_with_default)]
use alloc::{string::String, vec::Vec};
#[cfg(feature = "json-schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::bytesrepr::{self, FromBytes, ToBytes};
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Default, Debug)]
#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct NamedKey {
pub name: String,
pub key: String,
}
impl ToBytes for NamedKey {
fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
let mut buffer = bytesrepr::allocate_buffer(self)?;
buffer.extend(self.name.to_bytes()?);
buffer.extend(self.key.to_bytes()?);
Ok(buffer)
}
fn serialized_length(&self) -> usize {
self.name.serialized_length() + self.key.serialized_length()
}
}
impl FromBytes for NamedKey {
fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
let (name, remainder) = String::from_bytes(bytes)?;
let (key, remainder) = String::from_bytes(remainder)?;
let named_key = NamedKey { name, key };
Ok((named_key, remainder))
}
}