Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DigestAs functionality #12

Merged
merged 15 commits into from
Aug 26, 2024
5 changes: 3 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
env:
CARGO_TERM_COLOR: always
CARGO_NET_GIT_FETCH_WITH_CLI: true
RUSTFLAGS: -D warnings

jobs:
check-and-test:
Expand All @@ -20,9 +21,9 @@ jobs:
with:
cache-on-failure: "true"
- name: Check
run: cargo check -p udigest --features "${{ matrix.features }}"
run: cargo check -p udigest --no-default-features --features "${{ matrix.features }}"
- name: Run tests
run: cargo test -p udigest --lib --tests --features "${{ matrix.features }}"
run: cargo test -p udigest --lib --tests --no-default-features --features "${{ matrix.features }}"
test-all-features:
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions udigest-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.3.0
* Add `#[udigest(as = ...)]` attribute support [#12]

[#12]: https://github.com/dfns/udigest/pull/12

## v0.2.0
* Fix proc macro causing clippy warnings in certain cases [#6]

Expand Down
2 changes: 1 addition & 1 deletion udigest-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "udigest-derive"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "Proc macro for `udigest` crate"
license = "MIT OR Apache-2.0"
Expand Down
57 changes: 38 additions & 19 deletions udigest-derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub enum Attr {
Skip(Skip),
Rename(Rename),
With(With),
As(As),
}

impl Attr {
Expand All @@ -30,6 +31,7 @@ impl Attr {
Attr::Skip(attr) => attr.skip.span,
Attr::Rename(attr) => attr.rename.span,
Attr::With(attr) => attr.with.span,
Attr::As(attr) => attr.as_.span,
}
}
}
Expand All @@ -51,6 +53,8 @@ impl syn::parse::Parse for Attr {
Rename::parse(input).map(Attr::Rename)
} else if lookahead.peek(kw::with) {
With::parse(input).map(Attr::With)
} else if lookahead.peek(syn::Token![as]) {
As::parse(input).map(Attr::As)
} else {
Err(lookahead.error())
}
Expand All @@ -59,7 +63,7 @@ impl syn::parse::Parse for Attr {

pub struct Root {
pub root: kw::root,
pub eq: syn::Token![=],
pub _eq: syn::Token![=],
pub path: RootPath,
}

Expand All @@ -68,68 +72,68 @@ pub type RootPath = syn::punctuated::Punctuated<syn::Ident, syn::Token![::]>;
impl syn::parse::Parse for Root {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let root = input.parse::<kw::root>()?;
let eq = input.parse::<syn::Token![=]>()?;
let _eq = input.parse::<syn::Token![=]>()?;
let path = RootPath::parse_separated_nonempty_with(input, syn::Ident::parse_any)?;

Ok(Self { root, eq, path })
Ok(Self { root, _eq, path })
}
}

pub struct Tag {
pub tag: kw::tag,
pub eq: syn::Token![=],
pub _eq: syn::Token![=],
pub value: syn::Expr,
}

impl syn::parse::Parse for Tag {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let tag = input.parse()?;
let eq = input.parse()?;
let _eq = input.parse()?;
let value = input.parse()?;

Ok(Self { tag, eq, value })
Ok(Self { tag, _eq, value })
}
}

pub struct AsBytes {
pub as_bytes: kw::as_bytes,
pub eq: Option<syn::Token![=]>,
pub _eq: Option<syn::Token![=]>,
pub value: Option<syn::Expr>,
}

impl syn::parse::Parse for AsBytes {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let as_bytes = input.parse()?;
let mut eq = None;
let mut _eq = None;
let mut value = None;

let lookahead = input.lookahead1();
if lookahead.peek(syn::Token![=]) {
eq = Some(input.parse()?);
_eq = Some(input.parse()?);
value = Some(input.parse()?);
}

Ok(Self {
as_bytes,
eq,
_eq,
value,
})
}
}

pub struct Bound {
pub bound: kw::bound,
pub eq: syn::Token![=],
pub _eq: syn::Token![=],
pub value: syn::LitStr,
}

impl syn::parse::Parse for Bound {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let bound = input.parse()?;
let eq = input.parse()?;
let _eq = input.parse()?;
let value = input.parse()?;

Ok(Self { bound, eq, value })
Ok(Self { bound, _eq, value })
}
}

Expand All @@ -146,31 +150,46 @@ impl syn::parse::Parse for Skip {

pub struct Rename {
pub rename: kw::rename,
pub eq: syn::Token![=],
pub _eq: syn::Token![=],
pub value: syn::Expr,
}

impl syn::parse::Parse for Rename {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let rename = input.parse()?;
let eq = input.parse()?;
let _eq = input.parse()?;
let value = input.parse()?;

Ok(Self { rename, eq, value })
Ok(Self { rename, _eq, value })
}
}

pub struct With {
pub with: kw::with,
pub eq: syn::Token![=],
pub _eq: syn::Token![=],
pub value: syn::Expr,
}

impl syn::parse::Parse for With {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let with = input.parse()?;
let eq = input.parse()?;
let _eq = input.parse()?;
let value = input.parse()?;
Ok(Self { with, eq, value })
Ok(Self { with, _eq, value })
}
}

pub struct As {
pub as_: syn::Token![as],
pub _eq: syn::Token![=],
pub value: syn::Type,
}

impl syn::parse::Parse for As {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let as_ = input.parse()?;
let _eq = input.parse()?;
let value = input.parse()?;
Ok(Self { as_, _eq, value })
}
}
Loading
Loading