uucore_procs-0.0.23/.cargo_vcs_info.json0000644000000001560000000000100136300ustar { "git": { "sha1": "a3af2230a64d3b60832ce9944df5388a5ba9128a" }, "path_in_vcs": "src/uucore_procs" }uucore_procs-0.0.23/Cargo.toml0000644000000017630000000000100116330ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" name = "uucore_procs" version = "0.0.23" authors = ["Roy Ivy III "] description = "uutils ~ 'uucore' proc-macros" homepage = "https://github.com/uutils/coreutils" keywords = [ "cross-platform", "proc-macros", "uucore", "uutils", ] license = "MIT" repository = "https://github.com/uutils/coreutils/tree/main/src/uucore_procs" [lib] proc-macro = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.uuhelp_parser] version = "0.0.23" uucore_procs-0.0.23/Cargo.toml.orig000064400000000000000000000011121046102023000153000ustar 00000000000000# spell-checker:ignore uuhelp [package] name = "uucore_procs" version = "0.0.23" authors = ["Roy Ivy III "] license = "MIT" description = "uutils ~ 'uucore' proc-macros" homepage = "https://github.com/uutils/coreutils" repository = "https://github.com/uutils/coreutils/tree/main/src/uucore_procs" # readme = "README.md" keywords = ["cross-platform", "proc-macros", "uucore", "uutils"] # categories = ["os"] edition = "2021" [lib] proc-macro = true [dependencies] proc-macro2 = "1.0" quote = "1.0" uuhelp_parser = { path = "../uuhelp_parser", version = "0.0.23" } uucore_procs-0.0.23/LICENSE000064400000000000000000000020551046102023000134250ustar 00000000000000Copyright (c) Jordi Boggiano and many others Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. uucore_procs-0.0.23/src/lib.rs000064400000000000000000000127421046102023000143270ustar 00000000000000// This file is part of the uutils coreutils package. // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. // // spell-checker:ignore backticks uuhelp use std::{fs::File, io::Read, path::PathBuf}; use proc_macro::{Literal, TokenStream, TokenTree}; use quote::quote; //## rust proc-macro background info //* ref: @@ //* ref: [path construction from LitStr](https://oschwald.github.io/maxminddb-rust/syn/struct.LitStr.html) @@ #[proc_macro_attribute] pub fn main(_args: TokenStream, stream: TokenStream) -> TokenStream { let stream = proc_macro2::TokenStream::from(stream); let new = quote!( pub fn uumain(args: impl uucore::Args) -> i32 { #stream let result = uumain(args); match result { Ok(()) => uucore::error::get_exit_code(), Err(e) => { let s = format!("{}", e); if s != "" { uucore::show_error!("{}", s); } if e.usage() { eprintln!("Try '{} --help' for more information.", uucore::execution_phrase()); } e.code() } } } ); TokenStream::from(new) } // FIXME: This is currently a stub. We could do much more here and could // even pull in a full markdown parser to get better results. /// Render markdown into a format that's easier to read in the terminal. /// /// For now, all this function does is remove backticks. /// Some ideas for future improvement: /// - Render headings as bold /// - Convert triple backticks to indented /// - Printing tables in a nice format fn render_markdown(s: &str) -> String { s.replace('`', "") } /// Get the about text from the help file. /// /// The about text is assumed to be the text between the first markdown /// code block and the next header, if any. It may span multiple lines. #[proc_macro] pub fn help_about(input: TokenStream) -> TokenStream { let input: Vec = input.into_iter().collect(); let filename = get_argument(&input, 0, "filename"); let text: String = uuhelp_parser::parse_about(&read_help(&filename)); TokenTree::Literal(Literal::string(&text)).into() } /// Get the usage from the help file. /// /// The usage is assumed to be surrounded by markdown code fences. It may span /// multiple lines. The first word of each line is assumed to be the name of /// the util and is replaced by "{}" so that the output of this function can be /// used with `uucore::format_usage`. #[proc_macro] pub fn help_usage(input: TokenStream) -> TokenStream { let input: Vec = input.into_iter().collect(); let filename = get_argument(&input, 0, "filename"); let text: String = uuhelp_parser::parse_usage(&read_help(&filename)); TokenTree::Literal(Literal::string(&text)).into() } /// Reads a section from a file of the util as a `str` literal. /// /// It reads from the file specified as the second argument, relative to the /// crate root. The contents of this file are read verbatim, without parsing or /// escaping. The name of the help file should match the name of the util. /// I.e. numfmt should have a file called `numfmt.md`. By convention, the file /// should start with a top-level section with the name of the util. The other /// sections must start with 2 `#` characters. Capitalization of the sections /// does not matter. Leading and trailing whitespace of each section will be /// removed. /// /// Example: /// ```md /// # numfmt /// ## About /// Convert numbers from/to human-readable strings /// /// ## Long help /// This text will be the long help /// ``` /// /// ```rust,ignore /// help_section!("about", "numfmt.md"); /// ``` #[proc_macro] pub fn help_section(input: TokenStream) -> TokenStream { let input: Vec = input.into_iter().collect(); let section = get_argument(&input, 0, "section"); let filename = get_argument(&input, 1, "filename"); if let Some(text) = uuhelp_parser::parse_section(§ion, &read_help(&filename)) { let rendered = render_markdown(&text); TokenTree::Literal(Literal::string(&rendered)).into() } else { panic!( "The section '{section}' could not be found in the help file. Maybe it is spelled wrong?" ) } } /// Get an argument from the input vector of `TokenTree`. /// /// Asserts that the argument is a string literal and returns the string value, /// otherwise it panics with an error. fn get_argument(input: &[TokenTree], index: usize, name: &str) -> String { // Multiply by two to ignore the `','` in between the arguments let string = match &input.get(index * 2) { Some(TokenTree::Literal(lit)) => lit.to_string(), Some(_) => panic!("Argument {index} should be a string literal."), None => panic!("Missing argument at index {index} for {name}"), }; string .parse::() .unwrap() .strip_prefix('"') .unwrap() .strip_suffix('"') .unwrap() .to_string() } /// Read the help file fn read_help(filename: &str) -> String { let mut content = String::new(); let mut path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); path.push(filename); File::open(path) .unwrap() .read_to_string(&mut content) .unwrap(); content }