Some intro i didn't write
TODO: the thing we leave in which we never fix
We will write it in two phases
- Saving the data
- The main control file
The projector file
this is where i feel our good decisions start to compound
pub fn save(&self) -> Result<()> {
let result = serde_json::to_string(&self.data)?;
// todo... mkdir
if let Some(p) = self.config.config.parent() {
if std::fs::metadata(p).is_err() {
std::fs::create_dir_all(p).ok();
}
}
std::fs::write(&self.config.config, result)?;
return Ok(());
}
The main file
Tying it all together
use clap::StructOpt;
use rust::{opts::ProjectorOpts, config::{ProjectorConfig, Operation}, error::ProjectorError, projector::Projector};
fn main() -> Result<(), ProjectorError> {
let config: ProjectorConfig = ProjectorOpts::parse().try_into()?;
let mut proj = Projector::load_from_disk(&config)?;
match &config.operation {
Operation::Add((k, v)) => {
proj.set_value(k, v.clone());
proj.save(&config)?;
},
Operation::Print(k) => {
match proj.get_value(k) {
Some(v) => println!("{}", v),
None => eprintln!("key {} not found", k),
}
}
Operation::Remove(k) => {
proj.remove_value(k);
proj.save(&config)?;
}
}
return Ok(());
}
Wowza!
We actually finished!