Skip to content

Commit 0abf912

Browse files
authored
Upgrade to magic_migrate proc macro interface (#392)
* Update magic_migrate version * Use the preferred derive macro * Use the preferred derive macro * Use the preferred derive macro * Use the preferred derive macro * Fix docs * Update cache_diff * Update example to use derive macro
1 parent 99305fb commit 0abf912

9 files changed

+105
-188
lines changed

Cargo.lock

+19-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
resolver = "2"
33
members = ["buildpacks/ruby", "commons"]
44

5+
[workspace.dependencies]
6+
cache_diff = { version = "1.1", features = ["bullet_stream"] }
7+
58
[workspace.package]
69
edition = "2021"
710
rust-version = "1.82"

buildpacks/ruby/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ureq = { version = "2", default-features = false, features = ["tls"] }
3030
url = "2"
3131
magic_migrate = "1.0"
3232
toml = "0.8"
33-
cache_diff = { version = "1.0.0", features = ["bullet_stream"] }
33+
cache_diff.workspace = true
3434

3535
[dev-dependencies]
3636
libcnb-test = "=0.26.1"

buildpacks/ruby/src/layers/bundle_download_layer.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use libcnb::data::layer_name;
1616
use libcnb::layer::{EmptyLayerCause, LayerState};
1717
use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
1818
use libcnb::Env;
19-
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
19+
use magic_migrate::TryMigrate;
2020
use serde::{Deserialize, Serialize};
2121
use std::io::Stdout;
2222
use std::path::Path;
@@ -70,24 +70,15 @@ pub(crate) fn handle(
7070
}
7171

7272
pub(crate) type Metadata = MetadataV1;
73-
try_migrate_deserializer_chain!(
74-
deserializer: toml::Deserializer::new,
75-
error: MetadataError,
76-
chain: [MetadataV1],
77-
);
7873

79-
#[derive(Deserialize, Serialize, Debug, Clone, CacheDiff)]
74+
#[derive(Deserialize, Serialize, Debug, Clone, CacheDiff, TryMigrate)]
75+
#[try_migrate(from = None)]
8076
#[serde(deny_unknown_fields)]
8177
pub(crate) struct MetadataV1 {
8278
#[cache_diff(rename = "Bundler version")]
8379
pub(crate) version: ResolvedBundlerVersion,
8480
}
8581

86-
#[derive(Debug, thiserror::Error)]
87-
pub(crate) enum MetadataError {
88-
// Update if migrating between a metadata version can error
89-
}
90-
9182
fn download_bundler(
9283
bullet: Print<SubBullet<Stdout>>,
9384
env: &Env,

buildpacks/ruby/src/layers/bundle_install_layer.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use libcnb::{
3030
layer_env::{LayerEnv, ModificationBehavior, Scope},
3131
Env,
3232
};
33-
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
33+
use magic_migrate::TryMigrate;
3434
use serde::{Deserialize, Serialize};
3535
use std::io::Stdout;
3636
use std::{path::Path, process::Command};
@@ -121,22 +121,19 @@ pub(crate) fn handle(
121121
}
122122

123123
pub(crate) type Metadata = MetadataV3;
124-
try_migrate_deserializer_chain!(
125-
chain: [MetadataV1, MetadataV2, MetadataV3],
126-
error: MetadataMigrateError,
127-
deserializer: toml::Deserializer::new,
128-
);
129124

130-
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq)]
125+
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, TryMigrate)]
131126
#[serde(deny_unknown_fields)]
127+
#[try_migrate(from = None)]
132128
pub(crate) struct MetadataV1 {
133129
pub(crate) stack: String,
134130
pub(crate) ruby_version: ResolvedRubyVersion,
135131
pub(crate) force_bundle_install_key: String,
136132
pub(crate) digest: MetadataDigest, // Must be last for serde to be happy https://github.com/toml-rs/toml-rs/issues/142
137133
}
138134

139-
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq)]
135+
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, TryMigrate)]
136+
#[try_migrate(from = MetadataV1)]
140137
#[serde(deny_unknown_fields)]
141138
pub(crate) struct MetadataV2 {
142139
pub(crate) distro_name: String,
@@ -147,7 +144,8 @@ pub(crate) struct MetadataV2 {
147144
pub(crate) digest: MetadataDigest, // Must be last for serde to be happy https://github.com/toml-rs/toml-rs/issues/142
148145
}
149146

150-
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, CacheDiff)]
147+
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, CacheDiff, TryMigrate)]
148+
#[try_migrate(from = MetadataV2)]
151149
#[serde(deny_unknown_fields)]
152150
pub(crate) struct MetadataV3 {
153151
#[cache_diff(rename = "OS Distribution")]

buildpacks/ruby/src/layers/ruby_install_layer.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use flate2::read::GzDecoder;
2525
use libcnb::data::layer_name;
2626
use libcnb::layer::{EmptyLayerCause, LayerState};
2727
use libcnb::layer_env::LayerEnv;
28-
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
28+
use magic_migrate::TryMigrate;
2929
use serde::{Deserialize, Serialize};
3030
use std::io::{self, Stdout};
3131
use std::path::Path;
@@ -85,14 +85,16 @@ fn install_ruby(metadata: &Metadata, layer_path: &Path) -> Result<(), RubyBuildp
8585
Ok(())
8686
}
8787

88-
#[derive(Deserialize, Serialize, Debug, Clone)]
88+
#[derive(Deserialize, Serialize, Debug, Clone, TryMigrate)]
89+
#[try_migrate(from = None)]
8990
#[serde(deny_unknown_fields)]
9091
pub(crate) struct MetadataV1 {
9192
pub(crate) stack: String,
9293
pub(crate) version: ResolvedRubyVersion,
9394
}
9495

