Mercurial > crates > nonstick
comparison src/items.rs @ 59:3f4a77aa88be
Fix string copyting and improve error situation.
This change is too big and includes several things:
- Fix copying strings from PAM by fixing const and mut on pam funcs.
- Improve error enums by simplifying conversions and removing
unnecessary and ambiguous "success" variants.
- Make a bunch of casts nicer.
- Assorted other cleanup.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Wed, 21 May 2025 00:27:18 -0400 |
| parents | daa2cde64601 |
| children | 05cc2c27334f |
comparison
equal
deleted
inserted
replaced
| 58:868a278a362c | 59:3f4a77aa88be |
|---|---|
| 1 use num_derive::{FromPrimitive, ToPrimitive}; | 1 use crate::constants::InvalidEnum; |
| 2 use num_traits::{FromPrimitive, ToPrimitive}; | 2 use num_derive::FromPrimitive; |
| 3 use num_traits::FromPrimitive; | |
| 3 use std::ffi::{c_int, CStr}; | 4 use std::ffi::{c_int, CStr}; |
| 4 | 5 |
| 5 #[derive(FromPrimitive, ToPrimitive)] | 6 #[derive(FromPrimitive)] |
| 6 #[repr(i32)] | 7 #[repr(i32)] |
| 8 #[non_exhaustive] // because C could give us anything! | |
| 7 pub enum ItemType { | 9 pub enum ItemType { |
| 8 /// Unset. This should never be used. | |
| 9 Unset = 0, | |
| 10 /// The service name | 10 /// The service name |
| 11 Service = 1, | 11 Service = 1, |
| 12 /// The user name | 12 /// The user name |
| 13 User = 2, | 13 User = 2, |
| 14 /// The tty name | 14 /// The tty name |
| 33 XAuthData = 12, | 33 XAuthData = 12, |
| 34 /// The type for pam_get_authtok | 34 /// The type for pam_get_authtok |
| 35 AuthTokType = 13, | 35 AuthTokType = 13, |
| 36 } | 36 } |
| 37 | 37 |
| 38 impl From<c_int> for ItemType { | 38 impl TryFrom<c_int> for ItemType { |
| 39 fn from(value: c_int) -> Self { | 39 type Error = InvalidEnum<Self>; |
| 40 Self::from_i32(value).unwrap_or(Self::Unset) | 40 fn try_from(value: c_int) -> Result<Self, Self::Error> { |
| 41 Self::from_i32(value).ok_or(value.into()) | |
| 41 } | 42 } |
| 42 } | 43 } |
| 43 | 44 |
| 44 impl From<ItemType> for c_int { | 45 impl From<ItemType> for c_int { |
| 45 fn from(val: ItemType) -> Self { | 46 fn from(val: ItemType) -> Self { |
| 46 val.to_i32().unwrap_or(0) | 47 val as Self |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 // A type that can be requested by `pam::Handle::get_item`. | 51 /// A type that can be requested by [crate::Handle::get_item]. |
| 51 pub trait Item { | 52 pub trait Item { |
| 52 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. | 53 /// The `repr(C)` type that is returned (by pointer) by the underlying `pam_get_item` function. |
| 53 type Raw; | 54 type Raw; |
| 54 | 55 |
| 55 /// The `ItemType` for this type | 56 /// The `ItemType` for this type |
| 99 | 100 |
| 100 cstr_item!(Service); | 101 cstr_item!(Service); |
| 101 cstr_item!(User); | 102 cstr_item!(User); |
| 102 cstr_item!(Tty); | 103 cstr_item!(Tty); |
| 103 cstr_item!(RemoteHost); | 104 cstr_item!(RemoteHost); |
| 104 // Conv | 105 // Conversation is not included here since it's special. |
| 105 cstr_item!(AuthTok); | 106 cstr_item!(AuthTok); |
| 106 cstr_item!(OldAuthTok); | 107 cstr_item!(OldAuthTok); |
| 107 cstr_item!(RemoteUser); | 108 cstr_item!(RemoteUser); |
| 108 cstr_item!(UserPrompt); | 109 cstr_item!(UserPrompt); |
