From 17f4078a87e85eba355b11b7644203150a9cbc36 Mon Sep 17 00:00:00 2001 From: Kornel Date: Tue, 2 Apr 2024 01:33:49 +0100 Subject: [PATCH] Prefer keeping CFString --- security-framework/src/item.rs | 58 ++++++++++++++++------------------ 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/security-framework/src/item.rs b/security-framework/src/item.rs index 0c17e154..c29f2d62 100644 --- a/security-framework/src/item.rs +++ b/security-framework/src/item.rs @@ -543,49 +543,51 @@ pub struct ItemAddOptions { /// The value (by ref or data) of the item to add, required. pub value: ItemAddValue, /// Optional kSecAttrAccount attribute. - pub account_name: Option, + pub account_name: Option, /// Optional kSecAttrAccessGroup attribute. - pub access_group: Option, + pub access_group: Option, /// Optional kSecAttrComment attribute. - pub comment: Option, + pub comment: Option, /// Optional kSecAttrDescription attribute. - pub description: Option, + pub description: Option, /// Optional kSecAttrLabel attribute. - pub label: Option, + pub label: Option, /// Optional kSecAttrService attribute. - pub service: Option, + pub service: Option, /// Optional keychain location. pub location: Option, } impl ItemAddOptions { /// Specifies the item to add. - #[must_use] pub fn new(value: ItemAddValue) -> Self { + #[must_use] + pub fn new(value: ItemAddValue) -> Self { Self{ value, label: None, location: None, service: None, account_name: None, comment: None, description: None, access_group: None } } + /// Specifies the `kSecAttrAccount` attribute. - pub fn set_account_name(&mut self, account_name: impl Into) -> &mut Self { - self.account_name = Some(account_name.into()); + pub fn set_account_name(&mut self, account_name: impl AsRef) -> &mut Self { + self.account_name = Some(account_name.as_ref().into()); self } /// Specifies the `kSecAttrAccessGroup` attribute. - pub fn set_access_group(&mut self, access_group: impl Into) -> &mut Self { - self.access_group = Some(access_group.into()); + pub fn set_access_group(&mut self, access_group: impl AsRef) -> &mut Self { + self.access_group = Some(access_group.as_ref().into()); self } /// Specifies the `kSecAttrComment` attribute. - pub fn set_comment(&mut self, comment: impl Into) -> &mut Self { - self.comment = Some(comment.into()); + pub fn set_comment(&mut self, comment: impl AsRef) -> &mut Self { + self.comment = Some(comment.as_ref().into()); self } /// Specifies the `kSecAttrDescription` attribute. - pub fn set_description(&mut self, description: impl Into) -> &mut Self { - self.description = Some(description.into()); + pub fn set_description(&mut self, description: impl AsRef) -> &mut Self { + self.description = Some(description.as_ref().into()); self } /// Specifies the `kSecAttrLabel` attribute. - pub fn set_label(&mut self, label: impl Into) -> &mut Self { - self.label = Some(label.into()); + pub fn set_label(&mut self, label: impl AsRef) -> &mut Self { + self.label = Some(label.as_ref().into()); self } /// Specifies which keychain to add the item to. @@ -594,8 +596,8 @@ impl ItemAddOptions { self } /// Specifies the `kSecAttrService` attribute. - pub fn set_service(&mut self, service: impl Into) -> &mut Self { - self.service = Some(service.into()); + pub fn set_service(&mut self, service: impl AsRef) -> &mut Self { + self.service = Some(service.as_ref().into()); self } /// Populates a `CFDictionary` to be passed to @@ -633,28 +635,22 @@ impl ItemAddOptions { }, } } - let account_name = self.account_name.as_deref().map(CFString::from); - if let Some(account_name) = &account_name { + if let Some(account_name) = &self.account_name { dict.add(&unsafe { kSecAttrAccount }.to_void(), &account_name.to_void()); } - let access_group = self.access_group.as_deref().map(CFString::from); - if let Some(access_group) = &access_group { + if let Some(access_group) = &self.access_group { dict.add(&unsafe { kSecAttrAccessGroup }.to_void(), &access_group.to_void()); } - let comment = self.comment.as_deref().map(CFString::from); - if let Some(comment) = &comment { + if let Some(comment) = &self.comment { dict.add(&unsafe { kSecAttrComment }.to_void(), &comment.to_void()); } - let description = self.description.as_deref().map(CFString::from); - if let Some(description) = &description { + if let Some(description) = &self.description { dict.add(&unsafe { kSecAttrDescription }.to_void(), &description.to_void()); } - let label = self.label.as_deref().map(CFString::from); - if let Some(label) = &label { + if let Some(label) = &self.label { dict.add(&unsafe { kSecAttrLabel }.to_void(), &label.to_void()); } - let service = self.service.as_deref().map(CFString::from); - if let Some(service) = &service { + if let Some(service) = &self.service { dict.add(&unsafe { kSecAttrService }.to_void(), &service.to_void()); }