Skip to content

Commit

Permalink
Move things around re: absorbing merde_time in merde_core
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 29, 2024
1 parent 47a5752 commit 79fc02f
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 77 deletions.
5 changes: 1 addition & 4 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ check:
cargo check --all-features --all-targets
cargo hack --each-feature --exclude-features=default,full check

cargo check --example simple --no-default-features --features=json
cargo run --example simple --features=core,json

# can't use cargo-nextest because we want to run doctests
cargo test -F full

Expand All @@ -27,5 +24,5 @@ check:
just miri

miri:
cargo +nightly miri run --example opinions -F json
cargo +nightly miri run --example opinions -F deserialize,json
cargo +nightly miri test -p merde_core fieldslot
21 changes: 9 additions & 12 deletions merde/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,8 @@ Which solves two problems at once:
`merde` solves both of these with wrapper types:

```rust
use merde::time::OffsetDateTime; // re-export from the time crate
use merde::CowStr;
use merde::time::Rfc3339;
use merde::time::{Rfc3339, OffsetDateTime};

#[derive(Debug)]
struct Person<'s> {
Expand All @@ -477,17 +476,15 @@ merde::derive! {
impl (Deserialize, Serialize) for struct Person<'s> { name, birth }
}

fn main() {
let input = r#"
{
"name": "Jane Smith",
"birth": "1990-01-01T00:00:00Z"
}
"#;
let input = r#"
{
"name": "Jane Smith",
"birth": "1990-01-01T00:00:00Z"
}
"#;

let person: Person = merde::json::from_str(input).unwrap();
println!("person = {:?}", person);
}
let person: Person = merde::json::from_str(input).unwrap();
println!("person = {:?}", person);
```

You can of course make your own newtype wrappers to control how a field gets deserialized.
Expand Down
17 changes: 1 addition & 16 deletions merde/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
#[cfg(feature = "core")]
use merde::CowStr;
use merde_json::JsonSerialize;

#[cfg(not(feature = "core"))]
type CowStr<'s> = std::borrow::Cow<'s, str>;

#[cfg(all(feature = "core", feature = "json"))]
fn main() {
use merde::json::JsonSerialize;

let input = r#"
{
"name": "John Doe",
Expand Down Expand Up @@ -36,15 +30,6 @@ fn main() {
assert_eq!(person, person2);
}

#[cfg(not(all(feature = "core", feature = "json")))]
fn main() {
eprintln!("Well if the `core` feature is not enabled,");
eprintln!("we can't call `from_str_via_value` and stuff,");
eprintln!("but this still serves as an example that");
eprintln!("you can keep your `merde::derive!` in place,");
eprintln!("they'll just not generate any code.");
}

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
struct Address<'s> {
Expand Down
3 changes: 0 additions & 3 deletions merde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub use merde_yaml as yaml;
#[allow(unused_imports)]
use json::JsonSerialize;

#[cfg(feature = "time")]
pub use merde_time as time;

#[cfg(feature = "core")]
pub use merde_core::*;

Expand Down
1 change: 1 addition & 0 deletions merde_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ rusqlite = ["dep:rusqlite"]
[dev-dependencies]
insta = "1.40.0"
trybuild = "1.0.101"
time = { version = "0.3.36", features = ["macros"] }
4 changes: 2 additions & 2 deletions merde_core/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'s> IntoIterator for Array<'s> {
}
}

impl<'s> Default for Array<'s> {
impl Default for Array<'_> {
fn default() -> Self {
Self::new()
}
Expand All @@ -65,7 +65,7 @@ impl<'s> Deref for Array<'s> {
}
}

impl<'s> DerefMut for Array<'s> {
impl DerefMut for Array<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
Expand Down
4 changes: 2 additions & 2 deletions merde_core/src/cowbytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> From<&'a [u8]> for CowBytes<'a> {
}
}

impl<'a> From<Vec<u8>> for CowBytes<'a> {
impl From<Vec<u8>> for CowBytes<'_> {
fn from(v: Vec<u8>) -> Self {
CowBytes::Owned(CompactBytes::from(v))
}
Expand All @@ -67,7 +67,7 @@ impl<'a> From<Cow<'a, [u8]>> for CowBytes<'a> {
}
}

impl<'a, 'b> PartialEq<CowBytes<'a>> for CowBytes<'b> {
impl<'a> PartialEq<CowBytes<'a>> for CowBytes<'_> {
fn eq(&self, other: &CowBytes<'a>) -> bool {
self.deref() == other.deref()
}
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/cowstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl From<CowStr<'_>> for Box<str> {
}
}

