From 0eb8ad806580a9ad61b752781d8dd75d1fe8c49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Wed, 13 Jul 2022 23:57:20 +0200 Subject: [PATCH] Improve habit writing to files. --- src/habitmgr.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/habitmgr.rs b/src/habitmgr.rs index e0266ea..e8f8b2a 100644 --- a/src/habitmgr.rs +++ b/src/habitmgr.rs @@ -18,7 +18,7 @@ use std::path::PathBuf; use std::fs::{File, OpenOptions}; -use std::io::{BufReader, BufRead}; +use std::io::{BufReader, BufRead, Write}; use crate::habit::Habit; @@ -69,8 +69,8 @@ impl HabitMgr pub fn add(&mut self, name:String, bad:bool, weight:u8, _days:String) { - let habits_file = OpenOptions::new() - .write(true) + // TODO: Refractor so perhaps we import habits first and then export them + let mut habits_file = OpenOptions::new() .append(true) .open(&self.habits_path) .unwrap_or_else(|e| { @@ -78,7 +78,9 @@ impl HabitMgr }); let habit = Habit::new(name.clone(), bad, weight); - serde_json::to_writer(habits_file, &habit) + let mut habit_string = serde_json::to_string(&habit).unwrap(); + habit_string.push_str("\n"); + habits_file.write_all(habit_string.as_bytes()) .unwrap_or_else(|e| { panic!("Error writing to file {}:\n{}", self.habits_path.display(), e);