iso14229_1/common/
security_access.rs1use crate::{Configuration, Error, Placeholder, RequestData, ResponseData, utils};
5
6#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7pub struct SecurityAccessLevel(u8);
8
9impl SecurityAccessLevel {
10 pub fn new(level: u8) -> Result<Self, Error> {
11 if level < 1 || level > 0x7D {
12 return Err(Error::InvalidParam(format!("access level: {}", level)));
13 }
14
15 Ok(Self(level))
16 }
17}
18
19impl TryFrom<u8> for SecurityAccessLevel {
20 type Error = Error;
21 fn try_from(value: u8) -> Result<Self, Self::Error> {
22 Self::new(value)
23 }
24}
25
26impl Into<u8> for SecurityAccessLevel {
27 fn into(self) -> u8 {
28 self.0
29 }
30}
31
32#[derive(Debug, Clone)]
34pub struct SecurityAccessData(pub Vec<u8>);
35
36impl<'a> TryFrom<&'a [u8]> for SecurityAccessData {
37 type Error = Error;
38
39 fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
40 Ok(Self(value.to_vec()))
41 }
42}
43
44impl Into<Vec<u8>> for SecurityAccessData {
45 fn into(self) -> Vec<u8> {
46 self.0
47 }
48}
49
50impl RequestData for SecurityAccessData {
51 type SubFunc = SecurityAccessLevel;
52 fn try_parse(data: &[u8], _: Option<Self::SubFunc>, _: &Configuration) -> Result<Self, Error> {
53 Self::try_from(data)
54 }
55 fn to_vec(self, _: &Configuration) -> Vec<u8> {
56 self.into()
57 }
58}
59
60impl ResponseData for SecurityAccessData {
61 type SubFunc = SecurityAccessLevel;
62
63 fn try_parse(data: &[u8], _: Option<Self::SubFunc>, _: &Configuration) -> Result<Self, Error> {
64 Self::try_from(data)
65 }
66 fn to_vec(self, _: &Configuration) -> Vec<u8> {
67 self.into()
68 }
69}