22 lines
724 B
Rust
22 lines
724 B
Rust
use std::process::Command;
|
|
use std::str::FromStr;
|
|
|
|
pub fn get_line_count(filename: &str) -> Result<usize, Box<dyn std::error::Error>> {
|
|
// Run "wc -l" on the file.
|
|
let output = Command::new("wc")
|
|
.arg("-l")
|
|
.arg(filename)
|
|
.output()?;
|
|
|
|
if !output.status.success() {
|
|
return Err(format!("wc command failed with status: {:?}", output.status).into());
|
|
}
|
|
|
|
// Convert output to a String.
|
|
let stdout = String::from_utf8(output.stdout)?;
|
|
// The output format is typically "1234 filename", so split and parse the first token.
|
|
let count_str = stdout.split_whitespace().next().ok_or("Invalid output from wc")?;
|
|
let count = usize::from_str(count_str)?;
|
|
|
|
Ok(count)
|
|
} |