-
Notifications
You must be signed in to change notification settings - Fork 73
Implement FromStr for Language to be able to convert lang string to Language #99
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
base: master
Are you sure you want to change the base?
Conversation
tnull
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do you have these particular strings from exactly?
This feels generally out-of-scope for this crate, but in particular we don't want to use FromStr here as there could be multiple possibilities how to translate a string to a Language.
|
That is a valid concern regarding the scope and ambiguity of string parsing. The strings I used (e.g., "English", "Spanish") are the official, human-readable names of the languages already exposed by the Language enum variants themselves. They are not arbitrary translations.
If we only accept the exact, canonical name of the language (e.g., Language::ChineseSimplified maps only to "ChineseSimplified" or a canonical case-insensitive version like "chinese-simplified"), then ambiguity is eliminated. We are not aiming to support complex localization strings, only the name of the variant itself.
The ability to easily instantiate a core type like Language from its common string representation is a fundamental API feature. Since the names are intrinsic to the standard, providing a single, consistent FromStr implementation is a small, high-value API addition that prevents N-times boilerplate across every downstream wallet implementation. It keeps the parsing logic centralized and correct. If using the full name is deemed too long, an alternative could be to only support the three-letter language codes (e.g., "eng", "spa") often used in cryptocurrency standards, and implement FromStr for those. This is much less ambiguous than full names. |
Again, I'd like to ask you to reference where exactly you got the particular strings from. Are you talking about ISO639? What does 'official' mean here?
Not quite sure I understand why string parsing is fundamental to this library.
Well, again, different consumers of this library might have different ideas which kinds of strings they want to parse to a |
|
I appreciate the continued focus on clarity and scope. Your questions are valid, and my implementation addresses them directly: 1. Source of "Official" Strings Full Names: The strings like "english" and "chinese-simplified" are the exact snake_case names of the wordlists as defined in the official BIP-39 standard and implemented in this crate's API (the Language variant names). This is the primary, unambiguous source. Short Codes: The codes like "en", "fr", and "zh-cn" are drawn from ISO standards (ISO 639-1/3 and BCP 47 language tags) which are the universally accepted way to reference these languages in software and protocols. By supporting both, we map the two most common and canonical string representations consumers will encounter directly to our internal type. 2. Why FromStr is Fundamental The library defines the Language enum and, therefore, has the sole responsibility for knowing the definitive string(s) that map to it. This minimal FromStr implementation is a high-value API feature because it:
If a consumer has an idiosyncratic input (e.g., a fully localized language name), they only need to map that one string to one of the canonical names/codes once, and then rely on the official FromStr implementation provided here. |
Implement FromStr for Language to be able to convert lang string to Language