-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Request for Enhancement: Support for top-level "add-ons" for map #213
Comments
Understood, this does seem a little tedious. Not sure I'm in favor of a default map function, it would only be able to accept a single type? What happens to the Have you considered the alternative of splitting it out into a separate struct to hide some of these details? use deku::prelude::*;
use std::convert::TryFrom;
#[derive(Debug, DekuRead)]
pub struct H {
a: u16,
#[deku(bytes = "4")]
b: MorphedString,
}
#[derive(Debug, DekuRead)]
#[deku(ctx = "bytes: deku::ctx::Size")]
pub struct MorphedString(#[deku(map = "morph", bytes_read = "bytes.byte_size().unwrap()")] String);
pub fn morph(bytes: &[u8]) -> Result<String, DekuError> {
let str = std::str::from_utf8(&bytes).unwrap().to_string();
Ok(str)
}
fn main() {
let input = b"\xAB\xCDDEKU";
let the_h = H::try_from(input.as_ref()).unwrap();
dbg!(&the_h);
assert_eq!(the_h.a, 0xCDAB);
assert_eq!(the_h.b.0, "DEKU");
} You could implement Deref, PartialEq, etc. to make this type more transparent. Possibly related: #174 |
Yes, ideally configurable in a top-level setting such as
That'll be processed as normal; in this case, It will be really excellent to not have to code the
Obviously, I'm biased, but simplicity will surely help with adoption. |
Keeping it general... Top-level functions:
#[deku(endian="little", map="morph", map_types="String", map_sizes="tuple")]
pub struct H {
a: u16,
b: String,
c: String,
d: String,
e: String,
} |
I feel like a top-level Is there any reasons why you'd rather this approach vs attributes on each field? I feel like this is adding a unnecessary level on indirection with not much benefit. I'm not convinced of this addition. I believe the current attributes provide enough flexibility while keeping things explicit. I recommend explicitly on each field via the attributes, or abstracted as described above. I'm open to discuss further. |
Hi, perhaps something like rust-lang/rust#83366 can help with the implementation.
This is the case though... for all
It's just to have a single place to provide
Overall, it's no doubt that as the lib exists currently, it works. Before/After examples in this comment - #213 (comment) |
Any further thoughts on this, @sharksforarms? |
Similar to #217 (comment) I rather avoid having multiple ways of accomplishing the same thing, this sort of abstraction could be done in a separate proc-macro crate. |
Please consider adding support for custom encodings.
In #212, what I've described is the conversion of bytes from one encoding into String.
The
morph
referred there is essentially a codepage conversion function.If codepage can be added as a top-level attribute, that'll save me loads & loads of LoC.
That is, if the codepage conversion can optionally be applied to all
struct
fields that areString
.EDIT: A simple addition will be to say, 'if you supply your own codepage conversion function, then deku will allow the use a top-level setting to point to that function'.
Thanks again for this excellent library and all the work from the contributors!
The text was updated successfully, but these errors were encountered: