Skip to content

Commit

Permalink
Wrap SOME of Radix Engine Toolkit types, first of MANY PRs. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX authored Feb 19, 2024
1 parent 3e9d604 commit 73af6c9
Show file tree
Hide file tree
Showing 83 changed files with 5,908 additions and 776 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
fail_fast: true

repos:
- repo: https://github.com/crate-ci/typos
rev: v1.17.2
hooks:
- id: typos
- repo: local
hooks:
- id: ensure Swift not changed to local development
name: ensure Swift not changed to local development
language: system
types: [file, swift]
entry: ./scripts/ios/ensure-not-local.sh
pass_filenames: false

- id: fmt
name: fmt
language: system
Expand Down
12 changes: 10 additions & 2 deletions .tarpaulin.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
[all]
exclude-files = [
"tests/*",
"src/wrapped_radix_engine_toolkit/*",
"example/*",
"target/*",
"apple/*",
".swiftpm/*",
"scripts/*",
".build/*",
"Package.swift",
]
verbose = false
all-features = true
timeout = "1800s"
timeout = "5m"
skip-clean = true
out = ["Xml"]
force-clean = false
17 changes: 17 additions & 0 deletions Cargo.lock

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

9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ path = "src/bin.rs"
[dependencies]
log = "0.4.20"
pretty_env_logger = "0.5.0"
derive_more = { version = "1.0.0-beta.6", features = ["debug", "display"] }
derive_more = { version = "1.0.0-beta.6", features = [
"debug",
"display",
"from_str",
] }
serde = { version = "1.0.193", features = ["derive", "rc", "std"] }
serde_json = { version = "1.0.108", features = ["preserve_order"] }
serde_with = { version = "3.4.0" }
Expand All @@ -46,7 +50,7 @@ radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto",
radix-engine-toolkit-json = { git = "https://github.com/radixdlt/radix-engine-toolkit", rev = "9dc3deeb3299b4f4c255f36bb3d504681379ad5a" }
radix-engine-toolkit = { git = "https://github.com/radixdlt/radix-engine-toolkit", rev = "9dc3deeb3299b4f4c255f36bb3d504681379ad5a" }
enum-iterator = "1.4.1"
bip32 = "0.5.1" # only need Secp256k1, to do validation of PublicKey
bip32 = "0.5.1" # only need Secp256k1, to do validation of PublicKey
ed25519-dalek = "1.0.1"
rand = "0.8.5"
hex = "0.4.3"
Expand All @@ -64,6 +68,7 @@ bip39 = { version = "2.0.0", features = ["serde"] }
time-util = { version = "0.3.4", features = ["chrono"] }
assert-json-diff = "2.0.2"
url = { version = "2.5.0", features = ["serde"] }
num-format = "0.4.4"

[build-dependencies]
uniffi = { version = "0.26.0", features = ["build"] }
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription

let sargonBinaryTargetName = "SargonCoreRS"
let binaryTarget: Target
let useLocalFramework = true
let useLocalFramework = false

if useLocalFramework {
binaryTarget = .binaryTarget(
Expand Down
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
@@ -0,0 +1,5 @@
extension AppearanceID: CaseIterable {
public static var allCases: [Self] {
appearanceIdsAll()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension BagOfBytes {
public init(data: Data) {
self = newBagOfBytesFrom(bytes: data)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
extension Decimal192 {
public init(_ string: String) throws {
self = try newDecimalFromString(string: string)
}
}

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

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

extension Decimal192 {
/// Parse a local respecting string
public init(
formattedString: String,
locale: Locale = .autoupdatingCurrent
) throws {
let localConfig: LocaleConfig = LocaleConfig(locale: locale)
self = try newDecimalFromFormattedString(
formattedString: formattedString,
locale: localConfig
)
}
}

// 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: UInt8) -> Self {
rounded(decimalPlaces: decimalPlaces, roundingMode: .toZero)
}

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

}

extension Decimal192 {
public var clamped: Self {
decimalClampedToZero(decimal: self)
}

public func isNegative() -> Bool {
decimalIsNegative(decimal: self)
}
}

extension Decimal192: Comparable {
public static func > (lhs: Self, rhs: Self) -> Bool {
lhs.greaterThan(other: rhs)
}
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.lessThan(other: rhs)
}
public static func >= (lhs: Self, rhs: Self) -> Bool {
lhs.greaterThanOrEqual(other: rhs)
}
public static func <= (lhs: Self, rhs: Self) -> Bool {
lhs.lessThanOrEqual(other: rhs)
}
}
extension Decimal192 {

public func lessThan(other: Self) -> Bool {
decimalLessThan(lhs: self, rhs: other)
}

public func lessThanOrEqual(other: Self) -> Bool {
decimalLessThanOrEqual(lhs: self, rhs: other)
}

public func greaterThan(other: Self) -> Bool {
decimalGreaterThan(lhs: self, rhs: other)
}

public func greaterThanOrEqual(other: Self) -> Bool {
decimalGreaterThanOrEqual(lhs: self, rhs: other)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension DisplayName {
public init(validating name: String) throws {
self = try newDisplayName(name: name)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension Mnemonic {
public var phrase: String {
mnemonicPhrase(from: self)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
public typealias AppearanceID = AppearanceId

extension AppearanceID: Sendable {}
extension AppearanceID: CaseIterable {
public static var allCases: [Self] {
appearanceIdsAll()
extension AppearanceID: Identifiable {
public typealias ID = UInt8
public var id: ID {
value
}
}
extension AppearanceID: CustomStringConvertible {
public var description: String {
value.description
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
extension BagOfBytes {
public init(data: Data) {
self = newBagOfBytesFrom(bytes: data)
}
public static func random(byteCount: Int) -> Self {
var data = Data(repeating: 0, count: byteCount)
data.withUnsafeMutableBytes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,55 @@
extension Decimal192: Sendable {}
//extension Decimal192: ExpressibleByStringLiteral {
//
//}
//extension Decimal192: ExpressibleByFloatLiteral {
//
//}
//extension Decimal192: SignedNumeric /* Numeric, ExpressibleByIntegerLiteral AdditiveArithmetic */
//{
//
//}

extension Decimal192: ExpressibleByStringLiteral {
public init(stringLiteral string: String) {
try! self.init(string)
}
}
extension Decimal192: ExpressibleByIntegerLiteral {
public init(integerLiteral i64: Int64) {
self = newDecimalFromI64(value: i64)
}
}


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

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

public static func * (lhs: Self, rhs: Self) -> Self {
decimalMul(lhs: lhs, rhs: rhs)
}

public static func *= (lhs: inout Self, rhs: Self) {
lhs = lhs * rhs
}

public init?<T>(exactly source: T) where T: BinaryInteger {
if let i64 = Int64(exactly: source) {
self = newDecimalFromI64(value: i64)
} else if let u64 = UInt64(exactly: source) {
self = newDecimalFromU64(value: u64)
} else {
return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
extension DisplayName: Sendable {}
extension DisplayName {
public init(validating name: String) throws {
self = try newDisplayName(name: name)
}
}

#if DEBUG
extension DisplayName: ExpressibleByStringLiteral {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extension LocaleConfig: Sendable {}
extension LocaleConfig {
public init(locale: Locale) {
self.init(
decimalSeparator: locale.decimalSeparator,
groupingSeparator: locale.groupingSeparator
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension Mnemonic: Sendable {}
Loading

0 comments on commit 73af6c9

Please sign in to comment.