-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathmetadata.rs
More file actions
110 lines (89 loc) · 2.47 KB
/
metadata.rs
File metadata and controls
110 lines (89 loc) · 2.47 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Debug;
use std::fmt::Formatter;
use std::ops::Deref;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
/// Trait for serializing Vortex metadata to a vector of unaligned bytes.
pub trait SerializeMetadata {
fn serialize(self) -> Vec<u8>;
}
/// Trait for deserializing Vortex metadata from a vector of unaligned bytes.
pub trait DeserializeMetadata
where
Self: Sized,
{
/// The fully deserialized type of the metadata.
type Output;
/// Deserialize metadata from a vector of unaligned bytes.
fn deserialize(metadata: &[u8]) -> VortexResult<Self::Output>;
}
/// Empty array metadata
#[derive(Debug)]
pub struct EmptyMetadata;
impl SerializeMetadata for EmptyMetadata {
fn serialize(self) -> Vec<u8> {
vec![]
}
}
impl DeserializeMetadata for EmptyMetadata {
type Output = EmptyMetadata;
fn deserialize(metadata: &[u8]) -> VortexResult<Self::Output> {
if !metadata.is_empty() {
vortex_bail!("EmptyMetadata should not have metadata bytes")
}
Ok(EmptyMetadata)
}
}
/// A utility wrapper for raw metadata serialization. This delegates the serialiation step
/// to the arrays' vtable.
pub struct RawMetadata(pub Vec<u8>);
impl SerializeMetadata for RawMetadata {
fn serialize(self) -> Vec<u8> {
self.0
}
}
impl DeserializeMetadata for RawMetadata {
type Output = Vec<u8>;
fn deserialize(metadata: &[u8]) -> VortexResult<Self::Output> {
Ok(metadata.to_vec())
}
}
impl Debug for RawMetadata {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "\"{}\"", self.0.escape_ascii())
}
}
/// A utility wrapper for Prost metadata serialization.
pub struct ProstMetadata<M>(pub M);
impl<M> Deref for ProstMetadata<M> {
type Target = M;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<M: Debug> Debug for ProstMetadata<M> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<M> SerializeMetadata for ProstMetadata<M>
where
M: prost::Message,
{
fn serialize(self) -> Vec<u8> {
self.0.encode_to_vec()
}
}
impl<M> DeserializeMetadata for ProstMetadata<M>
where
M: Debug,
M: prost::Message + Default,
{
type Output = M;
fn deserialize(metadata: &[u8]) -> VortexResult<Self::Output> {
Ok(M::decode(metadata)?)
}
}