#payment #reduce #minimize #debts

debtsolver

通过最少交易追踪和结算各方债务

2个不稳定版本

0.2.0 2019年12月12日
0.1.0 2019年11月19日

#13 in #minimize

MIT许可证

21KB
343

Debtsolver

通过最少交易追踪和结算各方债务。

示例

use debtsolver::Ledger;
use debtsolver::Transaction;
use debtsolver::transaction;

fn main() {
    let mut ledger = Ledger::new();

    // Let's say that:
    // Alice paid 20 for Bob's lunch
    // Bob paid 20 for Charlie's dinner the next day.
    ledger.add_transaction(transaction!("Alice", "Bob", (20, "USD")));
    ledger.add_transaction(transaction!("Bob", "Charlie", (20, "USD")));

    for payment in ledger.settle() {
        println!("{}", payment)
    } 
    // Debtsolver will resolve this with one payment:
    // Alice owes Charlie 20.00 USD


    // Now lets say that:
    //   Bob paid for Alice's breakfast (20).
    //   Charlie paid for Bob's lunch (50).
    //   Alice paid for Charlie's dinner (35).
    ledger.add_transaction(transaction!("Alice", "Bob", (20, "USD")));
    ledger.add_transaction(transaction!("Bob", "Charlie", (50, "USD")));
    ledger.add_transaction(transaction!("Charlie", "Alice", (35, "USD")));
    

    for payment in ledger.settle() {
        println!("{}", payment)
    } 
    // Debtsolver will resolve this with just two payments:
    // Bob owes Alice 15.00 USD
    // Bob owes Charlie 15.00 USD
}

依赖项

~1.5MB
~32K SLoC