Skip to content

Commit

Permalink
fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jewlexx committed Dec 11, 2024
2 parents 3faca18 + 829a5e9 commit ab36c94
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 28 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
name = "quork"
repository = "https://github.com/jewlexx/quork.git"
rust-version = "1.69.0"
version = "0.7.1"
version = "0.8.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -20,12 +20,13 @@ members = ["quork-proc"]
[dependencies]
cfg-if = "1.0"
lock_api = { version = "0.4", optional = true }
parking_lot = { version = "0.12", optional = true }
quork-proc = { version = "0.3", path = "quork-proc", optional = true }
spin = { version = "0.9", optional = true }
thiserror = { version = "1.0" }
thiserror = { version = "2.0" }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.56", features = [
windows = { version = "0.58", features = [
"Win32_Foundation",
"Win32_Networking_NetworkListManager",
"Win32_Security",
Expand All @@ -35,7 +36,7 @@ windows = { version = "0.56", features = [
] }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28", features = ["user"] }
nix = { version = "0.29", features = ["user"] }

[features]
all = ["macros", "network", "root", "std", "traits", "sized_string"]
Expand All @@ -48,7 +49,7 @@ std = []
traits = []

[build-dependencies]
cc = "1.0"
cc = "1.2"

[dev-dependencies]
is-root = "0.1"
17 changes: 10 additions & 7 deletions quork-proc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "quork-proc"
license = "MIT"
version = "0.3.1"
description = "The proc macros for quork (not intended for independant use)"
authors = ["Juliette Cordor"]
description = "The proc macros for quork (not intended for independant use)"
documentation = "https://docs.rs/quork"
repository = "https://github.com/jewlexx/quork.git"
homepage = "https://github.com/jewlexx/quork.git"
edition = "2018"
homepage = "https://github.com/jewlexx/quork.git"
license = "MIT"
name = "quork-proc"
repository = "https://github.com/jewlexx/quork.git"
version = "0.3.2"

[lib]
proc-macro = true
Expand All @@ -16,7 +16,10 @@ proc-macro = true

[dependencies]
proc-macro-crate = "3.1"
proc-macro-error = "1.0"
proc-macro-error2 = "2.0"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "2.0", features = ["full"] }

[dev-dependencies]
strum = { version = "0.26", features = ["derive"] }
4 changes: 2 additions & 2 deletions quork-proc/src/enum_list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_error::{abort_if_dirty, emit_error};
use proc_macro_error2::{abort_if_dirty, emit_error};
use syn::{spanned::Spanned, Data, DeriveInput};

use proc_macro_crate::{crate_name, FoundCrate};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn enum_list(ast: &DeriveInput) -> TokenStream {
})
.collect::<Vec<_>>()
}
_ => proc_macro_error::abort_call_site!("Can only be derived on an enum"),
_ => proc_macro_error2::abort_call_site!("Can only be derived on an enum"),
};

abort_if_dirty();
Expand Down
2 changes: 1 addition & 1 deletion quork-proc/src/from_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::Span;
use proc_macro_error::abort_call_site;
use proc_macro_error2::abort_call_site;
use syn::{DeriveInput, Ident, Type};

