Skip to content

Commit

Permalink
add 2 new crates: noelware-config & noelware-remi, fix noelware-serde…
Browse files Browse the repository at this point in the history
… doctests
  • Loading branch information
auguwu committed Jan 14, 2024
1 parent aee6ed6 commit 94059ea
Show file tree
Hide file tree
Showing 23 changed files with 1,279 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

[workspace]
resolver = "2"
members = ["crates/log", "crates/serde"]
members = ["crates/*"]

[workspace.package]
version = "0.1.0"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
## Why aren't you publishing these crates to crates.io?
Because, it felt unneccessary to upload and pollute crates.io with Rust crates that are the scope of Noelware's software projects itself, while you're free to use them and grab the code for library-facing code (with the license attached), since this repository is released under the **MIT License**.

If you don't wish to import from `cargo.noelware.cloud`, then you can just use a Git import instead of the commit hash you need. We don't mind!

## Are you going to do this to your public crates already on `crates.io`?
No. We don't plan on moving our public-facing crates like [`remi-rs`](https://github.com/Noelware/remi-rs) to our Cargo registry.

Expand Down
44 changes: 44 additions & 0 deletions crates/config-derive/Cargo.toml
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"
58 changes: 58 additions & 0 deletions crates/config-derive/src/args.rs
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)
}
}
85 changes: 85 additions & 0 deletions crates/config-derive/src/expand.rs
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)),
})
}
44 changes: 44 additions & 0 deletions crates/config-derive/src/lib.rs
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()
}
28 changes: 28 additions & 0 deletions crates/config-derive/tests/ui.rs
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");
}
25 changes: 25 additions & 0 deletions crates/config-derive/tests/ui/enum.rs
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() {}
7 changes: 7 additions & 0 deletions crates/config-derive/tests/ui/enum.stderr
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)
40 changes: 40 additions & 0 deletions crates/config-derive/tests/ui/struct.rs
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);
}
29 changes: 29 additions & 0 deletions crates/config-derive/tests/ui/union.rs
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() {}
7 changes: 7 additions & 0 deletions crates/config-derive/tests/ui/union.stderr
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)
Loading

0 comments on commit 94059ea

Please sign in to comment.