Skip to content

Commit a689b6b

Browse files
Improve code, support using files to save/load portfolio
1 parent 231211f commit a689b6b

File tree

4 files changed

+109
-17
lines changed

4 files changed

+109
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target/
2+
/.idea/

Cargo.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description = "A terminal-based portfolio tracker"
1010
comfy-table = "6.1.1"
1111
text_io = "0.1.12"
1212
indoc = "1.0"
13+
serde = {version = "1.0", features = ["derive"]}
14+
serde_json = "1.0"
1315

1416
[profile.release]
1517
# make a small binary - I care much more about small size than speed here

src/main.rs

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ use comfy_table::presets::UTF8_FULL;
22
use comfy_table::Table;
33
use comfy_table::TableComponent::*;
44
use indoc::indoc;
5+
use serde::{Deserialize, Serialize};
6+
use std::fs;
7+
use std::vec;
58
use text_io::read;
69

10+
#[derive(Serialize, Deserialize, Debug)]
11+
struct Portfolio {
12+
assets: Vec<Asset>,
13+
}
14+
15+
#[derive(Serialize, Deserialize, Debug)]
716
struct Asset {
817
ticker: String,
918
buy_price_cents: u32,
@@ -118,35 +127,70 @@ fn print_help() {
118127
let help_text = indoc! {"
119128
assets - prints all assets, both held and sold
120129
new - adds a new asset
121-
help - prints this help text"};
130+
help - prints this help text
131+
load - loads assets from a file"};
122132
println!("{}", help_text);
123133
}
124134

135+
fn prompt(text: &str) -> String {
136+
print!("{}", text);
137+
let data: String = read!();
138+
data
139+
}
140+
141+
fn load_portfolio() -> Option<Portfolio> {
142+
// get the filename and read the file
143+
let filename = prompt("Enter filename to load: ");
144+
let data = fs::read_to_string(filename);
145+
146+
let raw_portfolio: String;
147+
if data.is_ok() {
148+
raw_portfolio = data.unwrap();
149+
} else {
150+
return None;
151+
};
152+
153+
// convert the read file into an actual Portfolio struct
154+
let portfolio = serde_json::from_str(&raw_portfolio);
155+
156+
if portfolio.is_ok() {
157+
Some(portfolio.unwrap())
158+
} else {
159+
None
160+
}
161+
}
162+
163+
fn dump_portfolio(portfolio: &Portfolio) {
164+
let json = serde_json::to_string(&portfolio);
165+
let filename = prompt("Enter filename to dump assets to: ");
166+
if json.is_ok() {
167+
//println!("{}", json.unwrap())
168+
let result = fs::write(filename, json.unwrap());
169+
if result.is_err() {
170+
println!("Error occurred when dumping. Portfolio not dumped.");
171+
}
172+
} else {
173+
println!("Error occurred when dumping. Portfolio not dumped.");
174+
}
175+
}
176+
125177
fn main() {
126-
let mut assets: Vec<Asset> = vec![
127-
Asset {
128-
ticker: "MSFT".to_string(),
129-
buy_price_cents: 1000,
130-
current_price_cents: 25000,
131-
sell_price_cents: None,
132-
},
133-
Asset {
134-
ticker: "APPL".to_string(),
135-
buy_price_cents: 30000,
136-
current_price_cents: 10000000,
137-
sell_price_cents: Some(40000),
138-
},
139-
];
178+
let mut active_portfolio: Portfolio = Portfolio { assets: vec![] };
140179
let mut input: String;
141180
loop {
142181
print!("» ");
143182
input = read!();
144183
match input.as_str() {
145184
// TODO: handle blank input properly somehow
146-
"assets" => print_assets(&assets),
185+
"assets" => print_assets(&active_portfolio.assets),
147186
"exit" => break,
148-
"new" => assets.push(add_asset()),
187+
"new" => active_portfolio.assets.push(add_asset()),
149188
"help" => print_help(),
189+
"load" => match load_portfolio() {
190+
None => println!("An error occurred when loading portfolio. Portfolio not loaded."),
191+
Some(x) => active_portfolio = x,
192+
},
193+
"dump" => dump_portfolio(&active_portfolio),
150194
_ => println!("Unknown command. Enter 'help' for a list of valid commands"),
151195
}
152196
}

0 commit comments

Comments
 (0)