lwk/
precision.rs

1use std::sync::Arc;
2
3use crate::LwkError;
4
5/// Wrapper over [`lwk_common::Precision`]
6#[derive(uniffi::Object, Debug)]
7pub struct Precision {
8    inner: lwk_common::Precision,
9}
10
11#[uniffi::export]
12impl Precision {
13    #[uniffi::constructor]
14
15    /// See [`lwk_common::Precision::new`]
16    pub fn new(precision: u8) -> Result<Arc<Precision>, LwkError> {
17        Ok(Arc::new(Precision {
18            inner: lwk_common::Precision::new(precision)?,
19        }))
20    }
21
22    /// See [`lwk_common::Precision::sats_to_string`]
23    pub fn sats_to_string(&self, sats: i64) -> String {
24        self.inner.sats_to_string(sats)
25    }
26
27    /// See [`lwk_common::Precision::string_to_sats`]
28    pub fn string_to_sats(&self, val: &str) -> Result<i64, LwkError> {
29        Ok(self.inner.string_to_sats(val)?)
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use crate::Precision;
36
37    #[test]
38    fn test_precision() {
39        let precision = Precision::new(2).unwrap();
40        assert_eq!(precision.sats_to_string(100), "1.00");
41        assert_eq!(precision.string_to_sats("1").unwrap(), 100);
42    }
43}