uucore_procs: separate file opening & parsing

This commit is contained in:
Daniel Hofstetter 2023-02-17 09:25:32 +01:00
parent 9fdce975fd
commit 94eddc4701

View file

@ -61,7 +61,7 @@ fn render_markdown(s: &str) -> String {
pub fn help_usage(input: TokenStream) -> TokenStream { pub fn help_usage(input: TokenStream) -> TokenStream {
let input: Vec<TokenTree> = input.into_iter().collect(); let input: Vec<TokenTree> = input.into_iter().collect();
let filename = get_argument(&input, 0, "filename"); let filename = get_argument(&input, 0, "filename");
let text: String = parse_usage(&parse_help("usage", &filename)); let text: String = parse_usage(&parse_help_section("usage", &read_help(&filename)));
TokenTree::Literal(Literal::string(&text)).into() TokenTree::Literal(Literal::string(&text)).into()
} }
@ -94,7 +94,7 @@ pub fn help_section(input: TokenStream) -> TokenStream {
let input: Vec<TokenTree> = input.into_iter().collect(); let input: Vec<TokenTree> = input.into_iter().collect();
let section = get_argument(&input, 0, "section"); let section = get_argument(&input, 0, "section");
let filename = get_argument(&input, 1, "filename"); let filename = get_argument(&input, 1, "filename");
let text = parse_help(&section, &filename); let text = parse_help_section(&section, &read_help(&filename));
let rendered = render_markdown(&text); let rendered = render_markdown(&text);
TokenTree::Literal(Literal::string(&rendered)).into() TokenTree::Literal(Literal::string(&rendered)).into()
} }
@ -121,13 +121,11 @@ fn get_argument(input: &[TokenTree], index: usize, name: &str) -> String {
.to_string() .to_string()
} }
/// Read the help file and extract a section /// Read the help file
fn parse_help(section: &str, filename: &str) -> String { fn read_help(filename: &str) -> String {
let section = section.to_lowercase();
let section = section.trim_matches('"');
let mut content = String::new(); let mut content = String::new();
let mut path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let mut path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
path.push(filename); path.push(filename);
File::open(path) File::open(path)
@ -135,7 +133,7 @@ fn parse_help(section: &str, filename: &str) -> String {
.read_to_string(&mut content) .read_to_string(&mut content)
.unwrap(); .unwrap();
parse_help_section(section, &content) content
} }
/// Get a single section from content /// Get a single section from content
@ -147,6 +145,8 @@ fn parse_help_section(section: &str, content: &str) -> String {
.map_or(false, |l| l.trim().to_lowercase() == section) .map_or(false, |l| l.trim().to_lowercase() == section)
} }
let section = &section.to_lowercase();
// We cannot distinguish between an empty or non-existing section below, // We cannot distinguish between an empty or non-existing section below,
// so we do a quick test to check whether the section exists to provide // so we do a quick test to check whether the section exists to provide
// a nice error message. // a nice error message.
@ -209,6 +209,10 @@ mod tests {
parse_help_section("some section", input), parse_help_section("some section", input),
"This is some section" "This is some section"
); );
assert_eq!(
parse_help_section("SOME SECTION", input),
"This is some section"
);
assert_eq!( assert_eq!(
parse_help_section("another section", input), parse_help_section("another section", input),
"This is the other section\nwith multiple lines" "This is the other section\nwith multiple lines"