impl<'a, 'b> PartialEq<CowStr<'a>> for CowStr<'b> {
impl<'a> PartialEq<CowStr<'a>> for CowStr<'_> {
#[inline]
fn eq(&self, other: &CowStr<'a>) -> bool {
crate::compatibility_check_once();
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl std::fmt::Display for MerdeError<'_> {
}
}

impl<'s> std::error::Error for MerdeError<'s> {}
impl std::error::Error for MerdeError<'_> {}

impl Value<'_> {
/// Returns the [ValueType] for a given [Value].
Expand Down
4 changes: 2 additions & 2 deletions merde_core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'s> From<&'s str> for Event<'s> {
}
}

impl<'s> From<String> for Event<'s> {
impl From<String> for Event<'_> {
fn from(v: String) -> Self {
Event::Str(v.into())
}
Expand All @@ -89,7 +89,7 @@ impl<'s> From<&'s [u8]> for Event<'s> {
}
}

impl<'s> From<Vec<u8>> for Event<'s> {
impl From<Vec<u8>> for Event<'_> {
fn from(v: Vec<u8>) -> Self {
Event::Bytes(v.into())
}
Expand Down
4 changes: 2 additions & 2 deletions merde_core/src/into_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
}
}

impl<'a, T> IntoStatic for Cow<'a, T>
impl<T> IntoStatic for Cow<'_, T>
where
T: ToOwned + ?Sized + 'static,
{
Expand All @@ -51,7 +51,7 @@ where
}
}

