This little project will help you touch many topics around Rust, in a small and contained way. It will touch
- struct
- Vec
- enum
- HashMap
- Generics
- impl block
- traits
- Error handling
- Result
- Async
- Handling datetimes
- Static values
- JSON (de)serialization
- reading .env variables
- Integrating CLI commands
- Logging
- Reading/writing from/to a file
- Cross-compiling Rust code
- Sending E-Mails via Rust
- Modularising your code
- Testing
- Macros
You can adjust the tutorial to your needs, but I find it more usefull to create something you will actually use. Therefore:
Update and read from a Google Sheet where you store your latest financial information and send out an E-Mail with a summary.
This can be adjuste to:
Update and read from a CSV file where you store your latest financial information and send out an E-Mail with a summary.
If you are not interested in the topic (finance), feel free to adjust it to whatever data inputs you consume a lot and want to automise.
If you want to go down the Google Sheets route, you need to:
- Create a "Service Account" via the Google Console.
- Create a key which downloads a JSON file you need for the library we are going to use.
- Create a new Google Sheet and share it with the Service Account E-Mail so the secret you just downloaded can read and modify the Google Sheet.
Depending on which external APIs you are going to use, you need to create API keys for them as well. For this tutorial, I am using:
- The coinmarketcap.com API to fetch the latest Crypto prices
- The eodhistoricaldata.com API to fetch ETF prices
- Send a GET request to https://httpbin.org/ip and print the result to the console.
- Send out a request to CMC and fetch the price of BTC.
- Read the API key from an .env file.
- Get the name, symbol, price and 7day of the BTC and store it in a struct.
- Pass the list of currencies to fetch via the CLI.
- Save the result in a CSV file.
- In case the API returns an error, write it out to a log file and abort the application.
- In addition to the coin prices, fetch the price of a random ETF and store it also in a struct.
- Add a rows in your Google Sheet with ISN, amount of coins, price, total and a row with the total value of your portfolio.
- Instead of saving the result to a CSV, update your Google sheet.
- Move out your business logic in different modules.
- Build your Rust code and move the binary to a server and run it from there.
- Send out an E-Mail with the coin and ETF overview and redeploy your application/binary.
This step is done in the browser.
This is done mostly locally.
> rustup target add x86_64-unknown-linux-musl
> cargo build --release --target=x86_64-unknown-linux-musl
You need to add a ssl dependency to Cargo.toml, and depending on your OS, install other third party packages. The error message is quite helpful.
For this, I overcomplicated things a bit to show some nice features of Rust. We can have different types in Rust. And instead of the "EMail-Type" knowing too much how to display information, we let the types themselves decide how to "display" their information in a HTML way. For this, we require types passed down to an E-Mail implement the HTML trait. Later on, we can have a Vec of Generics, and the "send_email" function has the trait bound to require the types to implement this trait.