Skip to content

Commit

Permalink
feat: kos-mobile BigNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrxlz committed Dec 3, 2024
1 parent f4c73fd commit dafc094
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/kos-mobile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ version.workspace = true
[lib]
crate-type = ["cdylib", "staticlib", "rlib"]

[features]
default = ["serde"]

[[bin]]
name = "uniffi-bindgen"
path = "src/bin/uniffi-bindgen.rs"
Expand All @@ -23,6 +26,9 @@ lazy_static = { workspace = true }
thiserror = { workspace = true }

uniffi = { workspace = true, features = ["cli"] }
serde = { workspace = true, features = ["derive"], optional = true }
num-bigint = "0.4"
num-traits = "0.2"

[build-dependencies]
uniffi = { workspace = true, features = ["build"] }
2 changes: 2 additions & 0 deletions packages/kos-mobile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod number;

use hex::FromHexError;
use hex::ToHex;
use kos::chains::{get_chain_by_base_id, Chain, ChainError, Transaction};
Expand Down
98 changes: 98 additions & 0 deletions packages/kos-mobile/src/number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
use serde::{Deserialize, Serialize};
use std::str::FromStr;

#[derive(Debug, Clone, Default, Serialize, Deserialize, uniffi::Record)]
pub struct BigNumber {
pub value: String,
}

impl BigNumber {
pub fn new(value: &str) -> Result<Self, String> {
BigInt::from_str(value)
.map(|_| BigNumber {
value: value.to_string(),
})
.map_err(|_| format!("Invalid number format: {}", value))
}

pub fn to_bigint(&self) -> Result<BigInt, String> {
BigInt::from_str(&self.value).map_err(|_| format!("Failed to parse BigInt: {}", self.value))
}
}

#[uniffi::export]
fn big_number_new(value: String) -> Result<BigNumber, String> {
BigNumber::new(&value)
}

#[uniffi::export]
fn big_number_add(lhs: BigNumber, rhs: BigNumber) -> Result<BigNumber, String> {
let left = lhs.to_bigint()?;
let right = rhs.to_bigint()?;
Ok(BigNumber {
value: (left + right).to_string(),
})
}

#[uniffi::export]
fn big_number_subtract(lhs: BigNumber, rhs: BigNumber) -> Result<BigNumber, String> {
let left = lhs.to_bigint()?;
let right = rhs.to_bigint()?;
Ok(BigNumber {
value: (left - right).to_string(),
})
}

#[uniffi::export]
fn big_number_multiply(lhs: BigNumber, rhs: BigNumber) -> Result<BigNumber, String> {
let left = lhs.to_bigint()?;
let right = rhs.to_bigint()?;
Ok(BigNumber {
value: (left * right).to_string(),
})
}

#[uniffi::export]
fn big_number_divide(lhs: BigNumber, rhs: BigNumber) -> Result<BigNumber, String> {
let left = lhs.to_bigint()?;
let right = rhs.to_bigint()?;
if right.is_zero() {
return Err("Cannot divide by zero".to_string());
}
Ok(BigNumber {
value: (left / right).to_string(),
})
}

#[uniffi::export]
fn big_number_is_zero(value: BigNumber) -> Result<bool, String> {
Ok(value.to_bigint()?.is_zero())
}

#[uniffi::export]
fn big_number_increment(value: BigNumber) -> Result<BigNumber, String> {
let current = value.to_bigint()?;
Ok(BigNumber {
value: (current + BigInt::one()).to_string(),
})
}

#[uniffi::export]
fn big_number_decrement(value: BigNumber) -> Result<BigNumber, String> {
let current = value.to_bigint()?;
Ok(BigNumber {
value: (current - BigInt::one()).to_string(),
})
}

#[uniffi::export]
fn big_number_is_positive(value: BigNumber) -> Result<bool, String> {
Ok(value.to_bigint()?.is_positive())
}

#[uniffi::export]
fn big_number_is_negative(value: BigNumber) -> Result<bool, String> {
Ok(value.to_bigint()?.is_negative())
}

0 comments on commit dafc094

Please sign in to comment.