From d44118cdffc9fb3c378886cb638bb6b22152ae2b Mon Sep 17 00:00:00 2001 From: Louis Capitanchik <contact@louiscap.co> Date: Tue, 2 Aug 2022 01:40:30 +0100 Subject: [PATCH] Write output --- src/main.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 181edc7..be0cfd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use lol_html::{element, rewrite_str, text, RewriteStrSettings}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fs::File; -use std::io::Read; +use std::io::{Read, Write}; use std::sync::{Arc, Mutex}; use thiserror::Error; @@ -47,6 +47,8 @@ pub enum AppError { InvalidOpfile(toml::de::Error), #[error("Failed to match selector {0}")] SelectorFailed(String), + #[error("Failed to apply edits {0}")] + EditsFailed(#[from] lol_html::errors::RewritingError), } pub type AppResult<T> = Result<T, AppError>; @@ -132,7 +134,20 @@ fn main() -> AppResult<()> { }, ); - println!("{}", html.unwrap()); + let write_path = if args.inline { + Some(args.input.clone()) + } else { + args.output + }; + + if let Some(path) = write_path { + File::create(&path) + .map_err(|_| AppError::FailedToOpen(args.input.clone()))? + .write_all(html?.as_bytes()) + .map_err(|_| AppError::FailedToRead(args.input.clone()))?; + } else { + println!("{}", html?); + } Ok(()) } -- GitLab