Skip to main content

MakefileItem

Enum MakefileItem 

Source
pub enum MakefileItem {
    Rule(Rule),
    Variable(VariableDefinition),
    Include(Include),
    Conditional(Conditional),
}
Expand description

Represents different types of items that can appear in a Makefile

Variants§

§

Rule(Rule)

A rule definition (e.g., “target: prerequisites”)

§

Variable(VariableDefinition)

A variable definition (e.g., “VAR = value”)

§

Include(Include)

An include directive (e.g., “include foo.mk”)

§

Conditional(Conditional)

A conditional block (e.g., “ifdef DEBUG … endif”)

Implementations§

Source§

impl MakefileItem

Source

pub fn replace(&mut self, new_item: MakefileItem) -> Result<(), Error>

Replace this MakefileItem with another MakefileItem

This preserves the position of the original item but replaces its content with the new item. Preceding comments are preserved.

§Example
use makefile_lossless::{Makefile, MakefileItem};
let mut makefile: Makefile = "VAR1 = old\nrule:\n\tcommand\n".parse().unwrap();
let temp: Makefile = "VAR2 = new\n".parse().unwrap();
let new_var = temp.variable_definitions().next().unwrap();
let mut first_item = makefile.items().next().unwrap();
first_item.replace(MakefileItem::Variable(new_var)).unwrap();
assert!(makefile.to_string().contains("VAR2 = new"));
assert!(!makefile.to_string().contains("VAR1"));
Source

pub fn add_comment(&mut self, comment_text: &str) -> Result<(), Error>

Add a comment before this MakefileItem

The comment text should not include the leading ‘#’ character. Multiple comment lines can be added by calling this method multiple times.

§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "VAR = value\n".parse().unwrap();
let mut item = makefile.items().next().unwrap();
item.add_comment("This is a variable").unwrap();
assert!(makefile.to_string().contains("# This is a variable"));
Source

pub fn preceding_comments(&self) -> impl Iterator<Item = String>

Get all preceding comments for this MakefileItem

Returns an iterator of comment strings (without the leading ‘#’ and whitespace).

§Example
use makefile_lossless::Makefile;
let makefile: Makefile = "# Comment 1\n# Comment 2\nVAR = value\n".parse().unwrap();
let item = makefile.items().next().unwrap();
let comments: Vec<_> = item.preceding_comments().collect();
assert_eq!(comments.len(), 2);
assert_eq!(comments[0], "Comment 1");
assert_eq!(comments[1], "Comment 2");
Source

pub fn remove_comments(&mut self) -> Result<usize, Error>

Remove all preceding comments for this MakefileItem

Returns the number of comments removed.

§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "# Comment 1\n# Comment 2\nVAR = value\n".parse().unwrap();
let mut item = makefile.items().next().unwrap();
let count = item.remove_comments().unwrap();
assert_eq!(count, 2);
assert!(!makefile.to_string().contains("# Comment"));
Source

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

Modify the first preceding comment for this MakefileItem

Returns true if a comment was found and modified, false if no comment exists. The comment text should not include the leading ‘#’ character.

§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = "# Old comment\nVAR = value\n".parse().unwrap();
let mut item = makefile.items().next().unwrap();
let modified = item.modify_comment("New comment").unwrap();
assert!(modified);
assert!(makefile.to_string().contains("# New comment"));
assert!(!makefile.to_string().contains("# Old comment"));
Source

pub fn insert_before(&mut self, new_item: MakefileItem) -> Result<(), Error>

Insert a new MakefileItem before this item

This inserts the new item immediately before the current item in the makefile. The new item is inserted at the same level as the current item.

§Example
use makefile_lossless::{Makefile, MakefileItem};
let mut makefile: Makefile = "VAR1 = first\nVAR2 = second\n".parse().unwrap();
let temp: Makefile = "VAR_NEW = inserted\n".parse().unwrap();
let new_var = temp.variable_definitions().next().unwrap();
let mut second_item = makefile.items().nth(1).unwrap();
second_item.insert_before(MakefileItem::Variable(new_var)).unwrap();
let result = makefile.to_string();
assert!(result.contains("VAR1 = first\nVAR_NEW = inserted\nVAR2 = second"));
Source

pub fn insert_after(&mut self, new_item: MakefileItem) -> Result<(), Error>

Insert a new MakefileItem after this item

This inserts the new item immediately after the current item in the makefile. The new item is inserted at the same level as the current item.

§Example
use makefile_lossless::{Makefile, MakefileItem};
let mut makefile: Makefile = "VAR1 = first\nVAR2 = second\n".parse().unwrap();
let temp: Makefile = "VAR_NEW = inserted\n".parse().unwrap();
let new_var = temp.variable_definitions().next().unwrap();
let mut first_item = makefile.items().next().unwrap();
first_item.insert_after(MakefileItem::Variable(new_var)).unwrap();
let result = makefile.to_string();
assert!(result.contains("VAR1 = first\nVAR_NEW = inserted\nVAR2 = second"));

Trait Implementations§

Source§

impl Clone for MakefileItem

Source§

fn clone(&self) -> MakefileItem

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

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<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

Uses borrowed data to replace owned data, usually by cloning. 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.