Skip to content

Commit

Permalink
rename file decimal -> decimal192.rs, add from f32.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Feb 17, 2024
1 parent ff8a4a4 commit ac0e057
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 45 deletions.
7 changes: 7 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ extend-exclude = [
"tarpaulin-report.html",
"tests/vectors/fixtures/*.json",
]


[default.extend-identifiers]
inout = "inout"

[default.extend-words]
inout = "inout"
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,55 @@ extension Decimal192: CustomStringConvertible {
}

extension Decimal192 {
public static let maxDivisibility: UInt = 18
public static let maxDivisibility: UInt8 = 18
}

// MARK: Truncation and rounding

extension Decimal192 {

private func rounded(decimalPlaces: UInt8, roundingMode: RoundingMode) -> Self {
precondition(
decimalPlaces <= Decimal192.maxDivisibility,
"Decimal places MUST be 0...18, was: \(decimalPlaces)"
)
do {
return try decimalRound(
decimal: self,
decimalPlaces: Int32(decimalPlaces),
roundingMode: roundingMode
)
} catch {
fatalError("Failed to round, error: \(error)")
}
}


/// Rounds to `decimalPlaces` decimals
public func rounded(decimalPlaces: UInt8 = 0) -> Self {
rounded(
decimalPlaces: decimalPlaces,
roundingMode: .toNearestMidpointAwayFromZero
)
}

/// Rounds to `decimalPlaces` decimals, in the direction of 0
public func floor(decimalPlaces: UInt) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .toZero)
public func floor(decimalPlaces: UInt8) -> Self {
rounded(decimalPlaces: decimalPlaces, roundingMode: .toZero)
}

/// Rounds to `decimalPlaces` decimals, in the direction away from zero
public func ceil(decimalPlaces: UInt) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .awayFromZero)
public func ceil(decimalPlaces: UInt8) -> Self {
rounded(decimalPlaces: decimalPlaces, roundingMode: .awayFromZero)
}

/// Rounds to `decimalPlaces` decimals
public func rounded(decimalPlaces: UInt = 0) -> Self {
try! round(decimalPlaces: Int32(decimalPlaces), roundingMode: .toNearestMidpointAwayFromZero)
}
}

