From ccdd62b03252c7fe9a40bd6f0b5f4e2c6fb30b7e Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Wed, 19 Jan 2022 00:04:27 +0800 Subject: [PATCH 1/3] fix: always panic error --- src/cli/translate/mod.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cli/translate/mod.rs b/src/cli/translate/mod.rs index 60a3361d7..529a7548c 100644 --- a/src/cli/translate/mod.rs +++ b/src/cli/translate/mod.rs @@ -3,6 +3,7 @@ use html_parser::Node; use std::fmt::Write; use std::io::Read; use std::path::PathBuf; +use std::process::exit; use structopt::StructOpt; pub mod extract_svgs; @@ -33,6 +34,7 @@ pub struct Translate { impl Translate { pub fn translate(self) -> anyhow::Result<()> { + let Translate { component: as_component, input, @@ -40,21 +42,29 @@ impl Translate { source, } = self; - let contents = input + let contents; + let temp = input .map(|f| { - std::fs::read_to_string(&f) - .unwrap_or_else(|e| panic!("Could not read input file: {}", e)) - }) - .unwrap_or(source.unwrap_or_else(|| { - if atty::is(atty::Stream::Stdin) { - panic!("No input file, source, or stdin to translate from"); - } + std::fs::read_to_string(&f).unwrap_or_else(|e| { + // panic!("Could not read input file: {}", e) + log::error!("Cloud not read input file: {}.", e); + exit(0); + }) + }); + if let None = temp { + if atty::is(atty::Stream::Stdin) { + // panic!("No input file, source, or stdin to translate from"); + log::error!("No input file, source, or stdin to translate from."); + exit(0); + } - let mut buffer = String::new(); - std::io::stdin().read_to_string(&mut buffer).unwrap(); + let mut buffer = String::new(); + std::io::stdin().read_to_string(&mut buffer).unwrap(); - buffer.trim().to_string() - })); + contents = buffer.trim().to_string(); + } else { + contents = temp.unwrap(); + } // parse the input as html and prepare the output let dom = Dom::parse(&contents)?; From 7f53b7de6aba13eafad124fd781ddc8900e62df5 Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Wed, 19 Jan 2022 00:42:43 +0800 Subject: [PATCH 2/3] fix: fmt code --- src/buildsystem/wasm.rs | 1 + src/cli/translate/extract_svgs.rs | 1 + src/cli/translate/mod.rs | 38 ++++++++++++++++--------------- src/cli/watch/mod.rs | 1 + src/main.rs | 27 ++++++++++------------ src/watch.rs | 1 + 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/buildsystem/wasm.rs b/src/buildsystem/wasm.rs index e69de29bb..8b1378917 100644 --- a/src/buildsystem/wasm.rs +++ b/src/buildsystem/wasm.rs @@ -0,0 +1 @@ + diff --git a/src/cli/translate/extract_svgs.rs b/src/cli/translate/extract_svgs.rs index e69de29bb..8b1378917 100644 --- a/src/cli/translate/extract_svgs.rs +++ b/src/cli/translate/extract_svgs.rs @@ -0,0 +1 @@ + diff --git a/src/cli/translate/mod.rs b/src/cli/translate/mod.rs index 529a7548c..a8d5352ae 100644 --- a/src/cli/translate/mod.rs +++ b/src/cli/translate/mod.rs @@ -34,7 +34,6 @@ pub struct Translate { impl Translate { pub fn translate(self) -> anyhow::Result<()> { - let Translate { component: as_component, input, @@ -43,25 +42,28 @@ impl Translate { } = self; let contents; - let temp = input - .map(|f| { - std::fs::read_to_string(&f).unwrap_or_else(|e| { - // panic!("Could not read input file: {}", e) - log::error!("Cloud not read input file: {}.", e); - exit(0); - }) - }); - if let None = temp { - if atty::is(atty::Stream::Stdin) { - // panic!("No input file, source, or stdin to translate from"); - log::error!("No input file, source, or stdin to translate from."); + let temp = input.map(|f| { + std::fs::read_to_string(&f).unwrap_or_else(|e| { + // panic!("Could not read input file: {}", e) + log::error!("Cloud not read input file: {}.", e); exit(0); + }) + }); + if let None = temp { + if let Some(s) = source { + contents = s; + } else { + if atty::is(atty::Stream::Stdin) { + // panic!("No input file, source, or stdin to translate from"); + log::error!("No input file, source, or stdin to translate from."); + exit(0); + } + + let mut buffer = String::new(); + std::io::stdin().read_to_string(&mut buffer).unwrap(); + + contents = buffer.trim().to_string(); } - - let mut buffer = String::new(); - std::io::stdin().read_to_string(&mut buffer).unwrap(); - - contents = buffer.trim().to_string(); } else { contents = temp.unwrap(); } diff --git a/src/cli/watch/mod.rs b/src/cli/watch/mod.rs index e69de29bb..8b1378917 100644 --- a/src/cli/watch/mod.rs +++ b/src/cli/watch/mod.rs @@ -0,0 +1 @@ + diff --git a/src/main.rs b/src/main.rs index 7ecb50ac7..798c492dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,24 +9,21 @@ async fn main() -> Result<()> { match args.action { Commands::Translate(opts) => { opts.translate(); - } + } // Commands::Build(_) => { + // // + // } - // Commands::Build(_) => { - // // - // } + // Commands::Clean(_) => { + // // + // } + // Commands::Config(_) => { + // // + // } - // Commands::Clean(_) => { - // // - // } - - // Commands::Config(_) => { - // // - // } - - // Commands::Serve(_) => { - // // - // } + // Commands::Serve(_) => { + // // + // } } Ok(()) diff --git a/src/watch.rs b/src/watch.rs index e69de29bb..8b1378917 100644 --- a/src/watch.rs +++ b/src/watch.rs @@ -0,0 +1 @@ + From 91907d21d249425e1a0dae40991664ae6e850812 Mon Sep 17 00:00:00 2001 From: mrxiaozhuox Date: Wed, 19 Jan 2022 00:47:48 +0800 Subject: [PATCH 3/3] feat: commit code --- src/cli/translate/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cli/translate/mod.rs b/src/cli/translate/mod.rs index a8d5352ae..5799cad02 100644 --- a/src/cli/translate/mod.rs +++ b/src/cli/translate/mod.rs @@ -44,7 +44,6 @@ impl Translate { let contents; let temp = input.map(|f| { std::fs::read_to_string(&f).unwrap_or_else(|e| { - // panic!("Could not read input file: {}", e) log::error!("Cloud not read input file: {}.", e); exit(0); }) @@ -54,7 +53,6 @@ impl Translate { contents = s; } else { if atty::is(atty::Stream::Stdin) { - // panic!("No input file, source, or stdin to translate from"); log::error!("No input file, source, or stdin to translate from."); exit(0); }