Strip the brake line at end of file

This commit is contained in:
Kogia-sima 2020-06-07 01:25:52 +09:00
parent f116941904
commit 6d07c43876
2 changed files with 17 additions and 2 deletions

View File

@ -9,7 +9,7 @@ use crate::optimizer::Optimizer;
use crate::parser::Parser;
use crate::resolver::Resolver;
use crate::translator::{Translator, TranslatedSource};
use crate::util::rustfmt_block;
use crate::util::{read_to_string, rustfmt_block};
#[derive(Default)]
pub struct Compiler {
@ -28,7 +28,7 @@ impl Compiler {
fn translate_file_contents(&self, input: &Path) -> Result<TranslatedSource, Error> {
let parser = Parser::new().delimiter(self.config.delimiter);
let translator = Translator::new().escape(self.config.escape);
let content = fs::read_to_string(input)
let content = read_to_string(input)
.chain_err(|| format!("Failed to open template file: {:?}", input))?;
let stream = parser.parse(&*content);

View File

@ -1,6 +1,21 @@
use std::io::{self, Write};
use std::path::Path;
use std::process::{Command, Stdio};
pub fn read_to_string(path: &Path) -> io::Result<String> {
let mut content = std::fs::read_to_string(path)?;
// strip break line at file end
if content.ends_with('\n') {
content.truncate(content.len() - 1);
if content.ends_with('\r') {
content.truncate(content.len() - 1);
}
}
Ok(content)
}
/// Format block expression using `rustfmt` command
pub fn rustfmt_block(source: &str) -> io::Result<String> {
let mut new_source = String::with_capacity(source.len() + 11);