casper_types/chainspec/
pricing_handling.rs1use crate::{
2 bytesrepr,
3 bytesrepr::{FromBytes, ToBytes},
4};
5use core::fmt::{Display, Formatter};
6#[cfg(feature = "datasize")]
7use datasize::DataSize;
8use serde::{Deserialize, Serialize};
9
10const PRICING_HANDLING_TAG_LENGTH: u8 = 1;
11
12const PRICING_HANDLING_PAYMENT_LIMITED_TAG: u8 = 0;
13const PRICING_HANDLING_FIXED_TAG: u8 = 1;
14
15#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
18#[serde(tag = "type", rename_all = "snake_case")]
19#[cfg_attr(feature = "datasize", derive(DataSize))]
20pub enum PricingHandling {
21 #[default]
22 PaymentLimited,
25 Fixed,
27}
28
29impl Display for PricingHandling {
30 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
31 match self {
32 PricingHandling::PaymentLimited => {
33 write!(f, "PricingHandling::PaymentLimited")
34 }
35 PricingHandling::Fixed => {
36 write!(f, "PricingHandling::Fixed")
37 }
38 }
39 }
40}
41
42impl ToBytes for PricingHandling {
43 fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
44 let mut buffer = bytesrepr::allocate_buffer(self)?;
45
46 match self {
47 PricingHandling::PaymentLimited => {
48 buffer.push(PRICING_HANDLING_PAYMENT_LIMITED_TAG);
49 }
50 PricingHandling::Fixed => {
51 buffer.push(PRICING_HANDLING_FIXED_TAG);
52 }
53 }
54
55 Ok(buffer)
56 }
57
58 fn serialized_length(&self) -> usize {
59 PRICING_HANDLING_TAG_LENGTH as usize
60 }
61}
62
63impl FromBytes for PricingHandling {
64 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
65 let (tag, rem) = u8::from_bytes(bytes)?;
66 match tag {
67 PRICING_HANDLING_PAYMENT_LIMITED_TAG => Ok((PricingHandling::PaymentLimited, rem)),
68 PRICING_HANDLING_FIXED_TAG => Ok((PricingHandling::Fixed, rem)),
69 _ => Err(bytesrepr::Error::Formatting),
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76
77 use super::*;
78
79 #[test]
80 fn bytesrepr_roundtrip_for_payment_limited() {
81 let handling = PricingHandling::PaymentLimited;
82 bytesrepr::test_serialization_roundtrip(&handling);
83 }
84
85 #[test]
86 fn bytesrepr_roundtrip_for_fixed() {
87 let handling = PricingHandling::Fixed;
88 bytesrepr::test_serialization_roundtrip(&handling);
89 }
90}