From 4f3cbd7f5d71d88767a81f3f2e6a2f41bd3e1ba7 Mon Sep 17 00:00:00 2001 From: Sammi De Guzman Date: Fri, 5 Mar 2021 22:55:01 -0800 Subject: [PATCH] Decode only when converting RTF->JSON and encode JSON->RTF --- src/main.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e34b40d..e85c79f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use std::io; use std::io::{Read, Write}; use std::path::PathBuf; use std::process::exit; -use std::str::FromStr; +use std::str::{FromStr, from_utf8}; use encoding_rs::WINDOWS_1252; use serde_json::Value; @@ -138,9 +138,13 @@ fn run_main() -> Result<(), RtfCreError> { let mut output = File::create(output)?; let mut buf = Vec::new(); input.read_to_end(&mut buf)?; - let (decoded, _, _) = WINDOWS_1252.decode(&buf[..]); - let contents = String::from(decoded); - println!("{:?}", contents); + let contents = match direction { + Direction::RtfToJson => { + let (decoded, _, _) = WINDOWS_1252.decode(&buf[..]); + String::from(decoded) + }, + Direction::JsonToRtf => from_utf8(buf.as_slice()).unwrap().to_string() + }; match direction { Direction::RtfToJson => { let dict = match parse_file(&contents) { @@ -168,7 +172,13 @@ fn run_main() -> Result<(), RtfCreError> { dict.add_entry(String::from(steno), translation.clone(), None); } } - dict.write(&mut output)?; + + let mut buf = Vec::new(); + dict.write(&mut buf)?; + let out = std::str::from_utf8(buf.as_slice()).unwrap(); + let (encoded, _, _) = WINDOWS_1252.encode(out); + output.write(&encoded)?; + Ok(()) }, _ => Err(RtfCreError::JsonParseError),