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 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 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.

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 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 · Source§

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 · Source§

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§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. 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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.