From b2945ed45bc7fda1f58280aa5276d88dddeb9dc6 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 20 Apr 2025 00:10:43 +0200 Subject: [PATCH] Use #[cfg(target_os = "macos")] Instead of the very verbose: #[cfg(not(any( target_os = "ios", target_os = "watchos", target_os = "tvos", target_os = "visionos" )))] --- src/imp/security_framework.rs | 98 +++++------------------------------ 1 file changed, 14 insertions(+), 84 deletions(-) diff --git a/src/imp/security_framework.rs b/src/imp/security_framework.rs index 302791a..061dd15 100644 --- a/src/imp/security_framework.rs +++ b/src/imp/security_framework.rs @@ -18,54 +18,24 @@ use std::str; use std::sync::Mutex; use std::sync::Once; -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] use self::security_framework::os::macos::certificate::{PropertyType, SecCertificateExt}; -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] use self::security_framework::os::macos::certificate_oids::CertificateOid; -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] use self::security_framework::os::macos::identity::SecIdentityExt; -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] use self::security_framework::os::macos::import_export::{ ImportOptions, Pkcs12ImportOptionsExt, SecItems, }; -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] use self::security_framework::os::macos::keychain::{self, KeychainSettings, SecKeychain}; use {Protocol, TlsAcceptorBuilder, TlsConnectorBuilder}; static SET_AT_EXIT: Once = Once::new(); -#[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" -)))] +#[cfg(target_os = "macos")] static TEMP_KEYCHAIN: Mutex> = Mutex::new(None); fn convert_protocol(protocol: Protocol) -> SslProtocol { @@ -110,22 +80,12 @@ pub struct Identity { } impl Identity { - #[cfg(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - ))] + #[cfg(not(target_os = "macos"))] pub fn from_pkcs8(_: &[u8], _: &[u8]) -> Result { panic!("Not implemented on iOS"); } - #[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - )))] + #[cfg(target_os = "macos")] pub fn from_pkcs8(pem: &[u8], key: &[u8]) -> Result { if !key.starts_with(b"-----BEGIN PRIVATE KEY-----") { return Err(Error(base::Error::from(errSecParam))); @@ -183,12 +143,7 @@ impl Identity { }) } - #[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - )))] + #[cfg(target_os = "macos")] fn import_options(buf: &[u8], pass: &str) -> Result, Error> { SET_AT_EXIT.call_once(|| { extern "C" fn atexit() { @@ -221,12 +176,7 @@ impl Identity { Ok(imports) } - #[cfg(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - ))] + #[cfg(not(target_os = "macos"))] fn import_options(buf: &[u8], pass: &str) -> Result, Error> { let imports = Pkcs12ImportOptions::new().passphrase(pass).import(buf)?; Ok(imports) @@ -255,12 +205,7 @@ impl Certificate { Ok(Certificate(cert)) } - #[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - )))] + #[cfg(target_os = "macos")] pub fn from_pem(buf: &[u8]) -> Result { let mut items = SecItems::default(); ImportOptions::new().items(&mut items).import(buf)?; @@ -271,12 +216,7 @@ impl Certificate { } } - #[cfg(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - ))] + #[cfg(not(target_os = "macos"))] pub fn from_pem(_: &[u8]) -> Result { panic!("Not implemented on iOS, tvOS, watchOS or visionOS"); } @@ -535,22 +475,12 @@ impl TlsStream { } } - #[cfg(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - ))] + #[cfg(not(target_os = "macos"))] pub fn tls_server_end_point(&self) -> Result>, Error> { Ok(None) } - #[cfg(not(any( - target_os = "ios", - target_os = "watchos", - target_os = "tvos", - target_os = "visionos" - )))] + #[cfg(target_os = "macos")] pub fn tls_server_end_point(&self) -> Result>, Error> { let cert = match self.cert { Some(ref cert) => cert.clone(),