-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 2 new crates: noelware-config & noelware-remi, fix noelware-serde…
… doctests
- Loading branch information
Showing
23 changed files
with
1,279 additions
and
6 deletions.
There are no files selected for viewing
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,44 @@ | ||
# 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
# Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
[package] | ||
name = "noelware-config-derive" | ||
description = "🐻❄️🪚 Derive macro for `noelware-config`, this crate shouldn't be imported directly." | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
rust-version.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro-error = "1.0.4" | ||
proc-macro2 = "1.0.76" | ||
quote = "1.0.35" | ||
syn = "2.0.48" | ||
|
||
[dev-dependencies] | ||
noelware-config = { path = "../config" } | ||
trybuild = "1.0.89" |
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,58 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
Path, Result, Token, | ||
}; | ||
|
||
#[derive(Default)] | ||
pub struct Args { | ||
pub is_skipped: bool, | ||
pub strategy: Option<Path>, | ||
} | ||
|
||
mod kw { | ||
syn::custom_keyword!(strategy); | ||
syn::custom_keyword!(skip); | ||
} | ||
|
||
impl Parse for Args { | ||
fn parse(input: ParseStream) -> Result<Self> { | ||
let mut args = Args::default(); | ||
|
||
if input.peek(kw::skip) { | ||
input.parse::<kw::skip>()?; | ||
args.is_skipped = true; | ||
|
||
return Ok(args); | ||
} | ||
|
||
if input.peek(kw::strategy) { | ||
input.parse::<kw::strategy>()?; | ||
input.parse::<Token![=]>()?; | ||
|
||
args.strategy = Some(input.parse()?); | ||
} | ||
|
||
Ok(args) | ||
} | ||
} |
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,85 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
use crate::args::Args; | ||
use proc_macro2::TokenStream; | ||
use proc_macro_error::set_dummy; | ||
use quote::{quote, quote_spanned}; | ||
use syn::{spanned::Spanned, DeriveInput, Field, Fields}; | ||
|
||
pub fn struct_fields(input: &DeriveInput, fields: &Fields) -> TokenStream { | ||
let name = &input.ident; | ||
set_dummy(quote! { | ||
impl ::noelware_config::merge::Merge for #name { | ||
fn merge(&self, other: Self) { | ||
unimplemented!() | ||
} | ||
} | ||
}); | ||
|
||
let mut assignments = Vec::with_capacity(fields.len()); | ||
for field in fields { | ||
if let Some(tt) = gen_struct_field_assignment(field) { | ||
assignments.push(tt); | ||
} | ||
} | ||
|
||
quote! { | ||
impl ::noelware_config::merge::Merge for #name { | ||
fn merge(&mut self, other: Self) { | ||
#(#assignments)* | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn gen_struct_field_assignment(field: &Field) -> Option<TokenStream> { | ||
let attr = field | ||
.attrs | ||
.iter() | ||
.filter(|s| match s.meta.path().get_ident() { | ||
Some(ident) => ident == "merge", | ||
None => false, | ||
}) | ||
.filter_map(|s| s.parse_args::<Args>().ok()) | ||
.collect::<Vec<_>>(); | ||
|
||
let first = attr | ||
.first() | ||
// only needed since &Args doesn't implement Default | ||
// TODO(@auguwu): fix | ||
.map(|s| Args { | ||
is_skipped: s.is_skipped, | ||
strategy: s.strategy.clone(), | ||
}) | ||
.unwrap_or_default(); | ||
|
||
// don't even attempt to merge if it is skipped | ||
if first.is_skipped { | ||
return None; | ||
} | ||
|
||
let name = field.ident.as_ref().expect("expected identifier"); | ||
Some(match first.strategy { | ||
Some(path) => quote_spanned!(path.span()=> #path(&mut self.#name, other.#name)), | ||
None => quote_spanned!(field.span()=> ::noelware_config::merge::Merge::merge(&mut self.#name, other.#name)), | ||
}) | ||
} |
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,44 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro2::Span; | ||
use proc_macro_error::proc_macro_error; | ||
use std::fmt::Display; | ||
use syn::{parse_macro_input, Data, DataStruct, DeriveInput}; | ||
|
||
mod args; | ||
mod expand; | ||
|
||
#[proc_macro_error] | ||
#[proc_macro_derive(Merge, attributes(merge))] | ||
pub fn merge(body: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(body as DeriveInput); | ||
match &input.data { | ||
Data::Struct(DataStruct { fields, .. }) => expand::struct_fields(&input, fields).into(), | ||
Data::Enum(_) => error(Span::call_site(), "enums are not supported with #[derive(Merge)]"), | ||
Data::Union(_) => error(Span::call_site(), "unions are not supported with #[derive(Merge)]"), | ||
} | ||
} | ||
|
||
fn error<T: Display>(span: Span, msg: T) -> TokenStream { | ||
syn::Error::new(span, msg).into_compile_error().into() | ||
} |
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,28 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#[test] | ||
fn ui() { | ||
let cases = trybuild::TestCases::new(); | ||
cases.compile_fail("./tests/ui/union.rs"); | ||
cases.compile_fail("./tests/ui/enum.rs"); | ||
cases.pass("./tests/ui/struct.rs"); | ||
} |
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,25 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#[derive(noelware_config_derive::Merge)] | ||
enum Heck {} | ||
|
||
fn main() {} |
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,7 @@ | ||
error: enums are not supported with #[derive(Merge)] | ||
--> ./tests/ui/enum.rs:22:10 | ||
| | ||
22 | #[derive(noelware_config_derive::Merge)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the derive macro `noelware_config_derive::Merge` (in Nightly builds, run with -Z macro-backtrace for more info) |
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,40 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
use noelware_config::merge::Merge; | ||
|
||
#[derive(Debug, noelware_config_derive::Merge)] | ||
struct Something { | ||
a: String, | ||
} | ||
|
||
fn main() { | ||
let mut a = Something { | ||
a: String::from("weow"), | ||
}; | ||
|
||
let b = Something { | ||
a: String::from("heck"), | ||
}; | ||
|
||
a.merge(b); | ||
dbg!(a); | ||
} |
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,29 @@ | ||
// 🐻❄️🪚 core-rs: Collection of Rust crates that are used by and built for Noelware's projects | ||
// Copyright (c) 2024 Noelware, LLC. <[email protected]> | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#[repr(C)] | ||
#[derive(noelware_config_derive::Merge)] | ||
union MyUnion { | ||
f1: u32, | ||
f2: f32, | ||
} | ||
|
||
fn main() {} |
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,7 @@ | ||
error: unions are not supported with #[derive(Merge)] | ||
--> ./tests/ui/union.rs:23:10 | ||
| | ||
23 | #[derive(noelware_config_derive::Merge)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the derive macro `noelware_config_derive::Merge` (in Nightly builds, run with -Z macro-backtrace for more info) |
Oops, something went wrong.