Conditional

Struct Conditional 

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

An AST node for $ast

Implementations§

Source§

impl Conditional

Source

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

pub fn conditional_type(&self) -> Option<String>

Get the type of conditional (ifdef, ifndef, ifeq, ifneq)

Source

pub fn condition(&self) -> Option<String>

Get the condition expression

Source

pub fn has_else(&self) -> bool

Check if this conditional has an else clause

Source

pub fn if_body(&self) -> Option<String>

Get the body content of the if branch

Source

pub fn else_body(&self) -> Option<String>

Get the body content of the else branch (if it exists)

Source

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

Remove this conditional from the makefile

Source

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

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 rule
Source

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 branch
Source

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

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

impl Conditional

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 Conditional

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 Conditional

Source§

fn clone(&self) -> Conditional

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 Conditional

Source§

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

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

impl Hash for Conditional

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 Conditional

Source§

fn eq(&self, other: &Conditional) -> 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 Conditional

Source§

impl StructuralPartialEq for Conditional

Auto Trait Implementations§

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.