Giter VIP home page Giter VIP logo

dinero's Introduction

dinero's People

Contributors

raed667 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

dinero's Issues

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!

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
  • ???

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.