From 86c271eb2f9c0b3e1e2a35d26a2dca37435b5b8c Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 27 Mar 2021 17:02:11 -0600 Subject: why `pub` when you can `pub(crate)`? --- src/args.rs | 32 ++++++++++++++++---------------- src/makefile/command_line.rs | 6 +++--- src/makefile/inference_rules.rs | 8 ++++---- src/makefile/mod.rs | 14 +++++++------- src/makefile/target.rs | 14 +++++++------- src/makefile/token.rs | 22 +++++++++++----------- 6 files changed, 48 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index efc0f11..ae58ce2 100644 --- a/src/args.rs +++ b/src/args.rs @@ -7,11 +7,11 @@ use structopt::StructOpt; #[derive(StructOpt, Debug, PartialEq, Eq, Clone)] #[structopt(author, about)] -pub struct Args { +pub(crate) struct Args { /// Cause environment variables, including those with null values, to override macro /// assignments within makefiles. #[structopt(short, long)] - pub environment_overrides: bool, + pub(crate) environment_overrides: bool, /// Specify a different makefile (or '-' for standard input). /// @@ -27,14 +27,14 @@ pub struct Args { number_of_values = 1, parse(from_os_str) )] - pub makefile: Vec, + pub(crate) makefile: Vec, /// Ignore error codes returned by invoked commands. /// /// This mode is the same as if the special target .IGNORE were specified without /// prerequisites. #[structopt(short, long)] - pub ignore_errors: bool, + pub(crate) ignore_errors: bool, /// Continue to update other targets that do not depend on the current target if a /// non-ignored error occurs while executing the commands to bring a target @@ -45,7 +45,7 @@ pub struct Args { overrides_with = "keep-going", overrides_with = "no-keep-going" )] - pub keep_going: bool, + pub(crate) keep_going: bool, /// Write commands that would be executed on standard output, but do not execute them /// (but execute lines starting with '+'). @@ -59,14 +59,14 @@ pub struct Args { visible_alias = "just-print", visible_alias = "recon" )] - pub dry_run: bool, + pub(crate) dry_run: bool, /// Write to standard output the complete set of macro definitions and target /// descriptions. /// /// The output format is unspecified. #[structopt(short, long, visible_alias = "print-data-base")] - pub print_everything: bool, + pub(crate) print_everything: bool, /// Return a zero exit value if the target file is up-to-date; otherwise, return an /// exit value of 1. @@ -75,11 +75,11 @@ pub struct Args { /// command line (associated with the targets) with a ( '+' ) prefix /// shall be executed. #[structopt(short, long)] - pub question: bool, + pub(crate) question: bool, /// Clear the suffix list and do not use the built-in rules. #[structopt(short = "r", long)] - pub no_builtin_rules: bool, + pub(crate) no_builtin_rules: bool, /// Terminate make if an error occurs while executing the commands to bring a target /// up-to-date (default behavior, required by POSIX to be also a flag for some @@ -94,7 +94,7 @@ pub struct Args { overrides_with = "keep-going", overrides_with = "no-keep-going" )] - pub no_keep_going: bool, + pub(crate) no_keep_going: bool, /// Do not write makefile command lines or touch messages to standard output before /// executing. @@ -102,7 +102,7 @@ pub struct Args { /// This mode shall be the same as if the special target .SILENT were specified /// without prerequisites. #[structopt(short, long, visible_alias = "quiet")] - pub silent: bool, + pub(crate) silent: bool, /// Update the modification time of each target as though a touch target had been /// executed. @@ -113,14 +113,14 @@ pub struct Args { /// the makefile command lines associated with each target are not executed. However, /// a command line with a ( '+' ) prefix shall be executed. #[structopt(short, long)] - pub touch: bool, + pub(crate) touch: bool, /// Target names or macro definitions. /// /// If no target is specified, while make is processing the makefiles, the first /// target that make encounters that is not a special target or an inference rule /// shall be used. - pub targets_or_macros: Vec, + pub(crate) targets_or_macros: Vec, } impl Args { @@ -158,17 +158,17 @@ impl Args { Args::from_iter(args) } - pub fn from_env_and_args() -> Args { + pub(crate) fn from_env_and_args() -> Args { let env_makeflags = env::var("MAKEFLAGS").unwrap_or_default(); let args = env::args_os(); Self::from_given_args_and_given_env(args, env_makeflags) } - pub fn targets(&self) -> impl Iterator { + pub(crate) fn targets(&self) -> impl Iterator { self.targets_or_macros.iter().filter(|x| !x.contains('=')) } - pub fn macros(&self) -> impl Iterator { + pub(crate) fn macros(&self) -> impl Iterator { self.targets_or_macros.iter().filter(|x| x.contains('=')) } } diff --git a/src/makefile/command_line.rs b/src/makefile/command_line.rs index 9cc0b8e..6b42105 100644 --- a/src/makefile/command_line.rs +++ b/src/makefile/command_line.rs @@ -26,7 +26,7 @@ fn execute_command_line(command_line: &str, ignore_errors: bool) -> Result, or the -i option is present, or /// the special target .IGNORE has either the current target as a prerequisite or has /// no prerequisites, any error found while executing the command shall be ignored. @@ -43,7 +43,7 @@ pub struct CommandLine { } impl CommandLine { - pub fn from(mut line: TokenString) -> Self { + pub(crate) fn from(mut line: TokenString) -> Self { let mut ignore_errors = false; let mut silent = false; let mut always_execute = false; @@ -69,7 +69,7 @@ impl CommandLine { } } - pub fn execute(&self, file: &Makefile, target: &Target) { + pub(crate) fn execute(&self, file: &Makefile, target: &Target) { let ignore_error = self.ignore_errors || file.args.ignore_errors || file.special_target_has_prereq(".IGNORE", &target.name); diff --git a/src/makefile/inference_rules.rs b/src/makefile/inference_rules.rs index 3d18730..397008b 100644 --- a/src/makefile/inference_rules.rs +++ b/src/makefile/inference_rules.rs @@ -3,12 +3,12 @@ use std::fmt; use crate::makefile::command_line::CommandLine; #[derive(PartialEq, Eq, Clone, Debug)] -pub struct InferenceRule { +pub(crate) struct InferenceRule { /// POSIX calls this ".s1" but that's not useful. - pub product: String, + pub(crate) product: String, /// POSIX calls this ".s2" but that's not useful. - pub prereq: String, - pub commands: Vec, + pub(crate) prereq: String, + pub(crate) commands: Vec, } impl fmt::Display for InferenceRule { diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index 1ce335d..23243e4 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -29,17 +29,17 @@ enum MacroSource { Builtin, } -pub struct Makefile<'a> { +pub(crate) struct Makefile<'a> { inference_rules: Vec, macros: HashMap, targets: RefCell>>>, - pub first_non_special_target: Option, + pub(crate) first_non_special_target: Option, args: &'a Args, // TODO borrow warnings from Python version } impl<'a> Makefile<'a> { - pub fn new(args: &'a Args) -> Self { + pub(crate) fn new(args: &'a Args) -> Self { let mut inference_rules = vec![]; let mut macros = HashMap::new(); let mut targets = HashMap::new(); @@ -84,7 +84,7 @@ impl<'a> Makefile<'a> { } } - pub fn and_read_file(&mut self, path: impl AsRef) { + pub(crate) fn and_read_file(&mut self, path: impl AsRef) { let file = File::open(path); // TODO handle errors let file = file.expect("couldn't open makefile!"); @@ -92,7 +92,7 @@ impl<'a> Makefile<'a> { self.and_read(file_reader); } - pub fn and_read(&mut self, source: impl BufRead) { + pub(crate) fn and_read(&mut self, source: impl BufRead) { let mut lines_iter = source.lines().enumerate().peekable(); while let Some((line_number, line)) = lines_iter.next() { // TODO handle I/O errors at all @@ -369,7 +369,7 @@ impl<'a> Makefile<'a> { } } - pub fn get_target(&self, name: &str) -> Rc> { + pub(crate) fn get_target(&self, name: &str) -> Rc> { // TODO implement .POSIX let follow_gnu = true; @@ -467,7 +467,7 @@ impl<'a> Makefile<'a> { targets.get(name).expect("Target not found!").clone() } - pub fn update_target(&self, name: &str) { + pub(crate) fn update_target(&self, name: &str) { self.get_target(name).borrow().update(self); } diff --git a/src/makefile/target.rs b/src/makefile/target.rs index 1fbc75b..443eee5 100644 --- a/src/makefile/target.rs +++ b/src/makefile/target.rs @@ -8,11 +8,11 @@ use crate::makefile::command_line::CommandLine; use super::Makefile; #[derive(PartialEq, Eq, Clone, Debug)] -pub struct Target { - pub name: String, - pub prerequisites: Vec, - pub commands: Vec, - pub already_updated: Cell, +pub(crate) struct Target { + pub(crate) name: String, + pub(crate) prerequisites: Vec, + pub(crate) commands: Vec, + pub(crate) already_updated: Cell, } impl Target { @@ -22,7 +22,7 @@ impl Target { .ok() } - pub fn newer_than(&self, other: &Target) -> Option { + pub(crate) fn newer_than(&self, other: &Target) -> Option { let self_updated = self.already_updated.get(); let other_updated = other.already_updated.get(); Some(match (self.modified_time(), other.modified_time()) { @@ -48,7 +48,7 @@ impl Target { exists && newer_than_all_dependencies } - pub fn update(&self, file: &Makefile) { + pub(crate) fn update(&self, file: &Makefile) { for prereq in &self.prerequisites { file.update_target(prereq); } diff --git a/src/makefile/token.rs b/src/makefile/token.rs index 720055f..4fde3e5 100644 --- a/src/makefile/token.rs +++ b/src/makefile/token.rs @@ -12,22 +12,22 @@ use nom::{ }; #[derive(PartialEq, Eq, Clone, Debug)] -pub struct TokenString(Vec); +pub(crate) struct TokenString(Vec); impl TokenString { - pub fn text(text: impl Into) -> Self { + pub(crate) fn text(text: impl Into) -> Self { Self(vec![Token::Text(text.into())]) } - pub fn tokens(&self) -> impl Iterator { + pub(crate) fn tokens(&self) -> impl Iterator { self.0.iter() } - pub fn first_token_mut(&mut self) -> &mut Token { + pub(crate) fn first_token_mut(&mut self) -> &mut Token { &mut self.0[0] } - pub fn split_once(&self, delimiter: char) -> Option<(TokenString, TokenString)> { + pub(crate) fn split_once(&self, delimiter: char) -> Option<(TokenString, TokenString)> { let mut result0 = vec![]; let mut iter = self.0.iter(); while let Some(t) = iter.next() { @@ -47,14 +47,14 @@ impl TokenString { None } - pub fn ends_with(&self, pattern: &str) -> bool { + pub(crate) fn ends_with(&self, pattern: &str) -> bool { match self.0.last() { Some(Token::Text(t)) => t.ends_with(pattern), _ => false, } } - pub fn strip_suffix(&mut self, suffix: &str) { + pub(crate) fn strip_suffix(&mut self, suffix: &str) { if let Some(Token::Text(t)) = self.0.last_mut() { if let Some(x) = t.strip_suffix(suffix) { *t = x.into() @@ -62,11 +62,11 @@ impl TokenString { } } - pub fn extend(&mut self, other: TokenString) { + pub(crate) fn extend(&mut self, other: TokenString) { self.0.extend(other.0); } - pub fn trim_start(&mut self) { + pub(crate) fn trim_start(&mut self) { if let Some(Token::Text(t)) = self.0.first_mut() { *t = t.trim_start().into(); } @@ -83,7 +83,7 @@ impl fmt::Display for TokenString { } #[derive(PartialEq, Eq, Clone, Debug)] -pub enum Token { +pub(crate) enum Token { Text(String), MacroExpansion { name: String, @@ -192,7 +192,7 @@ fn full_text_tokens(input: &str) -> IResult<&str, TokenString> { all_consuming(tokens)(input) } -pub fn tokenize(input: &str) -> TokenString { +pub(crate) fn tokenize(input: &str) -> TokenString { // TODO handle errors gracefully let (_, result) = full_text_tokens(input).expect("couldn't parse"); result -- cgit v1.2.3