referrerpolicy=no-referrer-when-downgrade

Trait staging_xcm_builder::MatchXcm

source ·
pub trait MatchXcm {
    type Inst;
    type Loc;
    type Error;

    // Required methods
    fn assert_remaining_insts(self, n: usize) -> Result<Self, Self::Error>
       where Self: Sized;
    fn match_next_inst<F>(self, f: F) -> Result<Self, Self::Error>
       where Self: Sized,
             F: FnMut(&mut Self::Inst) -> Result<(), Self::Error>;
    fn match_next_inst_while<C, F>(
        self,
        cond: C,
        f: F,
    ) -> Result<Self, Self::Error>
       where Self: Sized,
             C: Fn(&Self::Inst) -> bool,
             F: FnMut(&mut Self::Inst) -> Result<ControlFlow<()>, Self::Error>;

    // Provided method
    fn skip_inst_while<C>(self, cond: C) -> Result<Self, Self::Error>
       where Self: Sized,
             C: Fn(&Self::Inst) -> bool { ... }
}
Expand description

API that allows to pattern-match against anything that is contained within an XCM.

The intended usage of the matcher API is to enable the ability to chain successive methods of this trait together, along with the ? operator for the purpose of facilitating the writing, maintenance and auditability of XCM barriers.

Example:

use frame_support::traits::ProcessMessageError;
use xcm::latest::Instruction;
use staging_xcm_builder::{CreateMatcher, MatchXcm};

let mut msg = [Instruction::<()>::ClearOrigin];
let res = msg
	.matcher()
	.assert_remaining_insts(1)?
	.match_next_inst(|inst| match inst {
		Instruction::<()>::ClearOrigin => Ok(()),
		_ => Err(ProcessMessageError::BadFormat),
	});
assert!(res.is_ok());

Ok::<(), ProcessMessageError>(())

Required Associated Types§

source

type Inst

The concrete instruction type. Necessary to specify as it changes between XCM versions.

source

type Loc

The Location type. Necessary to specify as it changes between XCM versions.

source

type Error

The error type to throw when errors happen during matching.

Required Methods§

source

fn assert_remaining_insts(self, n: usize) -> Result<Self, Self::Error>
where Self: Sized,

Returns success if the number of instructions that still have not been iterated over equals n, otherwise returns an error.

source

fn match_next_inst<F>(self, f: F) -> Result<Self, Self::Error>
where Self: Sized, F: FnMut(&mut Self::Inst) -> Result<(), Self::Error>,

Accepts a closure f that contains an argument signifying the next instruction to be iterated over. The closure can then be used to check whether the instruction matches a given condition, and can also be used to mutate the fields of an instruction.

The closure f returns success when the instruction passes the condition, otherwise it returns an error, which will ultimately be returned by this function.

source

fn match_next_inst_while<C, F>(self, cond: C, f: F) -> Result<Self, Self::Error>
where Self: Sized, C: Fn(&Self::Inst) -> bool, F: FnMut(&mut Self::Inst) -> Result<ControlFlow<()>, Self::Error>,

Attempts to continuously iterate through the instructions while applying f to each of them, until either the last instruction or cond returns false.

If f returns an error, then iteration halts and the function returns that error. Otherwise, f returns a ControlFlow which signifies whether the iteration breaks or continues.

Provided Methods§

source

fn skip_inst_while<C>(self, cond: C) -> Result<Self, Self::Error>
where Self: Sized, C: Fn(&Self::Inst) -> bool,

Iterate instructions forward until cond returns false. When there are no more instructions to be read, an error is returned.

Implementors§

source§

impl<'a, Call> MatchXcm for Matcher<'a, Call>

§

type Error = ProcessMessageError

§

type Inst = Instruction<Call>

§

type Loc = Location