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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add From<&str> implementation for types using deserialize_from_str! #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gibbz00
Copy link

@gibbz00 gibbz00 commented May 5, 2024

Writing an authorization server which needs to be able to parse CoreResponseType from an HTTP query string 馃槉

Went for `From` as it allows adding "public" functions which do not
require any explicit documentation up front with `deny(missing_docs)`.
That may be added later.
@ramosbugs
Copy link
Owner

Thanks for the PR!

I'm generally wary of From<&str> since it encourages stringly-typed code that can lead to bugs in user code. For example, consider a function:

fn foo(client_id: ClientId, client_secret: ClientSecret) { ... }

If both ClientId and ClientSecret were to implement From<&str> (neither currently does), then the following code will compile fine:

foo("my_secret".into(), "my_id".into())

However, the arguments are in the wrong order, which is not at all obvious from looking at the call site. Instead, the current interface requires:

foo(ClientId::new("my_secret".to_string()), ClientSecret::new("my_id".to_string()))

(which still has the same bug, but it's a lot more obvious while looking at the code).

To avoid this issue, the newtypes throughout this crate implement an explicit new() constructor instead of From<&str>.

The same concern about .into() applies to enums, but new() doesn't feel like the right name for an explicit constructor. Instead, I think we should just make the existing from_str() methods public, which should support your use case without introducing the .into() pitfalls. I'd be happy to merge that change.

@gibbz00
Copy link
Author

gibbz00 commented May 9, 2024

Hi!

Thanks for the well-put response. I see what you mean and it makes sense. The reason I chose to not simply make the from_str public, is because of the deny(missing_docs), as mentioned in the commit message 馃槉 How do you think we should go about this?

  • Add #[allow(missing_docs)] to each method and put off proper documentation for later?
  • Add documentation to each method? May require referencing specific specification sections.

@ramosbugs
Copy link
Owner

I would just add a generic doc message like "Construct a new <name of type>"

No need to reference the specs, which I think would be more appropriate in the type's top-level documentation rather than its constructor's documentation.

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

2 participants