I want to get the pretty print in Rust for ([u8; 32]), but can't make it build successfully. Especially I'm confused by the 1st build error at line 10: "expected array of 32 elements". Could somebody help?
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use serde_json::Error;
use serde::{Serializer, Deserialize, Deserializer};
#[derive(Serialize, Deserialize)]
struct OneFactor(
#[serde(serialize_with = "buffer_to_hex", deserialize_with = "hex_to_buffer")]
[u8; 32]
);
...
And I put the full code into the Rust Playground for your easier check.
The build errors:
Compiling playground v0.0.1 (file:///playground)
error[E0308]: match arms have incompatible types
--> src/main.rs:10:21
|
10 | #[derive(Serialize, Deserialize)]
| -----------
| |
| expected array of 32 elements, found struct `std::vec::Vec`
| match arm with an incompatible type
| in this macro invocation
|
= note: expected type `[u8; 32]`
found type `std::vec::Vec<u8>`
error[E0308]: match arms have incompatible types
--> src/main.rs:10:21
|
10 | #[derive(Serialize, Deserialize)]
| -----------
| |
| expected array of 32 elements, found struct `std::vec::Vec`
| match arm with an incompatible type
| in this macro invocation
|
= note: expected type `[u8; 32]`
found type `std::vec::Vec<u8>`
error[E0599]: no method named `to_hex` found for type `&[u8]` in the current scope
--> src/main.rs:21:45
|
21 | serializer.serialize_str(&buffer.as_ref().to_hex())
| ^^^^^^
|
= help: did you mean `to_vec`?
error[E0599]: no function or associated item named `from_hex` found for type `std::vec::Vec<_>` in the current scope
--> src/main.rs:30:24
|
30 | .and_then(|string| Vec::from_hex(&string).map_err(|err| Error::custom(err.to_string())))
| ^^^^^^^^^^^^^ function or associated item not found in `std::vec::Vec<_>`
error: aborting due to 4 previous errors