use std::borrow::Cow;
use super::{features::FeaturesSet, status::ErStatus, version::v0::RecordV0};
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;
use crate::{consts::ER_RECORD_SEED, ID};
#[derive(Debug, BorshSerialize, BorshDeserialize)]
#[cfg_attr(not(feature = "entrypoint"), derive(PartialEq, Eq, Clone))]
pub enum ErRecord {
V0(RecordV0),
}
impl ErRecord {
pub fn pda(&self) -> (Pubkey, u8) {
Pubkey::find_program_address(&self.seeds(), &ID)
}
pub fn seeds(&self) -> [&[u8]; 2] {
[ER_RECORD_SEED, self.identity().as_ref()]
}
pub fn identity(&self) -> &Pubkey {
match self {
Self::V0(r) => &r.identity,
}
}
pub fn addr(&self) -> &str {
match self {
Self::V0(v) => &v.addr,
}
}
pub fn base_fee(&self) -> u16 {
match self {
Self::V0(v) => v.base_fee,
}
}
pub fn features(&self) -> &FeaturesSet {
match self {
Self::V0(v) => &v.features,
}
}
pub fn block_time_ms(&self) -> u16 {
match self {
Self::V0(v) => v.block_time_ms,
}
}
pub fn status(&self) -> ErStatus {
match self {
Self::V0(v) => v.status,
}
}
pub fn load_average(&self) -> u32 {
match self {
Self::V0(v) => v.load_average,
}
}
pub fn country_code(&self) -> CountryCode {
match self {
Self::V0(v) => v.country_code,
}
}
pub fn set_addr(&mut self, addr: String) {
match self {
Self::V0(v) => v.addr = addr,
}
}
pub fn set_base_fee(&mut self, base_fee: u16) {
match self {
Self::V0(v) => v.base_fee = base_fee,
}
}
pub fn set_features(&mut self, features: FeaturesSet) {
match self {
Self::V0(v) => v.features = features,
}
}
pub fn set_block_time_ms(&mut self, block_time_ms: u16) {
match self {
Self::V0(v) => v.block_time_ms = block_time_ms,
}
}
pub fn set_status(&mut self, status: ErStatus) {
match self {
Self::V0(v) => v.status = status,
}
}
pub fn set_load_average(&mut self, load_average: u32) {
match self {
Self::V0(v) => v.load_average = load_average,
}
}
pub fn set_country_code(&mut self, country_code: CountryCode) {
match self {
Self::V0(v) => v.country_code = country_code,
}
}
}
#[derive(BorshDeserialize, BorshSerialize, Debug, Clone, Copy, PartialEq, Eq)]
pub struct CountryCode([u8; 3]);
impl<S: AsRef<[u8]>> From<S> for CountryCode {
fn from(value: S) -> Self {
const LEN: usize = std::mem::size_of::<CountryCode>();
let mut buf = [0u8; LEN];
buf.copy_from_slice(&value.as_ref()[..LEN]);
Self(buf)
}
}
impl CountryCode {
pub fn as_str(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.0)
}
}