use core::fmt::Debug;
use core::num::ParseIntError;
use core::str::FromStr;
use std::hash::Hash;
use strict_encoding::{DefaultBasedStrictDumb, StrictDecode, StrictDumb, StrictEncode};
use super::ExposedState;
use crate::{schema, RevealedState, StateType, LIB_NAME_RGB_COMMIT};
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
#[display(inner)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_COMMIT, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase", untagged)
)]
pub enum FungibleState {
#[from]
#[strict_type(tag = 8)] Bits64(u64),
}
impl DefaultBasedStrictDumb for FungibleState {}
impl Default for FungibleState {
fn default() -> Self { FungibleState::Bits64(0) }
}
impl FromStr for FungibleState {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> { s.parse().map(FungibleState::Bits64) }
}
impl From<FungibleState> for u64 {
fn from(value: FungibleState) -> Self {
match value {
FungibleState::Bits64(val) => val,
}
}
}
impl FungibleState {
pub fn fungible_type(&self) -> schema::FungibleType {
match self {
FungibleState::Bits64(_) => schema::FungibleType::Unsigned64Bit,
}
}
pub fn as_u64(&self) -> u64 { (*self).into() }
}
#[derive(Wrapper, Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, From, Display)]
#[display(inner)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[wrapper(Deref)]
#[strict_type(lib = LIB_NAME_RGB_COMMIT)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]
pub struct RevealedValue(FungibleState);
impl RevealedValue {
pub fn new(value: impl Into<FungibleState>) -> Self { Self(value.into()) }
}
impl ExposedState for RevealedValue {
fn state_type(&self) -> StateType { StateType::Fungible }
fn state_data(&self) -> RevealedState { RevealedState::Fungible(*self) }
}
impl From<u64> for RevealedValue {
fn from(value: u64) -> Self { Self(FungibleState::Bits64(value)) }
}