Mercurial > crates > nonstick
comparison src/conv.rs @ 79:2128123b9406
Format (oops!) and make some fun and/or stupid conversions available.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sun, 08 Jun 2025 04:21:58 -0400 |
| parents | 002adfb98c5c |
| children | 05291b601f0a |
comparison
equal
deleted
inserted
replaced
| 78:002adfb98c5c | 79:2128123b9406 |
|---|---|
| 158 "The `data_type` tag is a value that is simply passed through" | 158 "The `data_type` tag is a value that is simply passed through" |
| 159 "to the application. PAM does not define any meaning for it." | 159 "to the application. PAM does not define any meaning for it." |
| 160 ); | 160 ); |
| 161 | 161 |
| 162 /// Owned binary data. | 162 /// Owned binary data. |
| 163 /// | |
| 164 /// You can take ownership of the stored data by destructuring it: | |
| 165 /// | |
| 166 /// ``` | |
| 167 /// # use nonstick::BinaryData; | |
| 168 /// # let binary_data = BinaryData::new(vec![99, 88, 77], 66); | |
| 169 /// let (data, data_type) = binary_data.into(); | |
| 170 /// ``` | |
| 171 #[derive(Debug, Default, PartialEq)] | 163 #[derive(Debug, Default, PartialEq)] |
| 172 pub struct BinaryData { | 164 pub struct BinaryData { |
| 173 data: Vec<u8>, | 165 /// The data. |
| 174 data_type: u8, | 166 pub data: Vec<u8>, |
| 167 /// A tag describing the type of the data, to use how you please. | |
| 168 pub data_type: u8, | |
| 175 } | 169 } |
| 176 | 170 |
| 177 impl BinaryData { | 171 impl BinaryData { |
| 178 /// Creates a `BinaryData` with the given contents and type. | 172 /// Creates a `BinaryData` with the given contents and type. |
| 179 pub fn new(data: impl Into<Vec<u8>>, data_type: u8) -> Self { | 173 pub fn new(data: impl Into<Vec<u8>>, data_type: u8) -> Self { |
| 180 Self { data: data.into(), data_type } | 174 Self { |
| 175 data: data.into(), | |
| 176 data_type, | |
| 177 } | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 impl<IV: Into<Vec<u8>>> From<(IV, u8)> for BinaryData { | |
| 182 /// Makes a new BinaryData from borrowed data. | |
| 183 fn from((data, data_type): (IV, u8)) -> Self { | |
| 184 Self { | |
| 185 data: data.into(), | |
| 186 data_type, | |
| 187 } | |
| 181 } | 188 } |
| 182 } | 189 } |
| 183 | 190 |
| 184 impl From<BinaryData> for (Vec<u8>, u8) { | 191 impl From<BinaryData> for (Vec<u8>, u8) { |
| 192 /// Easy destructuring. | |
| 185 fn from(value: BinaryData) -> Self { | 193 fn from(value: BinaryData) -> Self { |
| 186 (value.data, value.data_type) | 194 (value.data, value.data_type) |
| 187 } | 195 } |
| 188 } | 196 } |
| 189 | 197 |
| 190 impl From<(&'_[u8], u8)> for BinaryData { | 198 impl<'a> From<&'a BinaryData> for (&'a [u8], u8) { |
| 191 fn from((data, data_type): (&'_[u8], u8)) -> Self { | |
| 192 Self { | |
| 193 data: data.to_vec(), | |
| 194 data_type, | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 impl<'a> From<&'a BinaryData> for (&'a[u8], u8) { | |
| 200 fn from(value: &'a BinaryData) -> Self { | 199 fn from(value: &'a BinaryData) -> Self { |
| 201 (&value.data, value.data_type) | 200 (&value.data, value.data_type) |
| 202 } | |
| 203 } | |
| 204 | |
| 205 impl From<BinaryData> for Vec<u8> { | |
| 206 /// Takes ownership of the data stored herein. | |
| 207 fn from(value: BinaryData) -> Self { | |
| 208 value.data | |
| 209 } | 201 } |
| 210 } | 202 } |
| 211 | 203 |
| 212 q_and_a!( | 204 q_and_a!( |
| 213 InfoMsg<'a, Q = &'a str, A = ()>, | 205 InfoMsg<'a, Q = &'a str, A = ()>, |
| 515 let radio = RadioQAndA::new("channel?"); | 507 let radio = RadioQAndA::new("channel?"); |
| 516 let bin = BinaryQAndA::new((&[10, 9, 8], 66)); | 508 let bin = BinaryQAndA::new((&[10, 9, 8], 66)); |
| 517 conv.communicate(&[radio.message(), bin.message()]); | 509 conv.communicate(&[radio.message(), bin.message()]); |
| 518 | 510 |
| 519 assert_eq!("zero", radio.answer().unwrap()); | 511 assert_eq!("zero", radio.answer().unwrap()); |
| 520 assert_eq!(BinaryData::new(vec![5, 5, 5], 5), bin.answer().unwrap()); | 512 assert_eq!(BinaryData::from(([5, 5, 5], 5)), bin.answer().unwrap()); |
| 521 } | 513 } |
| 522 | 514 |
| 523 fn test_mux() { | 515 fn test_mux() { |
| 524 struct MuxTester; | 516 struct MuxTester; |
| 525 | 517 |
| 544 assert_eq!("password!", ask.question()); | 536 assert_eq!("password!", ask.question()); |
| 545 ask.set_answer(Ok("open sesame".into())) | 537 ask.set_answer(Ok("open sesame".into())) |
| 546 } | 538 } |
| 547 Message::BinaryPrompt(prompt) => { | 539 Message::BinaryPrompt(prompt) => { |
| 548 assert_eq!((&[1, 2, 3][..], 69), prompt.question()); | 540 assert_eq!((&[1, 2, 3][..], 69), prompt.question()); |
| 549 prompt.set_answer(Ok(BinaryData::new(vec![3, 2, 1], 42))) | 541 prompt.set_answer(Ok(BinaryData::from((&[3, 2, 1], 42)))) |
| 550 } | 542 } |
| 551 Message::RadioPrompt(ask) => { | 543 Message::RadioPrompt(ask) => { |
| 552 assert_eq!("radio?", ask.question()); | 544 assert_eq!("radio?", ask.question()); |
| 553 ask.set_answer(Ok("yes".to_owned())) | 545 ask.set_answer(Ok("yes".to_owned())) |
| 554 } | 546 } |
