use thiserror::Error;
#[derive(Debug, Error)]
#[error("EfParseError: {0}")]
pub struct EfParseError(pub String);
impl EfParseError {
pub fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
}
impl From<asn1::ParseError> for EfParseError {
fn from(e: asn1::ParseError) -> Self {
Self(format!("ASN.1 parse error: {e}"))
}
}
pub trait ElementaryFile {
const FID: u16;
const SFI: u8;
fn to_bytes(&self) -> &[u8];
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug)]
struct DummyEf {
encoded: Vec<u8>,
}
impl DummyEf {
fn from_bytes(data: Vec<u8>) -> Result<Self, EfParseError> {
if data.is_empty() {
return Err(EfParseError::new("empty"));
}
Ok(Self { encoded: data })
}
}
impl ElementaryFile for DummyEf {
const FID: u16 = 0x011C;
const SFI: u8 = 0x1C;
fn to_bytes(&self) -> &[u8] {
&self.encoded
}
}
#[test]
fn trait_exposes_associated_constants() {
assert_eq!(DummyEf::FID, 0x011C);
assert_eq!(DummyEf::SFI, 0x1C);
}
#[test]
fn to_bytes_returns_payload() {
let ef = DummyEf::from_bytes(vec![1, 2, 3]).unwrap();
assert_eq!(ef.to_bytes(), &[1, 2, 3]);
}
#[test]
fn empty_payload_is_error() {
let err = DummyEf::from_bytes(vec![]).unwrap_err();
assert_eq!(err.0, "empty");
}
}