annotate src/constants.rs @ 63:a7aa5ca0d00d

Move MessageStyle to conv, the only place it is used.
author Paul Fisher <paul@pfish.zone>
date Wed, 21 May 2025 23:19:43 -0400
parents 05cc2c27334f
children bbe84835d6db
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
1 //! Constants and enum values from the PAM library.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
2
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
3 use bitflags::bitflags;
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
4 use libc::{c_int, c_uint};
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
5 use num_derive::FromPrimitive;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
6 use num_traits::FromPrimitive;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
7 use std::any;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
8 use std::marker::PhantomData;
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
9
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
10 bitflags! {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
11 /// The available PAM flags.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
12 ///
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
13 /// See `/usr/include/security/_pam_types.h` for more details.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
14 #[derive(Debug, PartialEq)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
15 #[repr(transparent)]
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
16 pub struct Flags: c_uint {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
17 /// Authentication service should not generate any messages.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
18 const SILENT = 0x8000;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
19 /// The service should return [ErrorCode::AuthError] if the user
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
20 /// has a null authentication token.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
21 const DISALLOW_NULL_AUTHTOK = 0x0001;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
22 /// Set user credentials for an authentication service.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
23 const ESTABLISH_CRED = 0x0002;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
24 /// Delete user credentials associated with
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
25 /// an authentication service.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
26 const DELETE_CRED = 0x0004;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
27 /// Reinitialize user credentials.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
28 const REINITIALIZE_CRED = 0x0008;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
29 /// Extend the lifetime of user credentials.
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
30 const REFRESH_CRED = 0x0010;
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
31 /// The password service should only update those passwords
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
32 /// that have aged. If this flag is _not_ passed,
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
33 /// the password service should update all passwords.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
34 const CHANGE_EXPIRED_AUTHTOK = 0x0020;
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
35 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
36 }
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
37
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
38 /// The Linux-PAM error return values. Success is an Ok [Result].
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
39 ///
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
40 /// Most abbreviations (except `AuthTok` and `Max`) are now full words.
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
41 /// For more detailed information, see
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
42 /// `/usr/include/security/_pam_types.h`.
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
43 #[allow(non_camel_case_types, dead_code)]
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
44 #[derive(Copy, Clone, Debug, PartialEq, thiserror::Error, FromPrimitive)]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
45 #[non_exhaustive] // C might give us anything!
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
46 pub enum ErrorCode {
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
47 #[error("dlopen() failure when dynamically loading a service module")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
48 OpenError = 1,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
49 #[error("symbol not found")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
50 SymbolError = 2,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
51 #[error("error in service module")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
52 ServiceError = 3,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
53 #[error("system error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
54 SystemError = 4,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
55 #[error("memory buffer error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
56 BufferError = 5,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
57 #[error("permission denied")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
58 PermissionDenied = 6,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
59 #[error("authentication failure")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
60 AuthenticationError = 7,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
61 #[error("cannot access authentication data due to insufficient credentials")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
62 CredentialsInsufficient = 8,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
63 #[error("underlying authentication service cannot retrieve authentication information")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
64 AuthInfoUnavailable = 9,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
65 #[error("user not known to the underlying authentication module")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
66 UserUnknown = 10,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
67 #[error("retry limit reached; do not attempt further")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
68 MaxTries = 11,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
69 #[error("new authentication token required")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
70 NewAuthTokRequired = 12,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
71 #[error("user account has expired")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
72 AccountExpired = 13,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
73 #[error("cannot make/remove an entry for the specified session")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
74 SessionError = 14,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
75 #[error("underlying authentication service cannot retrieve user credentials")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
76 CredentialsUnavailable = 15,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
77 #[error("user credentials expired")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
78 CredentialsExpired = 16,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
79 #[error("failure setting user credentials")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
80 CredentialsError = 17,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
81 #[error("no module-specific data is present")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
82 NoModuleData = 18,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
83 #[error("conversation error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
84 ConversationError = 19,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
85 #[error("authentication token manipulation error")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
86 AuthTokError = 20,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
87 #[error("authentication information cannot be recovered")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
88 AuthTokRecoveryError = 21,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
89 #[error("authentication token lock busy")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
90 AuthTokLockBusy = 22,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
91 #[error("authentication token aging disabled")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
92 AuthTokDisableAging = 23,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
93 #[error("preliminary check by password service")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
94 TryAgain = 24,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
95 #[error("ignore underlying account module, regardless of control flag")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
96 Ignore = 25,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
97 #[error("critical error; this module should fail now")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
98 Abort = 26,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
99 #[error("authentication token has expired")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
100 AuthTokExpired = 27,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
101 #[error("module is not known")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
102 ModuleUnknown = 28,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
103 #[error("bad item passed to pam_[whatever]_item")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
104 BadItem = 29,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
105 #[error("conversation function is event-driven and data is not available yet")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
106 ConversationAgain = 30,
55
676675c3d434 Make PamResultCode implement Error.
Paul Fisher <paul@pfish.zone>
parents: 45
diff changeset
107 #[error("call this function again to complete authentication stack")]
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
108 Incomplete = 31,
15
27730595f1ea Adding pam-http module
Anthony Nowell <anthony@algorithmia.com>
parents:
diff changeset
109 }
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
110
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
111 /// A PAM-specific Result type with an [ErrorCode] error.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
112 pub type Result<T> = std::result::Result<T, ErrorCode>;
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
113
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
114 impl ErrorCode {
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
115 /// Converts this [Result] into a C-compatible result code.
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
116 pub fn result_to_c<T>(value: Result<T>) -> c_int {
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
117 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
118 Ok(_) => 0, // PAM_SUCCESS
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
119 Err(otherwise) => otherwise.into(),
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
120 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
121 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
122
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
123 /// Converts a C result code into a [Result], with success as Ok.
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
124 /// Invalid values are returned as a [Self::SystemError].
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
125 pub fn result_from(value: c_int) -> Result<()> {
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
126 match value {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
127 0 => Ok(()),
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
128 value => Err(value.try_into().unwrap_or(Self::SystemError)),
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
129 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
130 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
131 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
132
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
133 impl TryFrom<c_int> for ErrorCode {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
134 type Error = InvalidEnum<Self>;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
135
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
136 fn try_from(value: c_int) -> std::result::Result<Self, Self::Error> {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
137 Self::from_i32(value).ok_or(value.into())
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
138 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
139 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
140
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
141 impl From<ErrorCode> for c_int {
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
142 fn from(val: ErrorCode) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
143 val as Self
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
144 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
145 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
146
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
147 /// Error returned when attempting to coerce an invalid C integer into an enum.
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
148 #[derive(thiserror::Error)]
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
149 #[error("{0} is not a valid {type}", type = any::type_name::<T>())]
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
150 #[derive(Debug, PartialEq)]
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
151 pub struct InvalidEnum<T>(c_int, PhantomData<T>);
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
152
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
153 impl<T> From<InvalidEnum<T>> for c_int {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
154 fn from(value: InvalidEnum<T>) -> Self {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
155 value.0
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
156 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
157 }
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
158
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
159 impl<T> From<c_int> for InvalidEnum<T> {
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
160 fn from(value: c_int) -> Self {
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
161 Self(value, PhantomData)
56
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
162 }
daa2cde64601 Big big refactor. Probably should have been multiple changes.
Paul Fisher <paul@pfish.zone>
parents: 55
diff changeset
163 }
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
164
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
165 #[cfg(test)]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
166 mod tests {
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
167 use super::*;
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
168
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
169 #[test]
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
170 fn test_enums() {
63
a7aa5ca0d00d Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents: 60
diff changeset
171 assert_eq!(Ok(ErrorCode::ServiceError), 3.try_into());
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
172 assert_eq!(Err(InvalidEnum::from(999)), ErrorCode::try_from(999));
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
173 assert_eq!(Ok(()), ErrorCode::result_from(0));
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
174 assert_eq!(Err(ErrorCode::Abort), ErrorCode::result_from(26));
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
175 assert_eq!(Err(ErrorCode::SystemError), ErrorCode::result_from(423));
63
a7aa5ca0d00d Move MessageStyle to conv, the only place it is used.
Paul Fisher <paul@pfish.zone>
parents: 60
diff changeset
176 assert!(InvalidEnum::<ErrorCode>(33, PhantomData)
60
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
177 .to_string()
05cc2c27334f The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents: 59
diff changeset
178 .starts_with("33 is not a valid "));
59
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
179 }
3f4a77aa88be Fix string copyting and improve error situation.
Paul Fisher <paul@pfish.zone>
parents: 56
diff changeset
180 }