pub struct Conditional(/* private fields */);Expand description
An AST node for $ast
Implementations§
Source§impl Conditional
impl Conditional
Sourcepub fn parent(&self) -> Option<MakefileItem>
pub fn parent(&self) -> Option<MakefileItem>
Get the parent item of this conditional, if any
Returns Some(MakefileItem) if this conditional has a parent that is a MakefileItem
(e.g., another Conditional for nested conditionals), or None if the parent is the root Makefile node.
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = r#"ifdef OUTER
ifdef INNER
VAR = value
endif
endif
"#.parse().unwrap();
let outer = makefile.conditionals().next().unwrap();
let inner = outer.if_items().find_map(|item| {
if let makefile_lossless::MakefileItem::Conditional(c) = item {
Some(c)
} else {
None
}
}).unwrap();
// Inner conditional's parent is the outer conditional
assert!(inner.parent().is_some());Sourcepub fn conditional_type(&self) -> Option<String>
pub fn conditional_type(&self) -> Option<String>
Get the type of conditional (ifdef, ifndef, ifeq, ifneq)
Sourcepub fn else_body(&self) -> Option<String>
pub fn else_body(&self) -> Option<String>
Get the body content of the else branch (if it exists)
Sourcepub fn unwrap(&mut self) -> Result<(), Error>
pub fn unwrap(&mut self) -> Result<(), Error>
Remove the conditional directives (ifdef/endif) but keep the body content
This “unwraps” the conditional, keeping only the if branch content. Returns an error if the conditional has an else clause.
§Example
use makefile_lossless::Makefile;
let mut makefile: Makefile = r#"ifdef DEBUG
VAR = debug
endif
"#.parse().unwrap();
let mut cond = makefile.conditionals().next().unwrap();
cond.unwrap().unwrap();
// Now makefile contains just "VAR = debug\n"
assert!(makefile.to_string().contains("VAR = debug"));
assert!(!makefile.to_string().contains("ifdef"));Sourcepub fn if_items(&self) -> impl Iterator<Item = MakefileItem> + '_
pub fn if_items(&self) -> impl Iterator<Item = MakefileItem> + '_
Get all items (rules, variables, includes, nested conditionals) in the if branch
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = r#"ifdef DEBUG
VAR = debug
rule:
command
endif
"#.parse().unwrap();
let cond = makefile.conditionals().next().unwrap();
let items: Vec<_> = cond.if_items().collect();
assert_eq!(items.len(), 2); // One variable, one ruleSourcepub fn else_items(&self) -> impl Iterator<Item = MakefileItem> + '_
pub fn else_items(&self) -> impl Iterator<Item = MakefileItem> + '_
Get all items (rules, variables, includes, nested conditionals) in the else branch
§Example
use makefile_lossless::Makefile;
let makefile: Makefile = r#"ifdef DEBUG
VAR = debug
else
VAR = release
endif
"#.parse().unwrap();
let cond = makefile.conditionals().next().unwrap();
let items: Vec<_> = cond.else_items().collect();
assert_eq!(items.len(), 1); // One variable in else branchSourcepub fn add_if_item(&mut self, item: MakefileItem)
pub fn add_if_item(&mut self, item: MakefileItem)
Add an item to the if branch of the conditional
§Example
use makefile_lossless::{Makefile, MakefileItem};
let mut makefile: Makefile = "ifdef DEBUG\nendif\n".parse().unwrap();
let mut cond = makefile.conditionals().next().unwrap();
let temp: Makefile = "CFLAGS = -g\n".parse().unwrap();
let var = temp.variable_definitions().next().unwrap();
cond.add_if_item(MakefileItem::Variable(var));
assert!(makefile.to_string().contains("CFLAGS = -g"));Sourcepub fn add_else_item(&mut self, item: MakefileItem)
pub fn add_else_item(&mut self, item: MakefileItem)
Add an item to the else branch of the conditional
If the conditional doesn’t have an else branch, this will create one.
§Example
use makefile_lossless::{Makefile, MakefileItem};
let mut makefile: Makefile = "ifdef DEBUG\nVAR=1\nendif\n".parse().unwrap();
let mut cond = makefile.conditionals().next().unwrap();
let temp: Makefile = "CFLAGS = -O2\n".parse().unwrap();
let var = temp.variable_definitions().next().unwrap();
cond.add_else_item(MakefileItem::Variable(var));
assert!(makefile.to_string().contains("else"));
assert!(makefile.to_string().contains("CFLAGS = -O2"));Trait Implementations§
Source§impl AstNode for Conditional
impl AstNode for Conditional
Source§impl Clone for Conditional
impl Clone for Conditional
Source§fn clone(&self) -> Conditional
fn clone(&self) -> Conditional
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more