Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced required use statements #14

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Unreleased

- Fix: Macro users no longer need to import `std::convert::Infallible` or `serde::de::Deserializer` (https://github.com/schneems/magic_migrate/pull/14)

## 1.0.0 - 2024/12/18

- Change: `TryMigrate::Error` must now be `Display` + `Debug` ()
- Change: `TryMigrate::Error` must now be `Display` + `Debug` (https://github.com/schneems/magic_migrate/pull/11)

## 0.2.1 - 2024/12/12

Expand Down
11 changes: 4 additions & 7 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,12 +56,10 @@ 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};
use std::convert::Infallible;

#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
Expand All @@ -76,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
1 change: 0 additions & 1 deletion src/fixtures/personV1_V2.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# use chrono::{DateTime, Utc};
# use serde::{Deserialize, Serialize};
# use std::convert::Infallible;
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
Expand Down
1 change: 0 additions & 1 deletion src/fixtures/try_personV1_V2.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# use serde::{Deserialize, Serialize};
# use std::convert::Infallible;
#
# #[derive(Deserialize, Serialize, Debug)]
# #[serde(deny_unknown_fields)]
Expand Down
35 changes: 13 additions & 22 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,12 +512,12 @@ 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)
}
}
impl From<Infallible> for $err {
fn from(_value: Infallible) -> Self {
impl From<std::convert::Infallible> for $err {
fn from(_value: std::convert::Infallible) -> Self {
unreachable!();
}
}
Expand Down
Loading