Debt
Debt
One of the greatest ways to learn a new language is making a new project, today I want to share with you one toy application made in rust. It is a simple CLI debt manager.
How to use it
Thanks to clap the design of its CLI was pretty straightforward, it has a must have feature I always look since I met docopt (from python), that is documentation integrated with the functional design:
Usage: debt [OPTIONS] <COMMAND>
Commands:
init Initialize Database
register Register a transaction
view View Registered data
add Add a new agent to lend or pay
help Print this message or the help of the given subcommand(s)
Options:
-d, --database-path <DATABASE_PATH> file path where data is stored [default: $XDG_DATA_HOME/debt/debt.db [env: DEBT_DB=]
-h, --help Print help
-V, --version Print version
Examples
Putting examples ease a lot the process of explaining a new user how to use a CLI tool, so below you have the necessary ones to learn use my program.
Initialize a debt database:
$ debt init
Add a new debtor:
$ debt add 'Jane Doe'
Borrow 1000 (dollars, euros or whatever) to Jane Doe:
$ debt register 'Jane Doe' 1000 'Some optional note'
Register a payment of 50 (of any exchange) from Jane Doe:
$ debt register -p 'Jane Doe' 50 'Another optional note'
View the people who owes you money.
$ debt view
Checkout payments and loans history:
$ debt view -H
Filter Jane Doe history
$ debt view -H 'Jane Doe'
How it works
It is basically a CLI programs that manages the database presented below using rusqlite.
CREATE TABLE Agents (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE
)
CREATE TABLE Registers (
id INTEGER PRIMARY KEY,
agent_id INTEGER,
register_date INTEGER NOT NULL,
amount INTEGER NOT NULL,
note TEXT,
FOREIGN KEY(agent_id) REFERENCES Agents(id)
)
What I learnt
Let me list you some of the things clarified to me while making this project:
- The difference between
&str
andString
data types. - How to work with Files.
- The point of
Option
andResult
generic types, and why is common to see methods likeunwrap
orexpect
. - Have a better idea of variable lifetimes.
- How to create modules in rust.
- Have a better sense of ownership and the borrow checker.
- A little about how to use macros.
- The difference between
Path
andPathbuf
.
Checkout the repo:
Feel free to clone my repo or check the source code here