2021-10-20 09:40:20 -04:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct StringOrBuffer(Vec<u8>);
|
|
|
|
|
|
|
|
impl Deref for StringOrBuffer {
|
|
|
|
type Target = Vec<u8>;
|
|
|
|
fn deref(&self) -> &Vec<u8> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:00:01 -04:00
|
|
|
impl StringOrBuffer {
|
|
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:40:20 -04:00
|
|
|
impl<'de> serde::Deserialize<'de> for StringOrBuffer {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<StringOrBuffer, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
|
|
|
StringOrBufferInner::deserialize(deserializer)
|
|
|
|
.map(|x| StringOrBuffer(x.into_bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@AaronO): explore if we can make this work with ZeroCopyBuf
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum StringOrBufferInner {
|
2021-11-10 10:48:46 -05:00
|
|
|
#[serde(with = "serde_bytes")]
|
2021-10-20 09:40:20 -04:00
|
|
|
Buffer(Vec<u8>),
|
2021-11-10 10:48:46 -05:00
|
|
|
String(String),
|
2021-10-20 09:40:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StringOrBufferInner {
|
|
|
|
fn into_bytes(self) -> Vec<u8> {
|
|
|
|
match self {
|
|
|
|
Self::String(s) => s.into_bytes(),
|
|
|
|
Self::Buffer(b) => b,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|