95-
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq)]
96+
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, TryMigrate)]
97+
#[try_migrate(from = MetadataV1)]
9698
#[serde(deny_unknown_fields)]
9799
pub(crate) struct MetadataV2 {
98100
pub(crate) distro_name: String,
@@ -101,7 +103,8 @@ pub(crate) struct MetadataV2 {
101103
pub(crate) ruby_version: ResolvedRubyVersion,
102104
}
103105

104-
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, CacheDiff)]
106+
#[derive(Deserialize, Serialize, Debug, Clone, Eq, PartialEq, CacheDiff, TryMigrate)]
107+
#[try_migrate(from = MetadataV2)]
105108
#[serde(deny_unknown_fields)]
106109
pub(crate) struct MetadataV3 {
107110
#[cache_diff(rename = "OS Distribution")]
@@ -123,11 +126,6 @@ impl MetadataV3 {
123126
}
124127

125128
pub(crate) type Metadata = MetadataV3;
126-
try_migrate_deserializer_chain!(
127-
chain: [MetadataV1, MetadataV2, MetadataV3],
128-
error: MetadataMigrateError,
129-
deserializer: toml::Deserializer::new,
130-
);
131129

132130
#[derive(thiserror::Error, Debug)]
133131
pub(crate) enum MetadataMigrateError {

commons/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ walkdir = "2"
2525
filetime = "0.2"
2626
magic_migrate = "1.0.1"
2727
toml = "0.8"
28-
cache_diff = "1.0"
28+
cache_diff.workspace = true
2929

3030
[dev-dependencies]
3131
filetime = "0.2"

commons/src/layer/diff_migrate.rs

+11-23
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ use bullet_stream as _;
9393
/// When a `CacheDiff::diff` is empty, the layer is kept and the old data is returned. Otherwise,
9494
/// the layer is deleted and the changes are returned.
9595
///
96-
/// **TUTORIAL:** In the [`diff_migrate`] module docs
96+
/// **TUTORIAL:** In the [`crate::layer::diff_migrate`] module docs
9797
#[derive(Debug, Clone, Eq, PartialEq)]
9898
pub struct DiffMigrateLayer {
9999
/// Whether the layer is intended for build.
@@ -138,7 +138,7 @@ impl DiffMigrateLayer {
138138
/// When given a prior [`LayerRename::from`] that exists, but the [`LayerRename::to`]
139139
/// does not, then the contents of the prior layer will be copied before being deleted.
140140
///
141-
/// After that this function callse [`cached_layer`] on the new layer.
141+
/// After that this function calls [`crate::layer::diff_migrate::DiffMigrateLayer::cached_layer`] on the new layer.
142142
///
143143
/// # Panics
144144
///
@@ -320,10 +320,11 @@ mod tests {
320320
use libcnb::data::layer_name;
321321
use libcnb::generic::{GenericMetadata, GenericPlatform};
322322
use libcnb::layer::{EmptyLayerCause, InvalidMetadataAction, LayerState, RestoredLayerAction};
323-
use magic_migrate::{migrate_toml_chain, try_migrate_deserializer_chain, Migrate, TryMigrate};
323+
use magic_migrate::TryMigrate;
324324
use std::convert::Infallible;
325325
/// Struct for asserting the behavior of `CacheBuddy`
326-
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
326+
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, TryMigrate)]
327+
#[try_migrate(from = None)]
327328
#[serde(deny_unknown_fields)]
328329
struct TestMetadata {
329330
value: String,
@@ -337,7 +338,6 @@ mod tests {
337338
}
338339
}
339340
}
340-
migrate_toml_chain! {TestMetadata}
341341

342342
struct FakeBuildpack;
343343
impl libcnb::Buildpack for FakeBuildpack {
@@ -555,13 +555,15 @@ mod tests {
555555
}
556556

557557
/// Struct for asserting the behavior of `invalid_metadata_action`
558-
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
558+
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, TryMigrate)]
559+
#[try_migrate(from = None)]
559560
#[serde(deny_unknown_fields)]
560561
struct PersonV1 {
561562
name: String,
562563
}
563564
/// Struct for asserting the behavior of `invalid_metadata_action`
564-
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone)]
565+
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, TryMigrate)]
566+
#[try_migrate(from = PersonV1)]
565567
#[serde(deny_unknown_fields)]
566568
struct PersonV2 {
567569
name: String,
@@ -583,25 +585,11 @@ mod tests {
583585
}
584586
}
585587
}
586-
#[derive(Debug, Eq, PartialEq)]
588+
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
589+
#[error("Not Richard")]
587590
struct NotRichard {
588591
name: String,
589592
}
590-
impl From<NotRichard> for PersonMigrationError {
591-
fn from(value: NotRichard) -> Self {
592-
PersonMigrationError::NotRichard(value)
593-
}
594-
}
595-
#[derive(Debug, Eq, PartialEq, thiserror::Error)]
596-
enum PersonMigrationError {
597-
#[error("Not Richard")]
598-
NotRichard(NotRichard),
599-
}
600-
try_migrate_deserializer_chain!(
601-
deserializer: toml::Deserializer::new,
602-
error: PersonMigrationError,
603-
chain: [PersonV1, PersonV2],
604-
);
605593

606594
#[test]
607595
fn test_invalid_metadata_action() {

0 commit comments

Comments
 (0)