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

Conversation

RCasatta
Copy link
Collaborator

@RCasatta RCasatta commented Jun 1, 2021

This would allow to ser/des RpcConfig downstream struct

Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK, but we should discuss the representation.

@@ -187,7 +187,7 @@ impl RawTx for String {
}

/// The different authentication methods for the client.
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub enum Auth {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be worth it implementing the serde logic manually because none of the available tagging options feel right. The default, external tagging, would result in:

"auth_field": {
  "UserPass": ["user", "pass"]
}

or

"auth_field": {
  "CookieFile": "/path/to/file"
}

I think we should at least add variant renaming to snake_case. But ideally it would look like this imo:

"auth_field": {
  "user": "user",
  "pass": "pass"
}

or

"auth_field": {
  "cookie_file": "/path/to/file"
}

We could achieve this with untagged serialization and changing the tuple variants to struct variants, but that would be an API break and would make direct usage more cumbersome. We could add constructors to ease the pain, but the API break would remain.

@RCasatta
Copy link
Collaborator Author

@sgeisler I updated the PR to serialize in a more comprehensible way and at the same time trying to be non-breaking and easily upgradable

we may want to add #[serde(untagged)] but I slightly prefer the default

@RCasatta RCasatta requested a review from sgeisler June 23, 2021 08:41
Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without #[serde(untagged)] it still seems unnecessarily deeply nested 😕 I think with #[serde(untagged)] and the CookieFile variant becoming a named struct instead of a tuple struct that might be quite nice. If you feel strongly about it I'm ok with this too though.

Also, if we were feeling really fancy we'd avoid the clone on serializing by having two structs: one owned and one borrowed.

@RCasatta
Copy link
Collaborator Author

Without #[serde(untagged)] it still seems unnecessarily deeply nested 😕 I think with #[serde(untagged)] and the CookieFile variant becoming a named struct instead of a tuple struct that might be quite nice. If you feel strongly about it I'm ok with this too though.

I am not feeling strongly about it and I added #[serde(untagged)] (but I wouldn't say "unnecessarily deeply nested" since there are reasons to prefer it...)
I also added the named variant for Cookie

Also, if we were feeling really fancy we'd avoid the clone on serializing by having two structs: one owned and one borrowed.

I don't think the code complexity is worth it in this case since serialize/deserialize of this value is done rarely

Copy link
Contributor

@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 4512d5d

but I wouldn't say "unnecessarily deeply nested" since there are reasons to prefer it...

Sorry for that wording, I just didn't understand it 😄 I guess there are some problems if a parent enum is untagged too and you have collisions in field names (just don't do that 😆 ). But since this is to be used in config (I suppose) and I as a user tend to get annoyed when I have to write seemingly redundant information I think this version is better.

I don't think the code complexity is worth it in this case since serialize/deserialize of this value is done rarely

Very good point.

@stevenroose
Copy link
Collaborator

Sorry for the delay guys.. It's a bit unfortunate that we have to clone stuff for this. I don't think it's too hard to implement serde manually without the need for an extra struct to copy into.

Even though the only problem with just doing this on our existing struct is that user and pass are a slice. Which is also not the end of the world:

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Auth {
    None,
    UserPass(String, String),
    CookieFile(PathBuf),
}

which gives

{"auth":"none"}
{"auth":{"user_pass":["userr","passs"]}}
{"auth":{"cookie_file":"/"}}

@stevenroose
Copy link
Collaborator

Just for curiosity, I tried it manually. It ends up being quite verbose like always using the visitor etc. But it works without re-allocations for serializing: stevenroose@04dd65b

I'm fine also just merging this if that's preferred.

@RCasatta
Copy link
Collaborator Author

RCasatta commented Jul 9, 2021

I am fine also with stevenroose/rust-bitcoincore-rpc@04dd65b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants