Skip to content

Commit

Permalink
No need to import use serde::de::Deserializer;
Browse files Browse the repository at this point in the history
  • Loading branch information
schneems committed Dec 20, 2024
1 parent 1c813cb commit 7a33913
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ Once defined, invoke migrations via the `try_from_str_migrations` associated fun
To define migrations you can use a macro like this:

```ignore
use magic_migrate::{TryMigrate, try_migrate_deserializer_chain};
use serde::de::Deserializer;
use magic_migrate::TryMigrate;
// ...
try_migrate_deserializer_chain!(
magic_migrate::try_migrate_deserializer_chain!(
deserializer: toml::Deserializer::new,
error: PersonMigrationError,
chain: [PersonV1, PersonV2],
Expand All @@ -57,8 +56,7 @@ try_migrate_deserializer_chain!(
Full example:

```rust
use magic_migrate::{TryMigrate, try_migrate_deserializer_chain};
use serde::de::Deserializer;
use magic_migrate::TryMigrate;

use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
Expand All @@ -75,7 +73,7 @@ struct PersonV2 {
name: String,
updated_at: DateTime<Utc>
}
try_migrate_deserializer_chain!( // <=========== HERE
magic_migrate::try_migrate_deserializer_chain!( // <=========== HERE
deserializer: toml::Deserializer::new,
error: PersonMigrationError,
chain: [PersonV1, PersonV2],
Expand Down
31 changes: 11 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![doc = include_str!("../README.md")]

use serde::de::DeserializeOwned;
use serde::Deserializer;
use std::any::{Any, TypeId};
use std::fmt::{Debug, Display};

Expand Down Expand Up @@ -40,7 +39,6 @@ use std::fmt::{Debug, Display};
///
/// ```rust
/// use magic_migrate::Migrate;
/// use serde::de::Deserializer;
///
#[doc = include_str!("fixtures/personV1_V2.txt")]
///
Expand All @@ -54,7 +52,7 @@ use std::fmt::{Debug, Display};
/// impl Migrate for PersonV1 {
/// type From = Self;
///
/// fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
/// fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
/// toml::Deserializer::new(input)
/// }
/// }
Expand All @@ -66,7 +64,7 @@ use std::fmt::{Debug, Display};
/// impl Migrate for PersonV2 {
/// type From = PersonV1;
///
/// fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
/// fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
/// <Self as Migrate>::From::deserializer(input)
/// }
/// }
Expand All @@ -91,7 +89,7 @@ use std::fmt::{Debug, Display};
pub trait Migrate: From<Self::From> + Any + DeserializeOwned + Debug {
type From: Migrate;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de>;
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de>;

fn from_str_migrations(input: &str) -> Option<Self> {
if let Ok(instance) = Self::deserialize(Self::deserializer(input)) {
Expand Down Expand Up @@ -128,8 +126,6 @@ pub trait Migrate: From<Self::From> + Any + DeserializeOwned + Debug {
///
/// ```rust
/// use magic_migrate::TryMigrate;
/// use serde::de::Deserializer;
///
#[doc = include_str!("fixtures/try_personV1_V2.txt")]
///
/// // First define a migration on the beginning of the chain
Expand All @@ -143,7 +139,7 @@ pub trait Migrate: From<Self::From> + Any + DeserializeOwned + Debug {
/// type TryFrom = Self;
/// type Error = PersonMigrationError;
///
/// fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
/// fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
/// toml::Deserializer::new(input)
/// }
/// }
Expand All @@ -165,7 +161,7 @@ pub trait Migrate: From<Self::From> + Any + DeserializeOwned + Debug {
/// type TryFrom = PersonV1;
/// type Error = PersonMigrationError;
///
/// fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
/// fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
/// <Self as TryMigrate>::TryFrom::deserializer(input)
/// }
/// }
Expand Down Expand Up @@ -195,7 +191,7 @@ pub trait TryMigrate: TryFrom<Self::TryFrom> + Any + DeserializeOwned + Debug {

/// Tell magic migrate how you want to deserialize your strings
/// into structs
fn deserializer<'de>(input: &str) -> impl Deserializer<'de>;
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de>;

type Error: From<<Self as TryFrom<<Self as TryMigrate>::TryFrom>>::Error>
+ From<<<Self as TryMigrate>::TryFrom as TryMigrate>::Error>
Expand Down Expand Up @@ -228,7 +224,7 @@ where
{
type TryFrom = <Self as Migrate>::From;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
<Self as Migrate>::deserializer(input)
}

Expand Down Expand Up @@ -258,7 +254,7 @@ macro_rules! migrate_link {
impl Migrate for $b {
type From = $a;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
<Self as Migrate>::From::deserializer(input)
}
}
Expand All @@ -285,7 +281,6 @@ macro_rules! migrate_link {
///
/// ```no_run
/// use magic_migrate::Migrate;
/// use serde::de::Deserializer;
#[doc = include_str!("fixtures/personV1_V2.txt")]
///
/// // - Link PersonV1 => PersonV1 and set the toml deserializer
Expand Down Expand Up @@ -335,7 +330,7 @@ macro_rules! try_migrate_link {
type TryFrom = $a;
type Error = <<Self as TryMigrate>::TryFrom as TryMigrate>::Error;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
<Self as TryMigrate>::TryFrom::deserializer(input)
}
}
Expand All @@ -358,8 +353,6 @@ macro_rules! try_migrate_link {
///
/// ```rust
/// use magic_migrate::TryMigrate;
/// use serde::Deserializer;
///
#[doc = include_str!("fixtures/try_personV1_V2.txt")]
///
/// magic_migrate::try_migrate_toml_chain!(
Expand Down Expand Up @@ -421,7 +414,6 @@ macro_rules! try_migrate_toml_chain {
///
/// ```rust
/// use magic_migrate::Migrate;
/// use serde::de::Deserializer;
#[doc = include_str!("fixtures/personV1_V2.txt")]
///
/// magic_migrate::migrate_deserializer_chain!(
Expand All @@ -447,7 +439,7 @@ macro_rules! migrate_deserializer_chain {
impl Migrate for $a {
type From = Self;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
$deser(input)
}
}
Expand Down Expand Up @@ -484,7 +476,6 @@ macro_rules! migrate_deserializer_chain {
///
/// ```rust
/// use magic_migrate::TryMigrate;
/// use serde::Deserializer;
#[doc = include_str!("fixtures/try_personV1_V2.txt")]
///
/// magic_migrate::try_migrate_deserializer_chain!(
Expand Down Expand Up @@ -521,7 +512,7 @@ macro_rules! try_migrate_deserializer_chain {
type TryFrom = Self;
type Error = $err;

fn deserializer<'de>(input: &str) -> impl Deserializer<'de> {
fn deserializer<'de>(input: &str) -> impl serde::de::Deserializer<'de> {
$deser(input)
}
}
Expand Down

0 comments on commit 7a33913

Please sign in to comment.