extension Decimal192 {
public var clamped: Self {
isNegative() ? .zero : self
decimalClampedToZero(decimal: self)
}

public func isNegative() -> Bool {
decimalIsNegative(decimal: self)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
extension Decimal192: Sendable {}

extension Decimal192 {
public init(_ string: String) throws {
self = try newDecimalFromString(string: string)
}
}

extension Decimal192: CustomStringConvertible {
public var description: String {
decimalToString(decimal: self)
}
}

extension Decimal192: ExpressibleByStringLiteral {
public init(stringLiteral string: String) {
try! self.init(string)
Expand All @@ -23,16 +11,7 @@ extension Decimal192: ExpressibleByIntegerLiteral {
}
}

extension Decimal192: ExpressibleByFloatLiteral {
public init(floatLiteral value: Double) {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.decimalSeparator = "." // Sargon ALWAYS uses "."
formatter.maximumFractionDigits = 18
let string = formatter.string(from: NSNumber(value: value))!
try! self.init(string)
}
}

extension Decimal192: AdditiveArithmetic {
public static var zero: Self {
newDecimalFromU64(value: 0)
Expand All @@ -41,19 +20,19 @@ extension Decimal192: AdditiveArithmetic {
decimalAdd(lhs: lhs, rhs: rhs)
}
public static func - (lhs: Self, rhs: Self) -> Self {
decimalSub(lhs, rhs)
decimalSub(lhs: lhs, rhs: rhs)
}
}
extension Decimal192: SignedNumeric {
public static func - (operand: Self) -> Self {
public prefix static func - (operand: Self) -> Self {
decimalNeg(decimal: operand)
}
}
extension Decimal192: Numeric {
public typealias Magnitude = Self

public var magnitude: Magnitude {
decimalAbs(self)
decimalAbs(decimal: self)
}

public static func * (lhs: Self, rhs: Self) -> Self {
Expand Down
166 changes: 166 additions & 0 deletions apple/Sources/UniFFI/Sargon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,19 @@ private struct FfiConverterInt64: FfiConverterPrimitive {
}
}

private struct FfiConverterFloat: FfiConverterPrimitive {
typealias FfiType = Float
typealias SwiftType = Float

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> Float {
return try lift(readFloat(&buf))
}

public static func write(_ value: Float, into buf: inout [UInt8]) {
writeFloat(&buf, lower(value))
}
}

private struct FfiConverterBool: FfiConverter {
typealias FfiType = Int8
typealias SwiftType = Bool
Expand Down Expand Up @@ -8739,6 +8752,104 @@ public func FfiConverterTypeResourceOrNonFungible_lower(_ value: ResourceOrNonFu

extension ResourceOrNonFungible: Equatable, Hashable {}

// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
/**
* Defines the rounding strategy used when you round e.g. `Decimal192`.
*
* Following the same naming convention as https://docs.rs/rust_decimal/latest/rust_decimal/enum.RoundingStrategy.html.
*/
public enum RoundingMode {
/**
* The number is always rounded toward positive infinity, e.g. `3.1 -> 4`, `-3.1 -> -3`.
*/
case toPositiveInfinity
/**
* The number is always rounded toward negative infinity, e.g. `3.1 -> 3`, `-3.1 -> -4`.
*/
case toNegativeInfinity
/**
* The number is always rounded toward zero, e.g. `3.1 -> 3`, `-3.1 -> -3`.
*/
case toZero
/**
* The number is always rounded away from zero, e.g. `3.1 -> 4`, `-3.1 -> -4`.
*/
case awayFromZero
/**
* The number is rounded to the nearest, and when it is halfway between two others, it's rounded toward zero, e.g. `3.5 -> 3`, `-3.5 -> -3`.
*/
case toNearestMidpointTowardZero
/**
* The number is rounded to the nearest, and when it is halfway between two others, it's rounded away from zero, e.g. `3.5 -> 4`, `-3.5 -> -4`.
*/
case toNearestMidpointAwayFromZero
/**
* The number is rounded to the nearest, and when it is halfway between two others, it's rounded toward the nearest even number. Also known as "Bankers Rounding".
*/
case toNearestMidpointToEven
}

public struct FfiConverterTypeRoundingMode: FfiConverterRustBuffer {
typealias SwiftType = RoundingMode

public static func read(from buf: inout (data: Data, offset: Data.Index)) throws -> RoundingMode {
let variant: Int32 = try readInt(&buf)
switch variant {
case 1: return .toPositiveInfinity

case 2: return .toNegativeInfinity

case 3: return .toZero

case 4: return .awayFromZero

case 5: return .toNearestMidpointTowardZero

case 6: return .toNearestMidpointAwayFromZero

case 7: return .toNearestMidpointToEven

default: throw UniffiInternalError.unexpectedEnumCase
}
}

public static func write(_ value: RoundingMode, into buf: inout [UInt8]) {
switch value {
case .toPositiveInfinity:
writeInt(&buf, Int32(1))

case .toNegativeInfinity:
writeInt(&buf, Int32(2))

case .toZero:
writeInt(&buf, Int32(3))

case .awayFromZero:
writeInt(&buf, Int32(4))

case .toNearestMidpointTowardZero:
writeInt(&buf, Int32(5))

case .toNearestMidpointAwayFromZero:
writeInt(&buf, Int32(6))

case .toNearestMidpointToEven:
writeInt(&buf, Int32(7))
}
}
}

public func FfiConverterTypeRoundingMode_lift(_ buf: RustBuffer) throws -> RoundingMode {
return try FfiConverterTypeRoundingMode.lift(buf)
}

public func FfiConverterTypeRoundingMode_lower(_ value: RoundingMode) -> RustBuffer {
return FfiConverterTypeRoundingMode.lower(value)
}

extension RoundingMode: Equatable, Hashable {}

// Note that we don't yet support `indirect` for enums.
// See https://github.com/mozilla/uniffi-rs/issues/396 for further discussion.
/**
Expand Down Expand Up @@ -10032,6 +10143,19 @@ public func decimalAdd(lhs: Decimal192, rhs: Decimal192) -> Decimal192 {
)
}

/**
* Clamps `decimal` to zero, i.e. `max(decimal, 0)`
*/
public func decimalClampedToZero(decimal: Decimal192) -> Decimal192 {
return try! FfiConverterTypeDecimal192.lift(
try! rustCall {
uniffi_sargon_fn_func_decimal_clamped_to_zero(
FfiConverterTypeDecimal192.lower(decimal), $0
)
}
)
}

/**
* `lhs / rhs``
*/
Expand Down Expand Up @@ -10192,6 +10316,24 @@ public func decimalNeg(decimal: Decimal192) -> Decimal192 {
)
}

/**
* Rounds this number to the specified decimal places.
*
* # Panics
* - Panic if the number of decimal places is not within [0..SCALE(=18)]
*/
public func decimalRound(decimal: Decimal192, decimalPlaces: Int32, roundingMode: RoundingMode) throws -> Decimal192 {
return try FfiConverterTypeDecimal192.lift(
rustCallWithError(FfiConverterTypeCommonError.lift) {
uniffi_sargon_fn_func_decimal_round(
FfiConverterTypeDecimal192.lower(decimal),
FfiConverterInt32.lower(decimalPlaces),
FfiConverterTypeRoundingMode.lower(roundingMode), $0
)
}
)
}

/**
* `lhs - rhs``
*/
Expand Down Expand Up @@ -10406,6 +10548,21 @@ public func newDecimalExponent(exponent: UInt8) -> Decimal192 {
)
}

/**
* Creates a new `Decimal192` from a f32 float, it does
* so by first converting the float to a String, using
* Rust's `to_string` on the float.
*/
public func newDecimalFromF32(value: Float) -> Decimal192 {
return try! FfiConverterTypeDecimal192.lift(
try! rustCall {
uniffi_sargon_fn_func_new_decimal_from_f32(
FfiConverterFloat.lower(value), $0
)
}
)
}

/**
* Creates a new `Decimal192` from a i32 integer.
*/
Expand Down Expand Up @@ -10789,6 +10946,9 @@ private var initializationResult: InitializationResult {
if uniffi_sargon_checksum_func_decimal_add() != 697 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_decimal_clamped_to_zero() != 64928 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_decimal_div() != 19189 {
return InitializationResult.apiChecksumMismatch
}
Expand Down Expand Up @@ -10825,6 +10985,9 @@ private var initializationResult: InitializationResult {
if uniffi_sargon_checksum_func_decimal_neg() != 41456 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_decimal_round() != 8832 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_decimal_sub() != 35224 {
return InitializationResult.apiChecksumMismatch
}
Expand Down Expand Up @@ -10891,6 +11054,9 @@ private var initializationResult: InitializationResult {
if uniffi_sargon_checksum_func_new_decimal_exponent() != 14207 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_new_decimal_from_f32() != 3137 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_sargon_checksum_func_new_decimal_from_i32() != 18727 {
return InitializationResult.apiChecksumMismatch
}
Expand Down
Loading

0 comments on commit ac0e057

Please sign in to comment.