Skip to main content

payrail_core/
amount.rs

1use crate::PaymentError;
2
3/// Integer amount in the smallest currency unit.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct MinorAmount(i64);
6
7impl MinorAmount {
8    /// Creates a positive minor-unit amount.
9    ///
10    /// # Errors
11    ///
12    /// Returns an error when `value` is zero or negative.
13    #[inline]
14    pub fn new(value: i64) -> Result<Self, PaymentError> {
15        if value <= 0 {
16            return Err(PaymentError::InvalidAmount(value));
17        }
18
19        Ok(Self(value))
20    }
21
22    /// Returns the raw minor-unit value.
23    #[inline]
24    #[must_use]
25    pub const fn value(self) -> i64 {
26        self.0
27    }
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn new_accepts_positive_amount() {
36        let amount = MinorAmount::new(100).expect("positive amount should be valid");
37
38        assert_eq!(amount.value(), 100);
39    }
40
41    #[test]
42    fn new_rejects_zero_amount() {
43        assert!(matches!(
44            MinorAmount::new(0),
45            Err(PaymentError::InvalidAmount(0))
46        ));
47    }
48}