Skip to content

Conversation

@volodymyrd
Copy link

@volodymyrd volodymyrd commented Nov 12, 2025

Implement FromStr for Language to be able to convert lang string to Language

@volodymyrd volodymyrd marked this pull request as ready for review November 12, 2025 17:06
@volodymyrd volodymyrd changed the title Lang from str Implement FromStr for Language to be able to convert lang string to Language Nov 12, 2025
Copy link
Collaborator

@tnull tnull left a 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.

@volodymyrd
Copy link
Author

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.

  1. Regarding Ambiguity (Multiple Possibilities):

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.

  1. Regarding Scope:

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.

@tnull
Copy link
Collaborator

tnull commented Nov 13, 2025

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.

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?

The ability to easily instantiate a core type like Language from its common string representation is a fundamental API feature.

Not quite sure I understand why string parsing is fundamental to this library.

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.

Well, again, different consumers of this library might have different ideas which kinds of strings they want to parse to a Language.

@volodymyrd
Copy link
Author

I appreciate the continued focus on clarity and scope. Your questions are valid, and my implementation addresses them directly:

1. Source of "Official" Strings
The strings used are derived from two canonical sources:

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 purpose of FromStr is not general-purpose language parsing; it is canonical BIP-39 wordlist identification.

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:

  • Prevents Scattered Boilerplate: It centralizes the parsing logic, preventing every downstream wallet/CLI/API from having to rebuild the exact same 20-line if/else block.

  • Guarantees Correctness: It ensures all consumers use the same, tested mapping, reducing integration bugs.

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.

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.

2 participants