mirror of
https://github.com/denoland/deno.git
synced 2024-12-11 18:17:48 -05:00
1d774683b2
To align with conventions used in our other crates
45 lines
918 B
Rust
45 lines
918 B
Rust
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
|
|
}
|
|
}
|
|
|
|
impl StringOrBuffer {
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
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 {
|
|
#[serde(with = "serde_bytes")]
|
|
Buffer(Vec<u8>),
|
|
String(String),
|
|
}
|
|
|
|
impl StringOrBufferInner {
|
|
fn into_bytes(self) -> Vec<u8> {
|
|
match self {
|
|
Self::String(s) => s.into_bytes(),
|
|
Self::Buffer(b) => b,
|
|
}
|
|
}
|
|
}
|