implement card model

This commit is contained in:
2026-08-18 20:51:19 +00:00
parent b55b444db4
commit 68250b2924
5 changed files with 82 additions and 29 deletions
+42 -10
View File
@@ -1,4 +1,7 @@
use alloc::{string::String, vec::Vec};
use num_enum::FromPrimitive;
use crate::card::decoder::{Events, decode_known_event, decode_packed_card_text, decode_secret};
#[derive(Debug, Clone, Copy)]
pub struct NfcPayload<'a> {
@@ -12,16 +15,18 @@ pub struct NfcPayload<'a> {
pub _opaque_trailer: &'a [u8],
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq, FromPrimitive)]
#[repr(u8)]
pub enum CardType {
Default,
Blueprint,
CYBER,
Nature,
Fossil,
Ghost,
Daemon,
Fairy,
#[num_enum(default)]
Default = 0,
Blueprint = 1,
CYBER = 2,
Nature = 3,
Fossil = 4,
Ghost = 5,
Daemon = 6,
Fairy = 7,
}
#[derive(Debug, Clone)]
@@ -32,8 +37,9 @@ pub struct Sprite {
#[derive(Debug, Clone)]
pub struct Card {
pub uuid: String,
pub name: String,
pub event: Events,
pub cardtype: CardType,
pub name: String,
pub trait1: String,
pub trait2: String,
@@ -42,3 +48,29 @@ pub struct Card {
pub sprite: Sprite,
}
impl TryFrom<NfcPayload<'static>> for Card {
type Error = &'static str;
fn try_from(payload: NfcPayload) -> Result<Self, Self::Error> {
let uuid: String = str::from_utf8(payload.card_uuid).unwrap().into();
let event = decode_known_event(payload.event_encoding);
let cardtype = CardType::from(payload.card_type);
let (name, trait1, trait2, trait3) = decode_packed_card_text(payload.packed_card_text);
let secret = decode_secret(payload.secret);
let sprite = Sprite {
data: payload.sprite.to_vec(),
};
Ok(Self {
uuid,
event,
cardtype,
name,
trait1,
trait2,
trait3,
secret,
sprite,
})
}
}