Skip to content

Type conversion rustification#18

Merged
AliSoftware merged 16 commits intotrunkfrom
type-conversion-rustification
Dec 15, 2025
Merged

Type conversion rustification#18
AliSoftware merged 16 commits intotrunkfrom
type-conversion-rustification

Conversation

@AliSoftware
Copy link
Copy Markdown
Contributor

@AliSoftware AliSoftware commented Dec 14, 2025

What

  • Refactor all the from_xyz methods to use the From<XYZ> and TryFrom<XYZ> traits, which is the idiomatic Rust way of implementing such conversion.
  • Also removed the Deref trait (which is meant only to be used to implement smart pointers with implicit/automatic dereferencing, while what we really wanted was AsRef to get a reference to the underlying array of bytes.
  • Finally, introduce enum KeySource to better represent the 3 possible supported sources for the argument to unlock (StdIn, EnvVar, ArgumentValue) with an explicit type

Review

I'd suggest to review this PR commit-by-commit, as each one tackles an individual refactor

  • 46dcee4 Deref ➡️ AsRef<[u8; Self::KEY_SIZE]>
  • 795f129 as_bytes() ➡️ as_ref()
  • 591831f from_bytes() ➡️ From<[u8; Self::KEY_SIZE]>
  • bf44215 bytes_to_key() ➡️ TryFrom<&[u8]>
  • cd57f4c from_file() ➡️ TryFrom<&Path>
  • ed4dd42 from_base64() ➡️ TryFrom<&str>
  • 43e400b to_base64() ➡️ Display
  • 52c583a Repo::from_repository ➡️ TryFrom<Repository>
  • Introduce enum KeySource type to represent the different sources supported for the key in the unlock command
    • a2278a3 Move read_from_source() from key.rs to commands/unlock.rs, since it's related to parsing command line arguments
    • f406ef1 Replace read_from_source() with dedicated enum KeySource to use as cmd_unlock parameter type; then impl FromStr for KeySource to implement string-representation parsing semantics, and impl From<KeySource> for Key to build/load a Key from a KeySource variant.

This was also the occasion to move some stuff around:

  • 43f6cbc Reorder Key unit tests, so that they are grouped in the same order as the methods they are testing
  • 88bf3f9 Make Key implement PartialEq, Eq (which makes assert_eq! easier to use in tests in particular)
  • c781bd4 use crate::key ➡️ use crate::key::Key, so we can just use Key instead of key::Key in code
  • b7873ba use crate::repo ➡️ use crate::repo::Repo, so we can just use Repo instead of repo::Repo in code
  • f1c9846 Use borrow for function parameters (instead of ownership transfer)

Testing

CI being green should be enough for testing this

The use of `Deref` was incorrect, as this trait is meant to implement smart pointers and get support for implicit coercion / automatic deref.
What we really wanted was to use `AsRef`, and require explicit `.as_ref()` conversions as call site when needed.
Now that we have `AsRef<[u8; Self::KEY_SIZE]>`, we don't really need `as_bytes()` anymore, since `as_ref()` already covers it.
Since this method is strongly tied to reading a key from the right source (stdin, env, param) for the `cmd_unlock` command, and thus strongly tied to the command line parsing, this make more sense to put this in `commands/unlock.rs` rather than as a method on the `key::Key` model object.
To avoid having to refer to it as `key::Key` in the implementations every time
For consistency with the order in which the methods and traits are declared in the type implementation and group them by theme
@AliSoftware AliSoftware self-assigned this Dec 14, 2025
@AliSoftware AliSoftware requested a review from iangmaia December 14, 2025 13:14
To avoid having to refer to it as `repo::Repo` in the implementations every time
e.g. most functions that do something with a `Key` need only to borrow the `Key` (e.g. to print its value) without taking ownership of it.
Same for functions that take should take `&str` instead of `String` to only borrow it
To make tests easier to write, and because it is idiomatic and makes semantic sense for such a wrapper type anyway
 - Allows the `#[command] Unlock`'s `#[arg] key_source: KeySource` to be more explicit in its type
 - Uses `impl std::str::FromStr for KeySource` to parse `"-"`/`"env:…"`/`"…"` strings into the right `KeySource` variant (which is more semantically correct than using `From<&str>` here because this is semantically more about parsing/interpreting an argument than about conversion)
 - Use `impl From<KeySource> for Key` to build/load a `Key` from a `KeySource`
Copy link
Copy Markdown
Contributor

@iangmaia iangmaia left a comment

Choose a reason for hiding this comment

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

✨ 👍

@AliSoftware AliSoftware merged commit 64899af into trunk Dec 15, 2025
4 checks passed
@AliSoftware AliSoftware deleted the type-conversion-rustification branch December 15, 2025 19:37
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