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
impl MakefileItem
Sourcepub fn replace(&mut self, new_item: MakefileItem) -> Result<(), Error>
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"));Sourcepub fn add_comment(&mut self, comment_text: &str) -> Result<(), Error>
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"));Sourcepub fn preceding_comments(&self) -> impl Iterator<Item = String>
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");Sourcepub fn remove_comments(&mut self) -> Result<usize, Error>
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"));Sourcepub fn modify_comment(&mut self, new_comment_text: &str) -> Result<bool, Error>
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"));Sourcepub fn insert_before(&mut self, new_item: MakefileItem) -> Result<(), Error>
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"));Sourcepub fn insert_after(&mut self, new_item: MakefileItem) -> Result<(), Error>
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
impl Clone for MakefileItem
Source§fn clone(&self) -> MakefileItem
fn clone(&self) -> MakefileItem
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more