Dinero is a Rust port of Dinero.js (unstable)

Overview

Dinero-rust

Stability: alpha Crates.io Crates.io Crates.io

Dinero is a Rust port of Dinero.js
Dinero lets you create, calculate, and format money in Rust.
docs.rs/dinero


📦 Install

$ cargo add dinero

⚡️ Quick start

Dinero objects are minimal. The API is heavily inspired by dinero.js unless there is a more suitable Rust way to implement things.

use dinero::{api::add, currencies::USD, format::to_unit, Dinero};

// Create a Dinero object of value 8.5 USD (the default scale for USD is 2)
let d1 = Dinero::new(850, USD, None);
// Create a Dinero object of value 5 USD with a custom scale 3
let d2 = Dinero::new(5000, USD, Some(3));

// Add the 2 Dineros, the value is stored in the result Dinero without modifying d1 and d2
let result = add(&d1, &d2); // Similar API as Dinero.js

// Or you can use the standard operators
let result = d1 + d2; // WIP missing currency check

match result {
  Ok(value) => println!("{} USD", to_unit(value, None, None)), // 13.5 USD
  Err(_) => println!("Error adding d1+d2"),
}

🦀 Disclaimer

I'm using this project to learn about Rust. And I'm working my way through the language and the ecosystem.

Consider the current version of Dinero unstable. There will definitely be breaking changes.

📜 License

MIT

You might also like...
Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]
Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]

Rust Latam: procedural macros workshop This repo contains a selection of projects designed to learn to write Rust procedural macros — Rust code that g

The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language.

rcc The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language. Compilers Language Co

Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed.

integra8 Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed. | This repo is in a "work in progress"

Neofetch but in Rust (rust-toml-fetch)
Neofetch but in Rust (rust-toml-fetch)

rtfetch Configuration Recompile each time you change the config file logo = "arch.logo" # in src/assets. info = [ "", "", "yellow{host_n

Rust Sandbox [code for 15 concepts of Rust language]

Rust-Programming-Tutorial Rust Sandbox [code for 15 concepts of Rust language]. The first time I've been introduced to Rust was on January 2022, you m

TypeRust - simple Rust playground where you can build or run your Rust code and share it with others

Rust playground Welcome to TypeRust! This is a simple Rust playground where you can build or run your Rust code and share it with others. There are a

Rust Imaging Library: A high-level Rust imaging crate.

ril Rust Imaging Library: A performant and high-level Rust imaging crate. Documentation • Crates.io • Discord What's this? This is a Rust crate design

In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang.
In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang.

Learn Rust What is this? In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang. This is usef

Game Boy Emulator written in Rust, as a way to fully grasp the Rust programming language

Flan's Game Boy Emulator Game Boy Emulator written in Rust, as a way to get hands-on with the Rust programming language, and creating a proper project

Comments
  • Todos

    Todos

    List of things to do before v1 release:

    • [ ] Determine finite list of features to port from https://v2.dinerojs.com/docs
    • [ ] Write all selected functions
    • [ ] Test all selected functions
    • [ ] Refactor code outside of core/src/lib.rs to make it easier to maintain
    • [ ] Add CI (lint, build, coverage, etc...)
    • [ ] Write docs for docs.rs
    • [ ] Make a doc landing page + ReadMe with more user-friendly guides
    • [ ] ???

    List of selected functions

    • [ ] allocate
    • [ ] trim_scale
    • [ ] transform_scale
    • [ ] normalize_scale
    • [ ] add
    • [ ] subtract
    • [ ] multiply
    • [ ] is_zero
    • [ ] is_negative
    • [ ] is_positive
    • [ ] have_same_amount
    • [ ] have_same_currency
    • [ ] ???

    Post release to do:

    • [ ] Setup auto-release and contribution guides
    • [ ] Setup contrib list
    • [ ] Setup issue templates
    • [ ] ???
    opened by raed667 0
  • Explore the idea of transforming `Currency` from struct to a trait.

    Explore the idea of transforming `Currency` from struct to a trait.

    Explore the idea of transforming Currency to a trait.

    Current implementation

    Today the Dinero struct looks like this:

    #[derive(Debug, Clone, Copy, Eq)]
    pub struct Dinero {
        pub amount: i128,
        pub currency: Currency, // <-- 
        pub scale: u32,
    }
    

    Goals

    When doing operations on Dinero we often check if they have the same currency at run-time.

    Example:

    let a = Dinero::new(1, EUR, None);
    let b = Dinero::new(2, USD, None);
    
    let z = add(x,y); // <-- This will return an `UnequalCurrencyError`
    

    Possible implementation

    Using Bounds this can be done at compile time.

    pub trait Currency {
        const CODE: CountryCode;
        const BASE: u32;
        const EXPONENT: u32;
    }
    

    Then maybe the Dinero struct can look something like this?

    #[derive(Debug, Clone, Copy)]
    pub struct Dinero<Currency> { // <-- Not sure how to reference the currency here?
        pub amount: i128,
        pub scale: u32,
    }
    
    // A constructor would look like this?
    impl Dinero<dyn Currency> {
        pub fn new(amount: i128, scale: Option<u32>) -> Dinero<dyn Currency> {
            Dinero {
                scale: scale.unwrap_or_else(|| self::Currency::EXPONENT.to_owned()),
                amount,
            }
        }
    }
    
    
    let d = Dinero::<USD>::new(42, Some(2));
    

    I'm still not sure how to make this work synthetically. So any help is more than welcome!

    enhancement help wanted good first issue 
    opened by raed667 0
