Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first implementation of getting line from context on warn and error #9

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Makefile {

for (i, result) in stream.lines().enumerate() {
// Set the context line number and extract the line.
self.context.line_number = i + 1;
self.context.line_number = (i + 1) as u64;
let line = result.map_err(|e| MakeError::new(e.to_string(), self.context.clone()))?;

// Parse the line.
Expand Down Expand Up @@ -220,6 +220,8 @@ impl Makefile {
Context {
path: None,
line_number: 0,
row_number: None,
line: None,
},
))
}
Expand Down
9 changes: 7 additions & 2 deletions src/lib/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ use std::path::PathBuf;
#[derive(Clone, Debug)]
pub struct Context {
pub path: Option<PathBuf>,
pub line_number: usize,
// pub row_number: Option(usize),
pub line_number: u64,
pub row_number: Option<u64>,
pub line: Option<String>,
}

impl Context {
pub fn new() -> Self {
Self {
path: None,
line_number: 0,
row_number: None,
line: None,
}
}
}
Expand All @@ -31,6 +34,8 @@ impl From<PathBuf> for Context {
Self {
path: Some(path),
line_number: 0,
row_number: None,
line: None,
}
}
}
32 changes: 30 additions & 2 deletions src/lib/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,47 @@ fn format_log(msg: impl AsRef<str>, level: &str, context: Option<&Context>) -> S
format!("make: {level_display} {context_display}| {}", msg.as_ref())
}

/// Get line content from a context.
/// TODO: improve the implementation of this function.
fn set_line_string_on_context(context: &mut Context) -> () {
if let Some(path) = &context.path {
return if let Ok(lines) = std::fs::read_to_string(path) {
if let Some(line) = lines.lines().nth(context.line_number as usize) {
context.line = Some(line.to_string());
} else {
context.line = None;
}
} else {
context.line = None;
};
}
}

/// Helper to format info.
fn format_info(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "INFO", context)
}

/// Helper to format warnings.
fn format_warn(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "WARN", context)
if let Some(context) = context {
let mut context = context.clone();
set_line_string_on_context(&mut context);
format_log(msg, "WARN", Some(&context))
} else {
format_log(msg, "WARN", None)
}
}

/// Helper to format errors.
fn format_err(msg: impl AsRef<str>, context: Option<&Context>) -> String {
format_log(msg, "ERROR", context)
if let Some(context) = context {
let mut context = context.clone();
set_line_string_on_context(&mut context);
format_log(msg, "ERROR", Some(&context))
} else {
format_log(msg, "ERROR", None)
}
}

/// Helper to log info to STDERR.
Expand Down