Skip to content

Commit 0b5f053

Browse files
ctzcpu
authored andcommitted
error: use decl+proc macro to generate u32 mapping
This works because cbindgen does not need to expand derive macros to see the enum items, and cbindgen does not care about the existence of `impl From<u32>` for the rust type that the derive outputs.
1 parent ad071cf commit 0b5f053

File tree

4 files changed

+48
-136
lines changed

4 files changed

+48
-136
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

librustls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ webpki = { workspace = true }
3535
libc = { workspace = true }
3636
log = { workspace = true }
3737
rustls-platform-verifier = { workspace = true }
38+
macro_rules_attribute = "0.2.2"
3839

3940
[lib]
4041
name = "rustls_ffi"

librustls/src/error.rs

Lines changed: 21 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,30 @@ use crate::panic::ffi_panic_boundary;
2020
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2121
pub struct rustls_io_result(pub libc::c_int);
2222

23+
/// Derive From<u32> for our result enum.
24+
macro_rules! ResultFromU32 {
25+
(
26+
$( #[$attr:meta] )*
27+
$pub:vis enum $NewType:ident {
28+
$($Item:ident = $Value:literal,)*
29+
$(,)?
30+
}
31+
) => {
32+
impl From<u32> for $NewType {
33+
fn from(x: u32) -> Self {
34+
match x {
35+
$($Value => Self::$Item,)*
36+
_ => Self::InvalidParameter,
37+
}
38+
}
39+
}
40+
};
41+
}
42+
2343
/// Numeric error codes returned from rustls-ffi API functions.
2444
#[allow(dead_code)]
2545
#[repr(u32)]
26-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46+
#[derive(Debug, Clone, Copy, PartialEq, Eq, ResultFromU32!)]
2747
pub enum rustls_result {
2848
Ok = 7000,
2949
Io = 7001,
@@ -189,141 +209,6 @@ pub enum rustls_result {
189209
InvalidEncryptedClientHelloSniRequired = 7702,
190210
}
191211

192-
impl From<u32> for rustls_result {
193-
fn from(x: u32) -> Self {
194-
use rustls_result::*;
195-
196-
match x {
197-
7000 => Ok,
198-
7001 => Io,
199-
7002 => NullParameter,
200-
7003 => InvalidDnsNameError,
201-
7004 => Panic,
202-
7005 => CertificateParseError,
203-
7006 => PrivateKeyParseError,
204-
7007 => InsufficientSize,
205-
7008 => NotFound,
206-
7009 => InvalidParameter,
207-
7010 => UnexpectedEof,
208-
7011 => PlaintextEmpty,
209-
7012 => AcceptorNotReady,
210-
7013 => AlreadyUsed,
211-
7014 => CertificateRevocationListParseError,
212-
7015 => NoServerCertVerifier,
213-
7016 => NoDefaultCryptoProvider,
214-
7017 => GetRandomFailed,
215-
7018 => NoCertResolver,
216-
7019 => HpkeError,
217-
7020 => BuilderIncompatibleTlsVersions,
218-
7101 => NoCertificatesPresented,
219-
7102 => DecryptError,
220-
7103 => FailedToGetCurrentTime,
221-
7113 => FailedToGetRandomBytes,
222-
7104 => HandshakeNotComplete,
223-
7105 => PeerSentOversizedRecord,
224-
7106 => NoApplicationProtocol,
225-
7114 => BadMaxFragmentSize,
226-
7115 => UnsupportedNameType,
227-
7116 => EncryptError,
228-
7121 => CertEncodingBad,
229-
7122 => CertExpired,
230-
7123 => CertNotYetValid,
231-
7124 => CertRevoked,
232-
7125 => CertUnhandledCriticalExtension,
233-
7126 => CertUnknownIssuer,
234-
7127 => CertBadSignature,
235-
7128 => CertNotValidForName,
236-
7129 => CertInvalidPurpose,
237-
7130 => CertApplicationVerificationFailure,
238-
7131 => CertOtherError,
239-
7154 => CertUnknownRevocationStatus,
240-
7156 => CertExpiredRevocationList,
241-
7157 => CertUnsupportedSignatureAlgorithm,
242-
7133 => MessageHandshakePayloadTooLarge,
243-
7134 => MessageInvalidCcs,
244-
7135 => MessageInvalidContentType,
245-
7136 => MessageInvalidCertStatusType,
246-
7137 => MessageInvalidCertRequest,
247-
7138 => MessageInvalidDhParams,
248-
7139 => MessageInvalidEmptyPayload,
249-
7140 => MessageInvalidKeyUpdate,
250-
7141 => MessageInvalidServerName,
251-
7142 => MessageTooLarge,
252-
7143 => MessageTooShort,
253-
7144 => MessageMissingData,
254-
7145 => MessageMissingKeyExchange,
255-
7146 => MessageNoSignatureSchemes,
256-
7147 => MessageTrailingData,
257-
7148 => MessageUnexpectedMessage,
258-
7149 => MessageUnknownProtocolVersion,
259-
7150 => MessageUnsupportedCompression,
260-
7151 => MessageUnsupportedCurveType,
261-
7152 => MessageUnsupportedKeyExchangeAlgorithm,
262-
7153 => MessageInvalidOther,
263-
7155 => MessageCertificatePayloadTooLarge,
264-
7107 => PeerIncompatibleError,
265-
7108 => PeerMisbehavedError,
266-
7109 => InappropriateMessage,
267-
7110 => InappropriateHandshakeMessage,
268-
7112 => General,
269-
7200 => AlertCloseNotify,
270-
7201 => AlertUnexpectedMessage,
271-
7202 => AlertBadRecordMac,
272-
7203 => AlertDecryptionFailed,
273-
7204 => AlertRecordOverflow,
274-
7205 => AlertDecompressionFailure,
275-
7206 => AlertHandshakeFailure,
276-
7207 => AlertNoCertificate,
277-
7208 => AlertBadCertificate,
278-
7209 => AlertUnsupportedCertificate,
279-
7210 => AlertCertificateRevoked,
280-
7211 => AlertCertificateExpired,
281-
7212 => AlertCertificateUnknown,
282-
7213 => AlertIllegalParameter,
283-
7214 => AlertUnknownCA,
284-
7215 => AlertAccessDenied,
285-
7216 => AlertDecodeError,
286-
7217 => AlertDecryptError,
287-
7218 => AlertExportRestriction,
288-
7219 => AlertProtocolVersion,
289-
7220 => AlertInsufficientSecurity,
290-
7221 => AlertInternalError,
291-
7222 => AlertInappropriateFallback,
292-
7223 => AlertUserCanceled,
293-
7224 => AlertNoRenegotiation,
294-
7225 => AlertMissingExtension,
295-
7226 => AlertUnsupportedExtension,
296-
7227 => AlertCertificateUnobtainable,
297-
7228 => AlertUnrecognisedName,
298-
7229 => AlertBadCertificateStatusResponse,
299-
7230 => AlertBadCertificateHashValue,
300-
7231 => AlertUnknownPSKIdentity,
301-
7232 => AlertCertificateRequired,
302-
7233 => AlertNoApplicationProtocol,
303-
7234 => AlertUnknown,
304-
7400 => CertRevocationListBadSignature,
305-
7401 => CertRevocationListInvalidCrlNumber,
306-
7402 => CertRevocationListInvalidRevokedCertSerialNumber,
307-
7403 => CertRevocationListIssuerInvalidForCrl,
308-
7404 => CertRevocationListOtherError,
309-
7405 => CertRevocationListParseError,
310-
7406 => CertRevocationListUnsupportedCrlVersion,
311-
7407 => CertRevocationListUnsupportedCriticalExtension,
312-
7408 => CertRevocationListUnsupportedDeltaCrl,
313-
7409 => CertRevocationListUnsupportedIndirectCrl,
314-
7410 => CertRevocationListUnsupportedRevocationReason,
315-
7411 => CertRevocationListUnsupportedSignatureAlgorithm,
316-
7500 => ClientCertVerifierBuilderNoRootAnchors,
317-
7600 => InconsistentKeysKeysMismatch,
318-
7601 => InconsistentKeysUnknown,
319-
7700 => InvalidEncryptedClientHelloInvalidConfigList,
320-
7701 => InvalidEncryptedClientHelloNoCompatibleConfig,
321-
7702 => InvalidEncryptedClientHelloSniRequired,
322-
_ => InvalidParameter,
323-
}
324-
}
325-
}
326-
327212
impl rustls_result {
328213
/// After a rustls function returns an error, you may call
329214
/// this to get a pointer to a buffer containing a detailed error

librustls/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ pub mod version;
4040
pub use error::rustls_result;
4141
pub use error::*;
4242
pub use version::rustls_version;
43+
44+
#[macro_use]
45+
extern crate macro_rules_attribute;

0 commit comments

Comments
 (0)