Skip to content

Commit

Permalink
serialize::per_type, ffi::yyjson and other reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Sep 20, 2023
1 parent 85ecaf3 commit 53e2f02
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 152 deletions.
2 changes: 1 addition & 1 deletion script/generate-yyjson
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ bindgen \
--allowlist-type=yyjson_val \
--allowlist-var=YYJSON_READ_NOFLAG \
--allowlist-var=YYJSON_READ_SUCCESS \
> "${_repo}/src/yyjson.rs"
> "${_repo}/src/ffi/yyjson.rs"
2 changes: 1 addition & 1 deletion src/deserialize/utf8.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::deserialize::DeserializeError;
use crate::error::INVALID_STR;
use crate::ffi::*;
use crate::str::*;
use crate::typeref::*;
use crate::util::INVALID_STR;
use std::borrow::Cow;
use std::os::raw::c_char;

Expand Down
2 changes: 1 addition & 1 deletion src/deserialize/yyjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use crate::deserialize::pyobject::*;
use crate::deserialize::DeserializeError;
use crate::ffi::yyjson::*;
use crate::str::*;
use crate::typeref::*;
use crate::yyjson::*;
use std::borrow::Cow;
use std::os::raw::c_char;
use std::ptr::{null, null_mut, NonNull};
Expand Down
3 changes: 0 additions & 3 deletions src/error.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ mod fragment;
mod immortal;
mod list;
mod long;
#[cfg(feature = "yyjson")]
pub mod yyjson;

pub use buffer::*;
pub use bytes::*;
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
mod util;

mod deserialize;
mod error;
mod ffi;
mod opt;
mod serialize;
mod str;
mod typeref;

#[cfg(feature = "yyjson")]
mod yyjson;

use pyo3_ffi::*;
use std::os::raw::c_char;
use std::os::raw::c_int;
Expand Down
3 changes: 1 addition & 2 deletions src/serialize/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::error::INVALID_STR;
use std::ffi::CStr;
use std::ptr::NonNull;

Expand Down Expand Up @@ -33,7 +32,7 @@ impl std::fmt::Display for SerializeError {
}
SerializeError::Integer53Bits => write!(f, "Integer exceeds 53-bit range"),
SerializeError::Integer64Bits => write!(f, "Integer exceeds 64-bit range"),
SerializeError::InvalidStr => write!(f, "{}", INVALID_STR),
SerializeError::InvalidStr => write!(f, "{}", crate::util::INVALID_STR),
SerializeError::InvalidFragment => write!(f, "orjson.Fragment's content is not of type bytes or str"),
SerializeError::KeyMustBeStr => write!(f, "Dict key must be str"),
SerializeError::RecursionLimit => write!(f, "Recursion limit reached"),
Expand Down
23 changes: 2 additions & 21 deletions src/serialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,31 +389,12 @@ where
type Ok = ();
type Error = Error;

#[inline(always)]
fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<()>
fn serialize_entry<K, V>(&mut self, _key: &K, _value: &V) -> Result<()>
where
K: ?Sized + Serialize,
V: ?Sized + Serialize,
{
self.ser
.formatter
.begin_object_key(&mut self.ser.writer, self.state == State::First)
.map_err(Error::io)?;
key.serialize(MapKeySerializer { ser: self.ser })?;

self.ser
.formatter
.end_object_key(&mut self.ser.writer)
.map_err(Error::io)?;
self.ser
.formatter
.begin_object_value(&mut self.ser.writer)
.map_err(Error::io)?;
value.serialize(&mut *self.ser)?;
self.ser
.formatter
.end_object_value(&mut self.ser.writer)
.map_err(Error::io)
unreachable!()
}

#[inline]
Expand Down
16 changes: 1 addition & 15 deletions src/serialize/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

mod dataclass;
mod datetime;
#[macro_use]
mod datetimelike;
mod default;
mod dict;
mod error;
mod float;
mod fragment;
mod int;
mod json;
mod list;
mod numpy;
mod pyenum;
mod per_type;
mod serializer;
mod str;
mod tuple;
mod uuid;
mod writer;

