Correct idiomatic way to make this DRY?

I have two from_json() functions that are doing the same thing but are different in return type. It seems like there should be a way to avoid duplication, if its not terribly complex.

#[derive(Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BasicWalletConfig {
    #[serde(rename = "fileName")]
    pub file_name: String,
    pub reset: Option<bool>
}

impl BasicWalletConfig {
    pub fn from_json(json: &str) -> Result<BasicWalletConfig, Error> {
        let config: BasicWalletConfig = serde_json::from_str(json)?;
        Ok(config)
    }
}

#[derive(Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IndyWalletConfig {
    pub seed: string,
    pub pool_file_name: string
}

impl IndyWalletConfig {
    pub fn from_json(json: &str) -> Result<IndyWalletConfig, Error> {
        let config: IndyWalletConfig = serde_json::from_str(json)?;
        Ok(config)
    }
}

First of all please follow the forum's code formatting guideline and indent the code properly.

Yes, it's serde_json::from_str(). Why have you made those function which does nothing more than it?

1 Like

The use case would be a consumer wouldn't know what type of wallet it is using. There is another trait and types and enums for wallet behaviors.

That being said I could probably still get rid of it. Im not expecting the consumer to build the config type. Thnx.