|
1 | 1 | // SPDX-License-Identifier: MPL-2.0 |
2 | | -// Copyright ijl (2025-2026) |
| 2 | +// Copyright ijl (2026) |
3 | 3 |
|
4 | 4 | use crate::deserialize::DeserializeError; |
5 | | -use crate::ffi::{PyByteArrayRef, PyBytesRef, PyMemoryViewRef, PyStrRef}; |
| 5 | +#[cfg(all(CPython, not(Py_GIL_DISABLED)))] |
| 6 | +use crate::ffi::{PyByteArrayRef, PyMemoryViewRef}; |
| 7 | +use crate::ffi::{PyBytesRef, PyStrRef}; |
6 | 8 | use crate::util::INVALID_STR; |
7 | 9 | use std::borrow::Cow; |
8 | 10 |
|
9 | | -#[cfg(CPython)] |
| 11 | +#[cfg(all(CPython, not(Py_GIL_DISABLED)))] |
10 | 12 | const INPUT_TYPE_MESSAGE: &str = "Input must be bytes, bytearray, memoryview, or str"; |
11 | 13 |
|
| 14 | +#[cfg(all(CPython, Py_GIL_DISABLED))] |
| 15 | +const INPUT_TYPE_MESSAGE: &str = "Input must be bytes or str"; |
| 16 | + |
12 | 17 | #[cfg(not(CPython))] |
13 | 18 | const INPUT_TYPE_MESSAGE: &str = "Input must be bytes, bytearray, or str"; |
14 | 19 |
|
15 | | -pub(crate) fn read_input_to_buf( |
16 | | - ptr: *mut crate::ffi::PyObject, |
17 | | -) -> Result<&'static str, DeserializeError<'static>> { |
18 | | - let buffer: Option<&'static str>; |
19 | | - if let Ok(ob) = PyBytesRef::from_ptr(ptr) { |
20 | | - buffer = ob.as_str(); |
21 | | - } else if let Ok(ob) = PyStrRef::from_ptr(ptr) { |
22 | | - buffer = ob.as_str(); |
23 | | - } else if let Ok(ob) = PyByteArrayRef::from_ptr(ptr) { |
24 | | - buffer = ob.as_str(); |
25 | | - } else if let Ok(ob) = PyMemoryViewRef::from_ptr(ptr) { |
26 | | - buffer = ob.as_str(); |
27 | | - } else { |
28 | | - return Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))); |
| 20 | +#[cfg_attr(not(Py_GIL_DISABLED), repr(transparent))] |
| 21 | +pub struct Utf8Buffer { |
| 22 | + buffer: &'static str, |
| 23 | +} |
| 24 | + |
| 25 | +impl Utf8Buffer { |
| 26 | + #[cfg(all(CPython, not(Py_GIL_DISABLED)))] |
| 27 | + fn buffer_from_ptr( |
| 28 | + ptr: *mut crate::ffi::PyObject, |
| 29 | + ) -> Result<Option<&'static str>, DeserializeError<'static>> { |
| 30 | + if let Ok(ob) = PyBytesRef::from_ptr(ptr) { |
| 31 | + Ok(ob.as_str()) |
| 32 | + } else if let Ok(ob) = PyStrRef::from_ptr(ptr) { |
| 33 | + Ok(ob.as_str()) |
| 34 | + } else if let Ok(ob) = PyByteArrayRef::from_ptr(ptr) { |
| 35 | + Ok(ob.as_str()) |
| 36 | + } else if let Ok(ob) = PyMemoryViewRef::from_ptr(ptr) { |
| 37 | + Ok(ob.as_str()) |
| 38 | + } else { |
| 39 | + Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + #[cfg(any(not(CPython), Py_GIL_DISABLED))] |
| 44 | + fn buffer_from_ptr( |
| 45 | + ptr: *mut crate::ffi::PyObject, |
| 46 | + ) -> Result<Option<&'static str>, DeserializeError<'static>> { |
| 47 | + if let Ok(ob) = PyBytesRef::from_ptr(ptr) { |
| 48 | + Ok(ob.as_str()) |
| 49 | + } else if let Ok(ob) = PyStrRef::from_ptr(ptr) { |
| 50 | + Ok(ob.as_str()) |
| 51 | + } else { |
| 52 | + Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))) |
| 53 | + } |
29 | 54 | } |
30 | | - match buffer { |
31 | | - Some(as_str) => { |
32 | | - if as_str.is_empty() { |
| 55 | + |
| 56 | + pub fn from_pyobject( |
| 57 | + ptr: *mut crate::ffi::PyObject, |
| 58 | + ) -> Result<Self, DeserializeError<'static>> { |
| 59 | + debug_assert!(!ptr.is_null()); |
| 60 | + match Utf8Buffer::buffer_from_ptr(ptr) { |
| 61 | + Ok(Some(as_str)) => { |
| 62 | + if as_str.is_empty() { |
| 63 | + cold_path!(); |
| 64 | + Err(DeserializeError::invalid(Cow::Borrowed( |
| 65 | + "Input is a zero-length, empty document", |
| 66 | + ))) |
| 67 | + } else { |
| 68 | + Ok(Self { buffer: as_str }) |
| 69 | + } |
| 70 | + } |
| 71 | + Ok(None) => { |
33 | 72 | cold_path!(); |
34 | | - Err(DeserializeError::invalid(Cow::Borrowed( |
35 | | - "Input is a zero-length, empty document", |
36 | | - ))) |
37 | | - } else { |
38 | | - Ok(as_str) |
| 73 | + Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))) |
39 | 74 | } |
| 75 | + Err(_) => Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))), |
40 | 76 | } |
41 | | - None => { |
42 | | - cold_path!(); |
43 | | - Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))) |
44 | | - } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn as_str(&self) -> &'static str { |
| 80 | + self.buffer |
| 81 | + } |
| 82 | + |
| 83 | + pub fn as_bytes(&self) -> &'static [u8] { |
| 84 | + self.buffer.as_bytes() |
| 85 | + } |
| 86 | + |
| 87 | + pub fn len(&self) -> usize { |
| 88 | + self.buffer.len() |
45 | 89 | } |
46 | 90 | } |
0 commit comments