I have a struct that uses an enum, but when printing it gives the enum name and the value instead of just the value. I want to serialize it using serde_json to send as a JSON request.
I want to re-use the struct for different commands to the geth json-rpc instead of making a different struct for every type of command. That's why I thought of using an enum. But I'm doing something wrong. It might be the printing but the json-rpc says the argument is invalid as well.
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
enum Params {
String (String),
Boolean (bool)
}
#[derive(Debug, Serialize, Deserialize)]
struct EthRequest {
jsonrpc : String,
method: String,
params: [Params; 2],
id: i32
}
fn main() {
let new_eth_request = EthRequest {
jsonrpc : "2.0".to_string(),
method : "eth_getBlockByNumber".to_string(),
params : [Params::String("latest".to_string()), Params::Boolean(true)],
id : 1
};
println!("{:#?}", new_eth_request);
}
output:
EthRequest {
jsonrpc: "2.0",
method: "eth_getBlockByNumber",
params: [
String(
"latest",
),
Boolean(
true,
),
],
id: 1,
}
What I need is the params field to be params: ["latest",true]