Skip to content
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
27 changes: 25 additions & 2 deletions document/migrations/snapshot/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct NodeSource {
pub version: u32,
pub function: String,
pub file: PathBuf,
/// The parameters and output as written, without their attributes. Comparing it tells whether stored inputs still fit.
/// The parameters and output as written, without their attributes or `#[data]` fields. Comparing it tells whether stored inputs still fit.
pub signature: String,
}

Expand Down Expand Up @@ -131,7 +131,10 @@ fn node_source(function: &syn::ItemFn, file: &Path, module_path: &[String]) -> O
}

let function_name = function.sig.ident.to_string();
let parameters = function.sig.inputs.iter().map(|parameter| match parameter {

// A `#[data]` field is the node's own state rather than a stored input, so documents don't notice it changing
let is_data_field = |parameter: &&syn::FnArg| matches!(parameter, syn::FnArg::Typed(typed) if typed.attrs.iter().any(|attribute| attribute.path().is_ident("data")));
let parameters = function.sig.inputs.iter().filter(|parameter| !is_data_field(parameter)).map(|parameter| match parameter {
syn::FnArg::Typed(typed) => {
let (pattern, ty) = (&typed.pat, &typed.ty);
quote::quote!(#pattern: #ty).to_string()
Expand All @@ -149,3 +152,23 @@ fn node_source(function: &syn::ItemFn, file: &Path, module_path: &[String]) -> O
signature,
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn data_fields_are_left_out_of_the_signature() {
let function: syn::ItemFn = syn::parse_quote! {
#[node_macro::node(category("Raster"))]
fn lookup(_: impl Ctx, image: Item<Color>, #[data] cache: Cache) -> Item<Color> {
image
}
};
let node = node_source(&function, Path::new("lib.rs"), &["raster_nodes".to_string()]).expect("the function is a node");

assert_eq!(node.identifier, "raster_nodes::LookupNode");
assert!(!node.signature.contains("cache"), "{}", node.signature);
assert!(node.signature.contains("image"), "{}", node.signature);
}
}
23 changes: 20 additions & 3 deletions document/migrations/snapshot/src/scaffold.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Scaffolds node migrations into `migration-nodes`, one file per step, in a module per node crate.

use crate::lineage::{MigrationStep, find_migrations};
use crate::lineage::{MigrationStep, find_all_migrations};
use crate::nodes::{NodeSource, find_nodes};
use convert_case::{Case, Casing};
use std::path::{Path, PathBuf};
Expand All @@ -18,7 +18,7 @@ pub(crate) fn scaffold(workspace: &Path, command: &str, arguments: &[String]) ->

match (command, arguments) {
("new", []) => {
let steps = missing_steps(&nodes, &find_migrations(&migrations)?);
let steps = missing_steps(&nodes, &find_all_migrations(workspace, &MIGRATION_CRATES)?);
if steps.is_empty() {
println!("Every node version already has a migration leading to it");
return Ok(());
Expand All @@ -35,7 +35,7 @@ pub(crate) fn scaffold(workspace: &Path, command: &str, arguments: &[String]) ->
));
}

let steps = missing_steps(std::slice::from_ref(node), &find_migrations(&migrations)?);
let steps = missing_steps(std::slice::from_ref(node), &find_all_migrations(workspace, &MIGRATION_CRATES)?);
if steps.is_empty() {
return Err(format!("migrations up to `{}`@{} already exist", node.identifier, node.version));
}
Expand Down Expand Up @@ -242,6 +242,7 @@ fn declare_module(file: &Path, module: &str) -> Result<(), String> {
#[cfg(test)]
mod tests {
use super::*;
use crate::lineage::find_migrations;

/// A workspace with one node crate and an empty `migration-nodes`, removed on drop.
struct Workspace(PathBuf);
Expand Down Expand Up @@ -329,6 +330,22 @@ mod tests {
assert!(missing_steps(&nodes, &found).is_empty());
}

#[test]
fn a_version_reached_from_a_compiled_migration_needs_no_step() {
let workspace = Workspace::new("compiled");
let compiled = workspace.0.join("document/migrations/compiled/src");
std::fs::create_dir_all(&compiled).unwrap();
std::fs::write(
compiled.join("lib.rs"),
"#[migration(\"old::Node\"@1 => \"blending_nodes::OpacityNode\"@2)]\npub fn opacity_from_node(inputs: &mut Inputs) {}\n",
)
.unwrap();

scaffold(&workspace.0, "new", &[]).unwrap();
assert!(!workspace.0.join(MIGRATIONS).join("blending_nodes/opacity_v1.rs").exists());
assert!(workspace.read("legacy/brighten_v1.rs").contains("\"legacy::adjust::BrightenNode\"@1 => @2"));
}

#[test]
fn a_file_no_module_declares_is_no_migration() {
let workspace = Workspace::new("undeclared");
Expand Down
Loading