impl<'s> IntoStatic for Event<'s> {
impl IntoStatic for Event<'_> {
type Output = Event<'static>;

fn into_static(self) -> Self::Output {
Expand Down
3 changes: 1 addition & 2 deletions merde_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ pub use deserialize::DeserializeOwned;
pub use deserialize::Deserializer;
pub use deserialize::FieldSlot;

mod rfc3339;
pub use rfc3339::Rfc3339;
pub mod time;

rubicon::compatibility_check! {
("merde_core_pkg_version", env!("CARGO_PKG_VERSION")),
Expand Down
2 changes: 1 addition & 1 deletion merde_core/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'s> IntoIterator for Map<'s> {
}
}

impl<'s> Default for Map<'s> {
impl Default for Map<'_> {
fn default() -> Self {
Self::new()
}
Expand Down
80 changes: 64 additions & 16 deletions merde_core/src/rfc3339.rs → merde_core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ use std::{
ops::{Deref, DerefMut},
};

use crate::WithLifetime;

/// A wrapper around date-time types that implements `Serialize` and `Deserialize`
/// when the right cargo features are enabled.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct Rfc3339<T>(pub T);

impl<T> WithLifetime<'_> for Rfc3339<T>
where
T: 'static,
{
type Lifetimed = Self;
}

impl<T> From<T> for Rfc3339<T> {
fn from(t: T) -> Self {
Rfc3339(t)
Expand Down Expand Up @@ -51,6 +60,9 @@ where
}
}

#[cfg(feature = "time")]
pub use time::OffsetDateTime;

#[cfg(feature = "time")]
mod time_impls {
use super::*;
Expand Down Expand Up @@ -99,28 +111,64 @@ mod time_impls {
#[cfg(all(test, feature = "full"))]
mod tests {
use super::*;
use merde_json::{from_str, JsonSerialize};
use crate::{Deserialize, Deserializer, Event, IntoStatic, MerdeError, Serializer};
use std::collections::VecDeque;
use time::macros::datetime;

#[test]
fn test_rfc3339_offset_date_time_roundtrip() {
let original = Rfc3339(datetime!(2023-05-15 14:30:00 UTC));
let serialized = original.to_json_string().unwrap();
let deserialized: Rfc3339<time::OffsetDateTime> = from_str(&serialized).unwrap();
assert_eq!(original, deserialized);
#[derive(Debug, Default)]
struct Journal {
events: VecDeque<Event<'static>>,
}

#[test]
fn test_rfc3339_offset_date_time_serialization() {
let dt = Rfc3339(datetime!(2023-05-15 14:30:00 UTC));
let serialized = dt.to_json_string().unwrap();
assert_eq!(serialized, r#""2023-05-15T14:30:00Z""#);
impl Serializer for Journal {
type Error = std::convert::Infallible;

async fn write(&mut self, event: Event<'_>) -> Result<(), Self::Error> {
self.events.push_back(event.into_static());
Ok(())
}
}

impl<'s> Deserializer<'s> for Journal {
type Error<'es> = MerdeError<'es>;

fn next(&mut self) -> Result<Event<'s>, Self::Error<'s>> {
Ok(self.events.pop_front().unwrap())
}

async fn t_starting_with<T: Deserialize<'s>>(
&mut self,
starter: Option<Event<'s>>,
) -> Result<T, Self::Error<'s>> {
if let Some(event) = starter {
self.events.push_front(event.into_static());
}
T::deserialize(self).await
}
}

#[test]
fn test_rfc3339_offset_date_time_deserialization() {
let json = r#""2023-05-15T14:30:00Z""#;
let deserialized: Rfc3339<time::OffsetDateTime> = from_str(json).unwrap();
assert_eq!(deserialized, Rfc3339(datetime!(2023-05-15 14:30:00 UTC)));
fn test_rfc3339_offset_date_time_roundtrip() {
let original = Rfc3339(datetime!(2023-05-15 14:30:00 UTC));
let mut journal: Journal = Default::default();

journal.serialize_sync(&original).unwrap();
let deserialized: Rfc3339<time::OffsetDateTime> = journal.deserialize_owned().unwrap();

assert_eq!(original, deserialized);
}

// #[test]
// fn test_rfc3339_offset_date_time_serialization() {
// let dt = Rfc3339(datetime!(2023-05-15 14:30:00 UTC));
// let serialized = dt.to_json_string().unwrap();
// assert_eq!(serialized, r#""2023-05-15T14:30:00Z""#);
// }

// #[test]
// fn test_rfc3339_offset_date_time_deserialization() {
// let json = r#""2023-05-15T14:30:00Z""#;
// let deserialized: Rfc3339<time::OffsetDateTime> = from_str(json).unwrap();
// assert_eq!(deserialized, Rfc3339(datetime!(2023-05-15 14:30:00 UTC)));
// }
}
10 changes: 5 additions & 5 deletions merde_core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ impl_from_for_value! {
CowBytes<'s> => Bytes,
}

impl<'s> From<f32> for Value<'s> {
impl From<f32> for Value<'_> {
fn from(v: f32) -> Self {
Value::Float((v as f64).into())
}
}

impl<'s> From<f64> for Value<'s> {
impl From<f64> for Value<'_> {
fn from(v: f64) -> Self {
Value::Float(v.into())
}
Expand All @@ -90,7 +90,7 @@ impl<'s> From<&'s str> for Value<'s> {
}
}

impl<'s> From<String> for Value<'s> {
impl From<String> for Value<'_> {
fn from(v: String) -> Self {
Value::Str(v.into())
}
Expand All @@ -102,13 +102,13 @@ impl<'s> From<&'s String> for Value<'s> {
}
}

impl<'s> From<()> for Value<'s> {
impl From<()> for Value<'_> {
fn from(_: ()) -> Self {
Value::Null
}
}

impl<'s> From<bool> for Value<'s> {
impl From<bool> for Value<'_> {
fn from(v: bool) -> Self {
Value::Bool(v)
}
Expand Down
6 changes: 3 additions & 3 deletions merde_core/src/with_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ macro_rules! impl_with_lifetime {
};
}

impl<'a, 's, B: ToOwned + ?Sized + 's> WithLifetime<'s> for Cow<'a, B> {
impl<'s, B: ToOwned + ?Sized + 's> WithLifetime<'s> for Cow<'_, B> {
type Lifetimed = Cow<'s, B>;
}

impl<'a, 's> WithLifetime<'s> for &'a str {
impl<'s> WithLifetime<'s> for &str {
type Lifetimed = &'s str;
}

Expand All @@ -66,7 +66,7 @@ impl_with_lifetime!(
f64,
);

impl<'s> WithLifetime<'s> for () {
impl WithLifetime<'_> for () {
type Lifetimed = ();
}

Expand Down
2 changes: 1 addition & 1 deletion merde_json/src/jiter_lite/string_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'t, 'j> From<StringOutput<'t, 'j>> for Cow<'j, str> {
}
}

impl<'t, 'j> StringOutput<'t, 'j> {
impl<'t> StringOutput<'t, '_> {
pub fn as_str(&self) -> &'t str {
match self {
Self::Tape(s, _) => s,
Expand Down
Loading

0 comments on commit 79fc02f

Please sign in to comment.