use std::fs;
use std::process;
use std::process::Command;
use std::error::Error;
use crate::process::Output;
pub struct Config {
pub query: String,
pub file_path: String,
pub repo_path: String,
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
let path : Output =
if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "cd"])
.output()
.expect("failed to execute process")
} else
if cfg!(target_os = "macos"){
Command::new("sh")
.arg("-c")
.arg("pwd")
.output()
.expect("failed to execute process")
} else
if cfg!(target_os = "linux"){
Command::new("sh")
.arg("-c")
.arg("pwd")
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("pwd")
.output()
.expect("failed to execute process")
};
let repo_path = String::from_utf8(path.stdout)
.map_err(|non_utf8| String::from_utf8_lossy(non_utf8.as_bytes()).into_owned())
.unwrap();
#[cfg(debug_assertions)]
println!("repo_path={:?}", repo_path);
#[cfg(debug_assertions)]
let apple: String = String::from("apple");
#[cfg(debug_assertions)]
let banana: String = String::from("banana");
#[cfg(debug_assertions)]
println!("{}", apple.ne(&banana));
#[cfg(debug_assertions)]
println!("{}", apple.eq(&banana));
let _ferris: String = String::from("Ferris");
let _frank: String = String::from("Frank");
let _bob: String = String::from("Bob");
let install: String = String::from("install");let gnostr_cat: String = String::from("gnostr-cat");
let gnostr_cli: String = String::from("gnostr-cli");
let gnostr_command: String = String::from("gnostr-command"); let gnostr_grep: String = String::from("gnostr-grep");
let gnostr_legit: String = String::from("gnostr-legit");
let gnostr_sha256: String = String::from("gnostr-sha256");
if args.len() > 1 {
let content = String::from(&args[1].clone());
for _arg in args.iter() {
#[cfg(debug_assertions)]
println!("_arg=apple:{}", _arg.eq(&apple));
#[cfg(debug_assertions)]
println!("_arg=banana:{}", _arg.eq(&banana));
if args.len() == 3 {if content.eq(&install) {
#[cfg(debug_assertions)]
println!("exec install sub argparse {:?}!", _arg);
let content = String::from(&args[2].clone());
if content.eq(&gnostr_cat) {
#[cfg(debug_assertions)]
println!("exec gnostr-cat install {:?}!", args);
}
if content.eq(&gnostr_cli) {
#[cfg(debug_assertions)]
println!("exec gnostr-cli install {:?}!", args);
}
if content.eq(&gnostr_command) {
#[cfg(debug_assertions)]
println!("exec gnostr-command install {:?}!", args);
}
if content.eq(&gnostr_grep) {
#[cfg(debug_assertions)]
println!("exec gnostr-grep install {:?}!", args);
}
if content.eq(&gnostr_legit) {
#[cfg(debug_assertions)]
println!("exec gnostr-legit install {:?}!", args);
}
if content.eq(&gnostr_sha256) {
#[cfg(debug_assertions)]
println!("exec gnostr-sha256 install {:?}!", args);
}
}}let content = String::from(&args[1].clone());
if content.eq(&_ferris) {
#[cfg(debug_assertions)]
println!("Matched {:?}!", _arg);
println!("Ferris:Hello {}", _ferris);
}
if content.eq(&_frank) {
#[cfg(debug_assertions)]
println!("Matched {:?}!", _arg);
println!("Frank:Hello {}", _frank);
}
if content.eq(&_bob) {
#[cfg(debug_assertions)]
println!("Matched {:?}!", _arg);
println!("Bob:Hello {}", _bob);
}
}}
if args.len() == 3 {
let query = args[1].clone();
let file_path = args[2].clone();
if query.contains(&install) {
#[cfg(debug_assertions)]
println!("_arg=query:{}", query);
#[cfg(debug_assertions)]
println!("_arg=install:{}", install);
#[cfg(debug_assertions)]
println!("query=install:{}", query.eq(&install));
Ok(Config { query, file_path, repo_path })
} else {
let query = args[1].clone();
let file_path = args[2].clone();
Ok(Config { query, file_path, repo_path })
}
} else { process::exit(0); }
}
}
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
if contents.contains("nostr") {
println!("___________gnostr___________:contents={}", contents);
Ok(())
} else {
println!("contents={}", contents);
println!("&config.query={}", &config.query);
for line in search(&config.query, &contents) {
println!("{line}");
}
Ok(())
}
}
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let q = fs::read_to_string(query);
println!("_________q________={:?}", q);
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
}