pub use serializer::serialize;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use crate::opt::*;
use crate::serialize::datetimelike::{DateTimeBuffer, DateTimeError, DateTimeLike, Offset};
use crate::serialize::error::*;
use crate::serialize::per_type::datetimelike::{
DateTimeBuffer, DateTimeError, DateTimeLike, Offset,
};
use crate::typeref::*;
use serde::ser::{Serialize, Serializer};

Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/serialize/dict.rs → src/serialize/per_type/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

use crate::ffi::PyDictIter;
use crate::opt::*;
use crate::serialize::datetime::*;
use crate::serialize::datetimelike::*;
use crate::serialize::error::*;
use crate::serialize::serializer::pyobject_to_obtype;
use crate::serialize::serializer::*;
use crate::serialize::uuid::*;
use crate::serialize::per_type::datetimelike::{DateTimeBuffer, DateTimeLike};
use crate::serialize::per_type::*;
use crate::serialize::serializer::{
pyobject_to_obtype, ObType, PyObjectSerializer, RECURSION_LIMIT,
};
use crate::str::*;
use crate::typeref::*;
use compact_str::CompactString;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/serialize/list.rs → src/serialize/per_type/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::ffi::PyListIter;
use crate::opt::*;
use crate::serialize::error::*;
use crate::serialize::serializer::*;
use crate::serialize::serializer::{PyObjectSerializer, RECURSION_LIMIT};

use serde::ser::{Serialize, SerializeSeq, Serializer};
use std::ptr::NonNull;
Expand Down
35 changes: 35 additions & 0 deletions src/serialize/per_type/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

mod dataclass;
mod datetime;
mod pybool;
#[macro_use]
mod datetimelike;
mod default;
mod dict;
mod float;
mod fragment;
mod int;
mod list;
mod none;
mod numpy;
mod pyenum;
mod tuple;
mod unicode;
mod uuid;

pub use dataclass::DataclassGenericSerializer;
pub use datetime::{Date, DateTime, Time};
pub use default::DefaultSerializer;
pub use dict::DictGenericSerializer;
pub use float::FloatSerializer;
pub use fragment::FragmentSerializer;
pub use int::{Int53Serializer, IntSerializer};
pub use list::ListSerializer;
pub use none::NoneSerializer;
pub use numpy::{is_numpy_array, is_numpy_scalar, NumpyScalar, NumpySerializer};
pub use pybool::BoolSerializer;
pub use pyenum::EnumSerializer;
pub use tuple::TupleSerializer;
pub use unicode::{StrSerializer, StrSubclassSerializer};
pub use uuid::UUID;
21 changes: 21 additions & 0 deletions src/serialize/per_type/none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use serde::ser::{Serialize, Serializer};

pub struct NoneSerializer;

impl NoneSerializer {
pub fn new() -> Self {
NoneSerializer {}
}
}

impl Serialize for NoneSerializer {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_unit()
}
}
7 changes: 5 additions & 2 deletions src/serialize/numpy.rs → src/serialize/per_type/numpy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::opt::*;
use crate::serialize::datetimelike::{DateTimeBuffer, DateTimeError, DateTimeLike, Offset};
use crate::serialize::default::*;

use crate::serialize::error::*;
use crate::serialize::per_type::datetimelike::{
DateTimeBuffer, DateTimeError, DateTimeLike, Offset,
};
use crate::serialize::per_type::*;
use crate::typeref::{load_numpy_types, ARRAY_STRUCT_STR, DESCR_STR, DTYPE_STR, NUMPY_TYPES};
use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike};
use pyo3_ffi::*;
Expand Down
24 changes: 24 additions & 0 deletions src/serialize/per_type/pybool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

use serde::ser::{Serialize, Serializer};

#[repr(transparent)]
pub struct BoolSerializer {
ptr: *mut pyo3_ffi::PyObject,
}

impl BoolSerializer {
pub fn new(ptr: *mut pyo3_ffi::PyObject) -> Self {
BoolSerializer { ptr: ptr }
}
}

impl Serialize for BoolSerializer {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bool(unsafe { self.ptr == crate::typeref::TRUE })
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 53e2f02

Please sign in to comment.