Skip to content

Commit

Permalink
Set default log level when not using RUST_LOG (#206)
Browse files Browse the repository at this point in the history
* set default log level when not using RUST_LOG

* update error output for parsing failures

* Use adaptive format for coloring when possible

* remove dead_code attribute
  • Loading branch information
darrell-roberts authored Nov 13, 2024
1 parent 330a40f commit dce5b85
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
20 changes: 13 additions & 7 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use std::{
use anyhow::{anyhow, Context};
use clap::{CommandFactory, Parser};
use clap_complete::Generator;
use flexi_logger::AdaptiveFormat;
use ignore::{overrides::OverrideBuilder, types::TypesBuilder, WalkBuilder};
use log::error;
use log::{error, info};
use rayon::iter::ParallelBridge;
#[cfg(feature = "go")]
use typeshare_core::language::Go;
Expand All @@ -37,13 +38,15 @@ use crate::{
};

fn main() -> anyhow::Result<()> {
flexi_logger::Logger::try_with_env()
.unwrap()
.start()
.unwrap();
flexi_logger::Logger::try_with_env_or_str("info")?
.adaptive_format_for_stderr(AdaptiveFormat::Detailed)
.adaptive_format_for_stdout(AdaptiveFormat::Detailed)
.start()?;

let options = Args::parse();

info!("typeshare started generating types");

if let Some(options) = options.subcommand {
match options {
Command::Completions { shell } => {
Expand Down Expand Up @@ -153,13 +156,15 @@ fn main() -> anyhow::Result<()> {
};

check_parse_errors(&crate_parsed_data)?;

write_generated(
destination,
lang.as_mut(),
crate_parsed_data,
import_candidates,
)?;

info!("typeshare finished generating types");
Ok(())
}

Expand Down Expand Up @@ -262,13 +267,14 @@ fn check_parse_errors(parsed_crates: &BTreeMap<CrateName, ParsedData>) -> anyhow
errors_encountered = true;
for error in &data.errors {
error!(
"Parsing error: \"{}\" in crate \"{}\" for file \"{}\"",
error.error, error.crate_name, error.file_name
"Parsing error: \"{}\" in file \"{}\"",
error.error, error.file_name
);
}
}

if errors_encountered {
error!("Errors encountered during parsing.");
Err(anyhow!("Errors encountered during parsing."))
} else {
Ok(())
Expand Down
2 changes: 0 additions & 2 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ pub enum ParseError {
/// Error with it's related data.
#[derive(Debug)]
pub struct ErrorInfo {
/// The crate where this error occurred.
pub crate_name: CrateName,
/// The file name being parsed.
pub file_name: String,
/// The parse error.
Expand Down
4 changes: 1 addition & 3 deletions core/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const IGNORED_TYPES: &[&str] = &["Option", "String", "Vec", "HashMap", "T", "I54
#[derive(Default)]
pub struct TypeShareVisitor<'a> {
parsed_data: ParsedData,
#[allow(dead_code)]
file_path: PathBuf,
ignored_types: &'a [&'a str],
target_os: &'a [String],
Expand Down Expand Up @@ -92,8 +91,7 @@ impl<'a> TypeShareVisitor<'a> {
match result {
Ok(data) => self.parsed_data.push(data),
Err(error) => self.parsed_data.errors.push(ErrorInfo {
crate_name: self.parsed_data.crate_name.clone(),
file_name: self.parsed_data.file_name.clone(),
file_name: self.file_path.to_string_lossy().into_owned(),
error,
}),
}
Expand Down

0 comments on commit dce5b85

Please sign in to comment.