-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1129 from spkenv/preserve-install-environment
Preserve install environment
- Loading branch information
Showing
26 changed files
with
395 additions
and
105 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "is_default_derive_macro" | ||
version = { workspace = true } | ||
license-file = { workspace = true } | ||
homepage = { workspace = true } | ||
repository = { workspace = true } | ||
readme = { workspace = true } | ||
description = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
syn = "2.0" | ||
quote = "1.0" | ||
indicatif = { workspace = true } | ||
|
||
[dev-dependencies] | ||
tracing = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Contributors to the SPK project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// https://github.com/spkenv/spk | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data, DeriveInput, Fields}; | ||
|
||
#[proc_macro_derive(IsDefault)] | ||
pub fn derive_is_default(input: TokenStream) -> TokenStream { | ||
// Parse the input tokens into a syntax tree | ||
let input = parse_macro_input!(input as DeriveInput); | ||
|
||
// Get the name of the struct | ||
let name = input.ident; | ||
|
||
// Generate the trait implementation based on the struct's data (fields) | ||
let expanded = match input.data { | ||
Data::Struct(data_struct) => { | ||
match data_struct.fields { | ||
Fields::Named(fields_named) => { | ||
// Generate code that calls `IsDefault::is_default` on each field | ||
let field_checks = fields_named.named.iter().map(|field| { | ||
let field_name = &field.ident; | ||
quote! { | ||
spk_schema_foundation::IsDefault::is_default(&self.#field_name) | ||
} | ||
}); | ||
|
||
quote! { | ||
impl spk_schema_foundation::IsDefault for #name { | ||
fn is_default(&self) -> bool { | ||
true #(&& #field_checks)* | ||
} | ||
} | ||
} | ||
} | ||
Fields::Unnamed(fields_unnamed) => { | ||
let field_checks = fields_unnamed.unnamed.iter().enumerate().map(|(i, _)| { | ||
let index = syn::Index::from(i); | ||
quote! { | ||
spk_schema_foundation::IsDefault::is_default(&self.#index) | ||
} | ||
}); | ||
|
||
quote! { | ||
impl spk_schema_foundation::IsDefault for #name { | ||
fn is_default(&self) -> bool { | ||
true #(&& #field_checks)* | ||
} | ||
} | ||
} | ||
} | ||
Fields::Unit => { | ||
quote! { | ||
impl spk_schema_foundation::IsDefault for #name { | ||
fn is_default(&self) -> bool { | ||
true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
_ => panic!("IsDefault can only be derived for structs"), | ||
}; | ||
|
||
// Return the generated code | ||
TokenStream::from(expanded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (c) Contributors to the SPK project. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// https://github.com/spkenv/spk | ||
|
||
pub trait IsDefault { | ||
/// Returns true if the value equivalent to the default value. | ||
fn is_default(&self) -> bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.