diff options
| author | Matt Mastracci <matthew@mastracci.com> | 2023-07-01 18:00:14 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-02 00:00:14 +0000 |
| commit | e746b6d80654ba4e4e26370fe6e4f784ce841d92 (patch) | |
| tree | 153ffad92a96126b9ab8e906dcdabf7648755931 /serde_v8/magic/string_or_buffer.rs | |
| parent | b9c0e7cd550ab14fa7da7e33ed87cbeeeb9785a0 (diff) | |
refactor(core): Extract deno_core (#19658)
`deno_core` is moving out! You'll find it at
https://github.com/denoland/deno_core/ once this PR lands.
Diffstat (limited to 'serde_v8/magic/string_or_buffer.rs')
| -rw-r--r-- | serde_v8/magic/string_or_buffer.rs | 58 |
1 files changed, 0 insertions, 58 deletions
diff --git a/serde_v8/magic/string_or_buffer.rs b/serde_v8/magic/string_or_buffer.rs deleted file mode 100644 index 986f7d32a..000000000 --- a/serde_v8/magic/string_or_buffer.rs +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -use super::buffer::JsBuffer; -use super::transl8::FromV8; -use crate::error::value_to_type_str; -use crate::magic::transl8::impl_magic; -use crate::Error; -use std::ops::Deref; - -#[derive(Debug)] -pub enum StringOrBuffer { - Buffer(JsBuffer), - String(String), -} - -impl_magic!(StringOrBuffer); - -impl Deref for StringOrBuffer { - type Target = [u8]; - fn deref(&self) -> &Self::Target { - match self { - Self::Buffer(b) => b.as_ref(), - Self::String(s) => s.as_bytes(), - } - } -} - -impl<'a> TryFrom<&'a StringOrBuffer> for &'a str { - type Error = std::str::Utf8Error; - fn try_from(value: &'a StringOrBuffer) -> Result<Self, Self::Error> { - match value { - StringOrBuffer::String(s) => Ok(s.as_str()), - StringOrBuffer::Buffer(b) => std::str::from_utf8(b.as_ref()), - } - } -} - -impl FromV8 for StringOrBuffer { - fn from_v8( - scope: &mut v8::HandleScope, - value: v8::Local<v8::Value>, - ) -> Result<Self, crate::Error> { - if let Ok(buf) = JsBuffer::from_v8(scope, value) { - return Ok(Self::Buffer(buf)); - } else if let Ok(s) = crate::from_v8(scope, value) { - return Ok(Self::String(s)); - } - Err(Error::ExpectedBuffer(value_to_type_str(value))) - } -} - -impl From<StringOrBuffer> for bytes::Bytes { - fn from(sob: StringOrBuffer) -> Self { - match sob { - StringOrBuffer::Buffer(b) => b.into(), - StringOrBuffer::String(s) => s.into_bytes().into(), - } - } -} |
