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

Use uncooked ident as the label #198

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
use uncooked ident as the label
KodrAus committed Sep 14, 2024
commit fc8eb10f77aa735e2d5dd9d84f9c2a8fd2992021
6 changes: 3 additions & 3 deletions buffer/src/error.rs
Original file line number Diff line number Diff line change
@@ -69,11 +69,11 @@ impl Error {
`sval_serde`, then you can enable their `alloc` or `std` features instead.
*/

#[cfg(all(debug_assertions, not(no_debug_assertions), not(test)))]
#[cfg(all(debug_assertions, not(feature = "no_debug_assertions"), not(test)))]
{
panic!("attempt to allocate for {} would fail; add the `alloc` feature of `sval_buffer` or the depdendent `sval_*` library to support allocation. This call will error instead of panicking in release builds. Add the `no_debug_assertions` feature of `sval_buffer` if this error is expected.", method);
panic!("attempt to allocate for {} would fail; add the `alloc` feature of `sval_buffer` or the depdendent `sval_*` library to support allocation. This call will error instead of panicking in release builds. Add the `feature = no_debug_assertions` feature of `sval_buffer` if this error is expected.", method);
}
#[cfg(not(all(debug_assertions, not(no_debug_assertions), not(test))))]
#[cfg(not(all(debug_assertions, not(feature = "no_debug_assertions"), not(test))))]
{
Error(ErrorKind::NoAlloc { method })
}
56 changes: 56 additions & 0 deletions derive/test/lib.rs
Original file line number Diff line number Diff line change
@@ -26,6 +26,26 @@ mod derive_struct {
})
}

#[test]
fn uncooked() {
#[derive(Value)]
struct RecordTuple {
r#type: i32,
}

assert_tokens(&RecordTuple { r#type: 42 }, {
use sval_test::Token::*;

&[
RecordTupleBegin(None, Some(sval::Label::new("RecordTuple")), None, Some(1)),
RecordTupleValueBegin(None, sval::Label::new("type"), sval::Index::new(0)),
I32(42),
RecordTupleValueEnd(None, sval::Label::new("type"), sval::Index::new(0)),
RecordTupleEnd(None, Some(sval::Label::new("RecordTuple")), None),
]
})
}

#[test]
fn generic() {
#[derive(Value)]
@@ -608,6 +628,19 @@ mod derive_unit_struct {
&[Tag(None, Some(sval::Label::new("Tag")), None)]
})
}

#[test]
#[allow(non_camel_case_types)]
fn uncooked() {
#[derive(Value)]
struct r#type;

assert_tokens(&r#type, {
use sval_test::Token::*;

&[Tag(None, Some(sval::Label::new("type")), None)]
})
}
}

mod derive_enum {
@@ -707,6 +740,29 @@ mod derive_enum {
});
}

#[test]
#[allow(non_camel_case_types)]
fn uncooked() {
#[derive(Value)]
enum Enum {
r#type,
}

assert_tokens(&Enum::r#type, {
use sval_test::Token::*;

&[
EnumBegin(None, Some(sval::Label::new("Enum")), None),
Tag(
None,
Some(sval::Label::new("type")),
Some(sval::Index::new(0)),
),
EnumEnd(None, Some(sval::Label::new("Enum")), None),
]
});
}

#[test]
fn unlabeled() {
#[derive(Value)]
4 changes: 2 additions & 2 deletions derive_macros/src/label.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use syn::Ident;
use syn::{ext::IdentExt, Ident};

#[derive(Debug, Clone)]
pub(crate) enum LabelValue {
@@ -22,7 +22,7 @@ fn explicit_label(explicit: LabelValue) -> Label {

fn ident_label(ident: &Ident) -> Label {
Label::Implicit({
let ident = ident.to_string();
let ident = ident.unraw().to_string();
quote!(#ident)
})
}