Skip to content

Commit

Permalink
use uncooked ident as the label
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Sep 14, 2024
1 parent e2dba59 commit fc8eb10
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
6 changes: 3 additions & 3 deletions buffer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down
56 changes: 56 additions & 0 deletions derive/test/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)]
Expand Down
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 {
Expand All @@ -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)
})
}
Expand Down

0 comments on commit fc8eb10

Please sign in to comment.