pub struct Rule(/* private fields */);Expand description
An AST node for $ast
Implementations§
Source§impl Rule
impl Rule
Sourcepub fn targets(&self) -> impl Iterator<Item = String> + '_
pub fn targets(&self) -> impl Iterator<Item = String> + '_
Targets of this rule
§Example
use makefile_lossless::Rule;
let rule: Rule = "rule: dependency\n\tcommand".parse().unwrap();
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["rule"]);Sourcepub fn prerequisites(&self) -> impl Iterator<Item = String> + '_
pub fn prerequisites(&self) -> impl Iterator<Item = String> + '_
Get the prerequisites in the rule
§Example
use makefile_lossless::Rule;
let rule: Rule = "rule: dependency\n\tcommand".parse().unwrap();
assert_eq!(rule.prerequisites().collect::<Vec<_>>(), vec!["dependency"]);Sourcepub fn recipes(&self) -> impl Iterator<Item = String>
pub fn recipes(&self) -> impl Iterator<Item = String>
Get the commands in the rule
§Example
use makefile_lossless::Rule;
let rule: Rule = "rule: dependency\n\tcommand".parse().unwrap();
assert_eq!(rule.recipes().collect::<Vec<_>>(), vec!["command"]);Sourcepub fn replace_command(&mut self, i: usize, line: &str) -> bool
pub fn replace_command(&mut self, i: usize, line: &str) -> bool
Replace the command at index i with a new line
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "rule: dependency\n\tcommand".parse().unwrap();
rule.replace_command(0, "new command");
assert_eq!(rule.recipes().collect::<Vec<_>>(), vec!["new command"]);Sourcepub fn push_command(&mut self, line: &str)
pub fn push_command(&mut self, line: &str)
Add a new command to the rule
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "rule: dependency\n\tcommand".parse().unwrap();
rule.push_command("command2");
assert_eq!(rule.recipes().collect::<Vec<_>>(), vec!["command", "command2"]);Sourcepub fn remove_command(&mut self, index: usize) -> bool
pub fn remove_command(&mut self, index: usize) -> bool
Remove command at given index
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "rule:\n\tcommand1\n\tcommand2\n".parse().unwrap();
rule.remove_command(0);
assert_eq!(rule.recipes().collect::<Vec<_>>(), vec!["command2"]);Sourcepub fn insert_command(&mut self, index: usize, line: &str) -> bool
pub fn insert_command(&mut self, index: usize, line: &str) -> bool
Insert command at given index
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "rule:\n\tcommand1\n\tcommand2\n".parse().unwrap();
rule.insert_command(1, "inserted_command");
let recipes: Vec<_> = rule.recipes().collect();
assert_eq!(recipes, vec!["command1", "inserted_command", "command2"]);Sourcepub fn recipe_count(&self) -> usize
pub fn recipe_count(&self) -> usize
Get the number of commands/recipes in this rule
§Example
use makefile_lossless::Rule;
let rule: Rule = "rule:\n\tcommand1\n\tcommand2\n".parse().unwrap();
assert_eq!(rule.recipe_count(), 2);Sourcepub fn clear_commands(&mut self)
pub fn clear_commands(&mut self)
Clear all commands from this rule
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "rule:\n\tcommand1\n\tcommand2\n".parse().unwrap();
rule.clear_commands();
assert_eq!(rule.recipe_count(), 0);Sourcepub fn remove_prerequisite(&mut self, target: &str) -> Result<bool, Error>
pub fn remove_prerequisite(&mut self, target: &str) -> Result<bool, Error>
Remove a prerequisite from this rule
Returns true if the prerequisite was found and removed, false if it wasn’t found.
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "target: dep1 dep2 dep3\n".parse().unwrap();
assert!(rule.remove_prerequisite("dep2").unwrap());
assert_eq!(rule.prerequisites().collect::<Vec<_>>(), vec!["dep1", "dep3"]);
assert!(!rule.remove_prerequisite("nonexistent").unwrap());Sourcepub fn add_prerequisite(&mut self, target: &str) -> Result<(), Error>
pub fn add_prerequisite(&mut self, target: &str) -> Result<(), Error>
Add a prerequisite to this rule
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "target: dep1\n".parse().unwrap();
rule.add_prerequisite("dep2").unwrap();
assert_eq!(rule.prerequisites().collect::<Vec<_>>(), vec!["dep1", "dep2"]);Sourcepub fn set_prerequisites(&mut self, prereqs: Vec<&str>) -> Result<(), Error>
pub fn set_prerequisites(&mut self, prereqs: Vec<&str>) -> Result<(), Error>
Set the prerequisites for this rule, replacing any existing ones
§Example
use makefile_lossless::Rule;
let mut rule: Rule = "target: old_dep\n".parse().unwrap();
rule.set_prerequisites(vec!["new_dep1", "new_dep2"]).unwrap();
assert_eq!(rule.prerequisites().collect::<Vec<_>>(), vec!["new_dep1", "new_dep2"]);Sourcepub fn remove(self) -> Result<(), Error>
pub fn remove(self) -> Result<(), Error>
Remove this rule from its parent Makefile
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "rule1:\n\tcommand1\nrule2:\n\tcommand2\n".parse().unwrap();
let rule = makefile.rules().next().unwrap();
rule.remove().unwrap();
assert_eq!(makefile.rules().count(), 1);This will also remove any preceding comments and up to 1 empty line before the rule.
Trait Implementations§
impl Eq for Rule
impl StructuralPartialEq for Rule
Auto Trait Implementations§
impl Freeze for Rule
impl !RefUnwindSafe for Rule
impl !Send for Rule
impl !Sync for Rule
impl Unpin for Rule
impl !UnwindSafe for Rule
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more