Skip to content

Commit

Permalink
Use Option::insert() (#729)
Browse files Browse the repository at this point in the history
## Type of change

<!-- (mark with an `X`) -->

```
- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective
Just discovered that `Option::insert` is a thing, which simplifies some
code that first sets the value and then immediately returns a reference
to it.
  • Loading branch information
dani-garcia authored Apr 23, 2024
1 parent dc4ad16 commit ee463ec
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions crates/bitwarden/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,11 @@ impl Client {
user_key: EncString,
private_key: EncString,
) -> Result<&EncryptionSettings> {
self.encryption_settings =
Some(EncryptionSettings::new(master_key, user_key, private_key)?);
Ok(self
.encryption_settings
.as_ref()
.expect("Value is initialized previously"))
Ok(self.encryption_settings.insert(EncryptionSettings::new(
master_key,
user_key,
private_key,
)?))
}

#[cfg(feature = "internal")]
Expand All @@ -267,14 +266,12 @@ impl Client {
user_key: SymmetricCryptoKey,
private_key: EncString,
) -> Result<&EncryptionSettings> {
self.encryption_settings = Some(EncryptionSettings::new_decrypted_key(
user_key,
private_key,
)?);
Ok(self
.encryption_settings
.as_ref()
.expect("Value is initialized previously"))
.insert(EncryptionSettings::new_decrypted_key(
user_key,
private_key,
)?))
}

#[cfg(feature = "mobile")]
Expand All @@ -292,10 +289,8 @@ impl Client {
&mut self,
key: SymmetricCryptoKey,
) -> &EncryptionSettings {
self.encryption_settings = Some(EncryptionSettings::new_single_key(key));
self.encryption_settings
.as_ref()
.expect("Value is initialized previously")
.insert(EncryptionSettings::new_single_key(key))
}

#[cfg(feature = "internal")]
Expand Down

0 comments on commit ee463ec

Please sign in to comment.