Releases(v0.0.11)
Owner
Raed
Sometimes I write code, most times I talk about writing code
Raed
Rust port of c-kzg-4844

KZG Rust Rust port of https://github.com/ethereum/c-kzg-4844 . Uses the blst rust bindings for all BLS functionality. Tries to be almost a line to lin

Pawan Dhananjay 13 Aug 24, 2023
Leetcode Solutions in Rust, Advent of Code Solutions in Rust and more

RUST GYM Rust Solutions Leetcode Solutions in Rust AdventOfCode Solutions in Rust This project demostrates how to create Data Structures and to implem

Larry Fantasy 635 Jan 3, 2023
Simple autoclicker written in Rust, to learn the Rust language.

RClicker is an autoclicker written in Rust, written to learn more about the Rust programming language. RClicker was was written by me to learn more ab

null 7 Nov 15, 2022
Rust programs written entirely in Rust

mustang Programs written entirely in Rust Mustang is a system for building programs built entirely in Rust, meaning they do not depend on any part of

Dan Gohman 561 Dec 26, 2022
Rust 核心库和标准库的源码级中文翻译,可作为 IDE 工具的智能提示 (Rust core library and standard library translation. can be used as IntelliSense for IDE tools)

Rust 标准库中文版 这是翻译 Rust 库 的地方, 相关源代码来自于 https://github.com/rust-lang/rust。 如果您不会说英语,那么拥有使用中文的文档至关重要,即使您会说英语,使用母语也仍然能让您感到愉快。Rust 标准库是高质量的,不管是新手还是老手,都可以从中

wtklbm 493 Jan 4, 2023
A library for extracting #[no_mangle] pub extern "C" functions (https://docs.rust-embedded.org/book/interoperability/rust-with-c.html#no_mangle)

A library for extracting #[no_mangle] pub extern "C" functions In order to expose a function with C binary interface for interoperability with other p

Dmitrii - Demenev 0 Feb 17, 2022
clone of grep cli written in Rust. From Chapter 12 of the Rust Programming Language book

minigrep is a clone of the grep cli in rust Minigrep will find a query string in a file. To test it out, clone the project and run cargo run body poem

Raunak Singh 1 Dec 14, 2021
Rust-blog - Educational blog posts for Rust beginners

pretzelhammer's Rust blog ?? I write educational content for Rust beginners and Rust advanced beginners. My posts are listed below in reverse chronolo

kirill 5.2k Jan 1, 2023
The ray tracer challenge in rust - Repository to follow my development of "The Raytracer Challenge" book by Jamis Buck in the language Rust

The Ray Tracer Challenge This repository contains all the code written, while step by implementing Ray Tracer, based on the book "The Ray Tracer Chall

Jakob Westhoff 54 Dec 25, 2022
Learn-rust-the-hard-way - "Learn C The Hard Way" by Zed Shaw Converted to Rust

Learn Rust The Hard Way This is an implementation of Zed Shaw's Learn X The Hard Way for the Rust Programming Language. Installing Rust TODO: Instruct

Ryan Levick 309 Dec 8, 2022