Git Product home page Git Product logo

libmw's Introduction

Library for constructing a pipeline of middleware functions that have the option to call the next in the chain if they so desire before returning back to the previous function call.

This allows for the programmer to perform work on the given context both before and after invoking the next function in the chain.

Example

use libmw::prelude::*;

/// Sample free standing handler function
fn standalone_func(ctx: &mut dyn PipelineContext, next: Pipeline) -> Result<(), PipelineError> {
  // do work before calling the next handler in the pipe
  println!("before in handler func");
  next.invoke(ctx)?;

  // do work after the next handler in the pipe returns
  println!("after in handler func");
  
  Ok(())
}

/// Use free standing functions to add additional trait bounds your context implements
/// for generic handlers that can be shared without needing knowledge of the implemented `PipelineContext`
fn standalone_func_enhance<CtxType>(ctx: &mut dyn PipelineContext, next: Pipeline) -> Result<(), PipelineError> 
where:
  CtxType: PipelineContext + Repeatable + 'static {
    if let Some(context) = ctx.as_any_mut().downcast_mut::<CtxType>() {
      while context.repeat() {
      next.invoke(context)?;

      if context.delay() != 0 {
        std::thread::sleep(std::time::Duration::from_millis(context.delay() as u64));
      }
    }
  }
  
  Ok(())
}

#[derive(PipelineContext)]
struct Context {
  take_branch: bool
}

fn main() {
  let mut builder = PipelineBuilder::new();

  // add freestanding function
  builder.with(standalone_func);

  // branch taken depending on how context was created
  builder.when(
    |ctx| match ctx.as_any().downcast_ref::<Context>() {
      Some(c) => c.take_branch,
      None => false,
    },
    |builder| {
      builder.with(|ctx, next| {
        println!("branch handler 1 before");
        next.invoke(ctx)?;
        println!("branch handler 1 after");

        Ok(())
      });

      builder.with(|ctx, next| {
        println!("branch handler 2 before");
        next.invoke(ctx)?;
        println!("branch handler 2 after");

        Ok(())
      });
    },
  );

  // with closure
  builder.with(|ctx, next| {
    // do work before calling the next handler in the pipe
  	println!("before in closure");
  	next.invoke(ctx)?;

    // do work after the next handler in the pipe returns
  	println!("after in closure");

  	Ok(())
  });
  
  // assemble the pipeline
  let pipeline = builder.assemble();

  // create context and call pipeline
  let mut context = Context { take_branch: true };
  let result = pipeline.invoke(&mut context);
  match result {
    Ok(_) -> {
      // deal with your mutated context
    }
    Err(_) => {
      // handle your pipeline errors
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.