Rule

Struct Rule 

Source
pub struct Rule(/* private fields */);
Expand description

An AST node for $ast

Implementations§

Source§

impl Rule

Source

pub fn parse(text: &str) -> Parse<Rule>

Parse rule text, returning a Parse result

Source

pub fn new(targets: &[&str], prerequisites: &[&str], recipes: &[&str]) -> Rule

Create a new rule with the given targets, prerequisites, and recipes

§Arguments
  • targets - A slice of target names
  • prerequisites - A slice of prerequisite names (can be empty)
  • recipes - A slice of recipe lines (can be empty)
§Example
use makefile_lossless::Rule;

let rule = Rule::new(&["all"], &["build", "test"], &["echo Done"]);
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["all"]);
assert_eq!(rule.prerequisites().collect::<Vec<_>>(), vec!["build", "test"]);
assert_eq!(rule.recipes().collect::<Vec<_>>(), vec!["echo Done"]);
Source

pub fn parent(&self) -> Option<MakefileItem>

Get the parent item of this rule, if any

Returns Some(MakefileItem) if this rule has a parent that is a MakefileItem (e.g., a Conditional), or None if the parent is the root Makefile node.

§Example
use makefile_lossless::Makefile;

let makefile: Makefile = r#"ifdef DEBUG
all:
    echo "test"
endif
"#.parse().unwrap();

let cond = makefile.conditionals().next().unwrap();
let rule = cond.if_items().next().unwrap();
// Rule's parent is the conditional
assert!(matches!(rule, makefile_lossless::MakefileItem::Rule(_)));
Source

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"]);
Source

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"]);
Source

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"]);
Source

pub fn recipe_nodes(&self) -> impl Iterator<Item = Recipe>

Get recipe nodes with line/column information

Returns an iterator over Recipe AST nodes, which support the line(), column(), and line_col() methods to get position information.

§Example
use makefile_lossless::Rule;

let rule_text = "test:\n\techo line1\n\techo line2\n";
let rule: Rule = rule_text.parse().unwrap();

let recipe_nodes: Vec<_> = rule.recipe_nodes().collect();
assert_eq!(recipe_nodes.len(), 2);
assert_eq!(recipe_nodes[0].text(), "echo line1");
assert_eq!(recipe_nodes[0].line(), 1); // 0-indexed
assert_eq!(recipe_nodes[1].text(), "echo line2");
assert_eq!(recipe_nodes[1].line(), 2);
Source

pub fn items(&self) -> impl Iterator<Item = RuleItem> + '_

Get all items (recipe lines and conditionals) in the rule’s body

This method iterates through the rule’s body and yields both recipe lines and any conditionals that appear within the rule.

§Example
use makefile_lossless::{Rule, RuleItem};

let rule_text = r#"test:
	echo "before"
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	./run-tests
endif
	echo "after"
"#;
let rule: Rule = rule_text.parse().unwrap();

let items: Vec<_> = rule.items().collect();
assert_eq!(items.len(), 3); // recipe, conditional, recipe

match &items[0] {
    RuleItem::Recipe(r) => assert_eq!(r, "echo \"before\""),
    _ => panic!("Expected recipe"),
}

match &items[1] {
    RuleItem::Conditional(_) => {},
    _ => panic!("Expected conditional"),
}

match &items[2] {
    RuleItem::Recipe(r) => assert_eq!(r, "echo \"after\""),
    _ => panic!("Expected recipe"),
}
Source

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"]);
Source

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"]);
Source

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"]);
Source

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"]);
Source

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);
Source

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);
Source

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());
Source

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"]);
Source

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"]);
Source

pub fn rename_target( &mut self, old_name: &str, new_name: &str, ) -> Result<bool, Error>

Rename a target in this rule

Returns Ok(true) if the target was found and renamed, Ok(false) if the target was not found.

§Example
use makefile_lossless::Rule;
let mut rule: Rule = "old_target: dependency\n\tcommand".parse().unwrap();
rule.rename_target("old_target", "new_target").unwrap();
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["new_target"]);
Source

pub fn add_target(&mut self, target: &str) -> Result<(), Error>

Add a target to this rule

§Example
use makefile_lossless::Rule;
let mut rule: Rule = "target1: dependency\n\tcommand".parse().unwrap();
rule.add_target("target2").unwrap();
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["target1", "target2"]);
Source

pub fn set_targets(&mut self, targets: Vec<&str>) -> Result<(), Error>

Set the targets for this rule, replacing any existing ones

Returns an error if the targets list is empty (rules must have at least one target).

§Example
use makefile_lossless::Rule;
let mut rule: Rule = "old_target: dependency\n\tcommand".parse().unwrap();
rule.set_targets(vec!["new_target1", "new_target2"]).unwrap();
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["new_target1", "new_target2"]);
Source

pub fn has_target(&self, target: &str) -> bool

Check if this rule has a specific target

§Example
use makefile_lossless::Rule;
let rule: Rule = "target1 target2: dependency\n\tcommand".parse().unwrap();
assert!(rule.has_target("target1"));
assert!(rule.has_target("target2"));
assert!(!rule.has_target("target3"));
Source

pub fn remove_target(&mut self, target_name: &str) -> Result<bool, Error>

Remove a target from this rule

Returns Ok(true) if the target was found and removed, Ok(false) if the target was not found. Returns an error if attempting to remove the last target (rules must have at least one target).

§Example
use makefile_lossless::Rule;
let mut rule: Rule = "target1 target2: dependency\n\tcommand".parse().unwrap();
rule.remove_target("target1").unwrap();
assert_eq!(rule.targets().collect::<Vec<_>>(), vec!["target2"]);
Source

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. When removing the last rule in a makefile, this will also trim any trailing blank lines from the previous rule to avoid leaving extra whitespace at the end of the file.

Source§

impl Rule

Source

pub fn line(&self) -> usize

Get the line number (0-indexed) where this node starts.

Source

pub fn column(&self) -> usize

Get the column number (0-indexed, in bytes) where this node starts.

Source

pub fn line_col(&self) -> (usize, usize)

Get both line and column (0-indexed) where this node starts. Returns (line, column) where column is measured in bytes from the start of the line.

Trait Implementations§

Source§

impl AstNode for Rule

Source§

type Language = Lang

Source§

fn can_cast(kind: SyntaxKind) -> bool

Source§

fn cast(syntax: SyntaxNode<Lang>) -> Option<Self>

Source§

fn syntax(&self) -> &SyntaxNode<Lang>

Source§

fn clone_for_update(&self) -> Self
where Self: Sized,

Source§

fn clone_subtree(&self) -> Self
where Self: Sized,

Source§

impl Clone for Rule

Source§

fn clone(&self) -> Rule

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Display for Rule

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl FromStr for Rule

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Rule

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Rule

Source§

fn eq(&self, other: &Rule) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Rule

Source§

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§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.