pub fn derive(input: &DeriveInput) -> proc_macro2::TokenStream {
Expand Down
31 changes: 19 additions & 12 deletions quork-proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,35 @@
#![warn(clippy::pedantic)]
#![warn(missing_docs)]

use proc_macro_error::proc_macro_error;
use proc_macro::TokenStream;
use proc_macro_error2::proc_macro_error;
use syn::{parse_macro_input, DeriveInput, LitStr};

mod const_str;
mod enum_list;
mod from_tuple;
mod new;
mod sized_string;
mod strip_enum;
mod time_fn;
mod trim_lines;

#[macro_use]
extern crate quote;

/// Create an additional enum with all values stripped
#[proc_macro_derive(Strip, attributes(stripped_meta, stripped))]
#[proc_macro_error]
pub fn strip_enum(input: TokenStream) -> TokenStream {
let mut ast = parse_macro_input!(input as DeriveInput);

strip_enum::strip_enum(&mut ast).into()
}

/// Implement [`quork::ListVariants`] for enums
#[proc_macro_derive(ListVariants)]
#[proc_macro_error]
pub fn derive_enum_list(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn derive_enum_list(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
enum_list::enum_list(&ast).into()
}
Expand All @@ -30,7 +41,7 @@ pub fn derive_enum_list(input: proc_macro::TokenStream) -> proc_macro::TokenStre
///
/// Converts an enum variant to a string literal, within a constant context.
#[proc_macro_derive(ConstStr)]
pub fn derive_const_str(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn derive_const_str(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
const_str::derive(&ast).into()
}
Expand All @@ -40,15 +51,15 @@ pub fn derive_const_str(input: proc_macro::TokenStream) -> proc_macro::TokenStre
/// Will follow the form of `new(field: Type, ...) -> Self`, where all fields are required.
#[proc_macro_derive(New)]
#[proc_macro_error]
pub fn derive_new(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn derive_new(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
new::derive(&ast).into()
}

/// Implement the [`std::convert::From`] trait for converting tuples into tuple structs
#[proc_macro_derive(FromTuple)]
#[proc_macro_error]
pub fn derive_from_tuple(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn derive_from_tuple(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput);
from_tuple::derive(&ast).into()
}
Expand All @@ -60,10 +71,7 @@ pub fn derive_from_tuple(input: proc_macro::TokenStream) -> proc_macro::TokenStr
/// You can pass "s", "ms", "ns"
#[proc_macro_attribute]
#[proc_macro_error]
pub fn time(
args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
pub fn time(args: TokenStream, input: TokenStream) -> TokenStream {
let args_str = args.to_string();
let fmt = match args_str.as_str() {
"ms" | "milliseconds" => time_fn::TimeFormat::Milliseconds,
Expand Down Expand Up @@ -99,7 +107,6 @@ pub fn ltrim_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {

/// Trim whitespace from the right of a string literal on each line
#[proc_macro]
#[deprecated = "Use trim_lines (renamed to avoid confusion)"]
pub fn strip_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let literal = parse_macro_input!(input as LitStr);

Expand All @@ -109,7 +116,7 @@ pub fn strip_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
/// Trim whitespace from the left and right of a string literal on each line
#[proc_macro]
#[deprecated = "Use rtrim_lines (renamed to avoid confusion)"]
pub fn rstrip_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn rstrip_lines(input: TokenStream) -> proc_macro::TokenStream {
let literal = parse_macro_input!(input as LitStr);

trim_lines::trim_lines(&literal, &trim_lines::Alignment::Right).into()
Expand All @@ -118,7 +125,7 @@ pub fn rstrip_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
/// Trim whitespace from the left of a string literal on each line
#[proc_macro]
#[deprecated = "Use ltrim_lines (renamed to avoid confusion)"]
pub fn lstrip_lines(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn lstrip_lines(input: TokenStream) -> proc_macro::TokenStream {
let literal = parse_macro_input!(input as LitStr);

trim_lines::trim_lines(&literal, &trim_lines::Alignment::Left).into()
Expand Down
2 changes: 1 addition & 1 deletion quork-proc/src/new.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use proc_macro_error::abort_call_site;
use proc_macro_error2::abort_call_site;
use syn::{Data, DeriveInput};

pub fn derive(ast: &DeriveInput) -> TokenStream {
Expand Down
172 changes: 172 additions & 0 deletions quork-proc/src/strip_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use proc_macro2::{Ident, TokenStream};
use proc_macro_error2::{abort, abort_call_site};
use quote::{quote, ToTokens};
use syn::{spanned::Spanned, DeriveInput, Meta, Variant, Visibility};

fn ignore_variant(variant: &Variant) -> bool {
variant.attrs.iter().any(|attr| match attr.meta {
syn::Meta::List(ref list) if list.path.is_ident("stripped") => {
let mut ignored = false;

let list_parser = syn::meta::parser(|meta| {
if meta.path.is_ident("ignore") {
ignored = true;
Ok(())
} else {
Err(meta.error("unsupported property"))
}
});

if let Err(err) = list.parse_args_with(list_parser) {
abort! {
err.span(),
"Failed to parse attribute: {}", err;
help = "Only supported properties on enum variants are `ignore`"
}
}

ignored
}
_ => abort!(
attr.span(),
"Expected list-style (i.e #[stripped(...)]), found other style attribute macro"
),
})
}

struct StrippedData {
ident: Ident,
variants: Vec<TokenStream>,
meta: Vec<Meta>,
vis: Visibility,
}

// struct MetaArgs {
// meta: Vec<Expr>,
// }

// impl Parse for MetaArgs {
// fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
// input.peek3(token)
// }
// }

pub fn strip_enum(ast: &mut DeriveInput) -> TokenStream {
let data = &ast.data;
let attrs = &mut ast.attrs;

let info: StrippedData = match data {
syn::Data::Enum(ref e) => {
let variants = e
.variants
.iter()
.filter_map(|variant| {
if ignore_variant(variant) {
None
} else {
Some(variant.ident.to_token_stream())
}
})
.collect::<Vec<_>>();

let default_ident = {
let ident = ast.ident.clone();
let span = ident.span();
move || Ident::new(&format!("{ident}Stripped"), span)
};

let new_ident = if let Some(info_attr_pos) = attrs
.iter()
.position(|attr| attr.path().is_ident("stripped"))
{
let info_attr = attrs.remove(info_attr_pos);

let mut new_ident: Option<Ident> = None;

let ident_parser = syn::meta::parser(|meta| {
if meta.path.is_ident("ident") {
new_ident = Some(meta.value()?.parse()?);
Ok(())
} else {
Err(meta.error("unsupported property"))
}
});

if let Err(err) = info_attr.parse_args_with(ident_parser) {
abort! {
err.span(),
"Failed to parse attribute: {}", err;
help = "Only supported properties on enum definitions are `ident`"
}
}

new_ident.unwrap_or_else(default_ident)
} else {
default_ident()
};

let meta_list: Vec<syn::Meta> = attrs
.iter()
.filter(|attr| attr.path().is_ident("stripped_meta"))
.flat_map(|meta_attr| match &meta_attr.meta {
Meta::List(meta_data) => match meta_data.parse_args::<syn::Meta>() {
Ok(meta) => vec![meta],
Err(err) => {
abort! {
err.span(),
"Failed to parse specified metadata: {}", err;
help = "Make sure the provided arguments are in the form of Rust metadata. (i.e the tokens contained within `#[...]`)"
}
}
},
// Meta::NameValue(MetaNameValue {
// value:
// syn::Expr::Lit(syn::ExprLit {
// lit: syn::Lit::Str(path),
// ..
// }),
// ..
// }) => {
// if &path.value() == "inherit" {
// attrs
// .iter()
// .filter(|attr| !attr.path().is_ident("stripped_meta"))
// .map(|attr| attr.meta.clone())
// .collect()
// } else {
// abort!(path.span(), "Expected `inherit`");
// }
// }
_ => abort!(
meta_attr.span(),
"Expected #[stripped_meta(...)]. Found other style attribute."
),
})
.collect();

StrippedData {
ident: new_ident,
variants,
meta: meta_list,
vis: ast.vis.clone(),
}
}
_ => abort_call_site!("`Strip` can only be derived for enums"),
};

let StrippedData {
ident,
variants,
meta,
vis,
} = info;

// panic!("{:?}", meta);

quote! {
#(#[#meta])*
#vis enum #ident {
#(#variants),*
}
}
}
Loading

0 comments on commit ab36c94

Please sign in to comment.