Skip to main content

Program

Struct Program 

Source
pub struct Program {
    pub schema_version: u32,
    pub blocks: Vec<Block>,
    pub main: Block,
}
Expand description

A lowered pattern: comparison blocks plus the observation-tier block.

Fields§

§schema_version: u32

Serialized schema version; see SCHEMA_VERSION.

§blocks: Vec<Block>

The comparison blocks, in lowering order.

§main: Block

The observation-tier block.

Implementations§

Source§

impl Program

Source

pub fn block(&self, id: BlockId) -> Option<&Block>

The comparison block with the given id, if present.

Source§

impl Program

Source

pub fn to_listing(&self) -> String

Render this program as a human-readable listing.

Comparison-block results are named t0, t1, … across all blocks in order; main-block results are named o0, o1, … . These names are display ordinals, not InstrId values.

Source§

impl Program

Source

pub fn span_of(&self, block: BlockId, instr: InstrId) -> Option<Span>

The source extent of an instruction: its own span when it has one, otherwise the union of its operands’ extents.

The boolean combinators carry no span of their own — their extent is exactly the span of the text their operands cover — so this computes it on demand rather than storing redundant state that structural edits would have to keep consistent.

Returns None when neither the instruction nor anything it references has a span, and when block or instr names nothing in this program.

Observe normally answers from its own span, which is the extent of the whole [...]; only when that is missing does this descend into the comparison block it names.

This is safe to call on a program that has not been validated. Descent is iterative, so depth costs heap rather than stack, and it visits each instruction at most once, so it terminates on cyclic or shared input and does work linear in the size of the program.

§Example
use stix_pattern::{ir, parse};

let src = "[file:size > 1] AND [file:name = 'a']";
let program = ir::lower(&parse(src).unwrap());
// The `and` in `main` has no span of its own.
let and = program.main.instructions[2].id;
let span = program.span_of(program.main.id, and).unwrap();
assert_eq!(&src[span.start..span.end], src);
Source§

impl Program

Source

pub fn validate(&self) -> Result<(), IrError>

Check that this program is well-formed.

Lowered programs always pass. Run this on any program that was built by hand or deserialized, since deserialization does not check invariants.

Unreferenced (“dead”) instructions are allowed — only dangling and forward references are errors. A value may have at most one consumer, however, and a comparison block at most one Observe: see IrError::MultipleUses and IrError::BlockObservedTwice.

Nesting depth is not limited: validation, render and Program::span_of are all iterative, so a deeply nested program cannot overflow the stack.

Trait Implementations§

Source§

impl Clone for Program

Source§

fn clone(&self) -> Program

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Program

Source§

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

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

impl<'de> Deserialize<'de> for Program

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Program

Source§

fn eq(&self, other: &Program) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Program

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Program

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.