asset.rs 849 B

12345678910111213141516171819202122232425262728293031
  1. use crate::{amount::AmountCents, Amount};
  2. use serde::{Deserialize, Serialize};
  3. use std::fmt::Display;
  4. pub type AssetId = u32;
  5. /// An asset
  6. ///
  7. /// The asset definition is just a u32 number, which is meaningless for the
  8. /// code, as long as each asset has an unique id.
  9. ///
  10. /// The asset struct also has a precision, which is a u8 number (from 0-255)
  11. /// which is used by amount when converting the inner_amount to a human readable
  12. /// format.
  13. #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
  14. pub struct Asset {
  15. pub id: AssetId,
  16. pub(crate) precision: u8,
  17. }
  18. impl Asset {
  19. pub fn new_amount(&self, cents: AmountCents) -> Amount {
  20. Amount::new(*self, cents)
  21. }
  22. }
  23. impl Display for Asset {
  24. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  25. write!(f, "{}", self.id)
  26. }
  27. }