Skip to content

Commit

Permalink
Decode only when converting RTF->JSON and encode JSON->RTF
Browse files Browse the repository at this point in the history
  • Loading branch information
sammdot committed Mar 6, 2021
1 parent 848e02e commit 4f3cbd7
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 4f3cbd7

Please sign in to comment.