Skip to content
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

Add serde traits to Auth #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 74 additions & 0 deletions client/src/client.rs
Expand Up @@ -194,6 +194,71 @@ pub enum Auth {
CookieFile(PathBuf),
}

/// [AuthNew] is used to serialize [Auth] in a more comprensible way, once a breaking release is
/// made it could replace [Auth], removing `From` and custom serialization implementation
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(untagged)]
enum AuthNew {
None,
UserPass {
username: String,
password: String,
},
Cookie {
file: PathBuf
},
}

impl From<Auth> for AuthNew {
fn from(auth: Auth) -> Self {
match auth {
Auth::None => AuthNew::None,
Auth::UserPass(username, password) => AuthNew::UserPass {
username,
password,
},
Auth::CookieFile(file) => AuthNew::Cookie{ file },
}
}
}

impl From<AuthNew> for Auth {
fn from(auth_new: AuthNew) -> Self {
match auth_new {
AuthNew::None => Auth::None,
AuthNew::UserPass {
username,
password,
} => Auth::UserPass(username, password),
AuthNew::Cookie { file } => Auth::CookieFile(file),
}
}
}

impl serde::Serialize for Auth {
fn serialize<S>(
&self,
serializer: S,
) -> std::result::Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where
S: serde::Serializer,
{
AuthNew::from(self.clone()).serialize(serializer)
}
}

impl<'de> serde::Deserialize<'de> for Auth {
fn deserialize<D>(
deserializer: D,
) -> std::result::Result<Self, <D as serde::Deserializer<'de>>::Error>
where
D: serde::Deserializer<'de>,
{
Ok(AuthNew::deserialize(deserializer)?.into())
}
}

impl Auth {
/// Convert into the arguments that jsonrpc::Client needs.
fn get_user_pass(self) -> Result<(Option<String>, Option<String>)> {
Expand Down Expand Up @@ -1233,4 +1298,13 @@ mod tests {
fn test_handle_defaults() {
test_handle_defaults_inner().unwrap();
}

#[test]
fn test_serde_auth() {
let auth = Auth::UserPass("mario".to_string(), "1234".to_string());
let ser = serde_json::to_string(&auth).unwrap();
assert_eq!("{\"username\":\"mario\",\"password\":\"1234\"}", ser);
let des: Auth = serde_json::from_str(&ser).unwrap();
assert_eq!(auth, des);
}
}
2 changes: 1 addition & 1 deletion json/src/lib.rs
Expand Up @@ -835,7 +835,7 @@ impl<'a> serde::Serialize for ImportMultiRequestScriptPubkey<'a> {
#[derive(Serialize)]
struct Tmp<'a> {
pub address: &'a Address,
};
}
serde::Serialize::serialize(
&Tmp {
address: addr,
Expand Down