// SPDX-FileCopyrightText: 2024 Markus Haug (Korrat)
//
// SPDX-License-Identifier: EUPL-1.2

use core::fmt::Display;

use beancount_amount::Amount;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PriceSpec {
    PerUnit(Amount),
    Total(Amount),
}

impl PriceSpec {
    pub const fn amount(&self) -> Amount {
        let (Self::PerUnit(amount) | Self::Total(amount)) = self;
        *amount
    }
}

impl Display for PriceSpec {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let symbol = match self {
            Self::PerUnit(_) => "@",
            Self::Total(_) => "@@",
        };
        let amount = self.amount();

        write!(f, "{symbol} {amount}")
    }
}

impl From<Amount> for PriceSpec {
    fn from(amount: Amount) -> Self {
        Self::PerUnit(amount)
    }
}