Module rad::processor [−][src]
Expand description
processor
“processor” module is about processing of given input.
Processor substitutes all macros only when the macros were already defined and returns untouched string back if not found any.
Processor can handle various types of inputs (string|stdin|file)
Detailed usage
use rad::RadError;
use rad::Processor;
use rad::MacroType;
use rad::AuthType;
use std::path::Path;
// Builder
let mut processor = Processor::new()
.purge(true) // Purge undefined macro
.greedy(true) // Makes all macro greedy
.silent(true) // Silents all warnings
.nopanic(true) // No panic in any circumstances
.strict(true) // Enable strict mode, panicks on any error
.custom_rules(Some(vec![Path::new("rule.r4f")]))? // Read from frozen rule files
.write_to_file(Some(Path::new("out.txt")))? // default is stdout
.error_to_file(Some(Path::new("err.txt")))? // default is stderr
.unix_new_line(true) // use unix new line for formatting
.discard(true) // discard all output
// Permission
.allow(Some(vec![AuthType::ENV])) // Grant permission of authtypes
.allow_with_warning(Some(vec![AuthType::CMD])) // Grant permission of authypes with warning enabled
// Debugging options
.debug(true) // Turn on debug mode
.log(true) // Use logging to terminal
.interactive(true) // Use interactive mode
// Create unreferenced instance
.build();
// Use Processor::empty() instead of Processor::new()
// if you don't want any default macros
// Print information about current processor permissions
// This is an warning and can be suppressed with silent option
processor.print_permission()?;
// Add basic rules(= register functions)
// test function is not included in this demo
processor.add_basic_rules(vec![("test", test as MacroType)]);
// You can add basic rule in form of closure too
processor.add_closure_rule(
"test", // Name of macro
2, // Count of arguments
Box::new(|args: Vec<String>| -> Option<String> { // Closure as an internal logic
Some(format!("First : {}\nSecond: {}", args[0], args[1]))
})
);
// Add custom rules(in order of "name, args, body")
processor.add_custom_rules(vec![("test","a_src a_link","$a_src() -> $a_link()")]);
// Process with inputs
// This prints to desginated write destinations
processor.from_string(r#"$define(test=Test)"#)?;
processor.from_stdin()?;
processor.from_file(Path::new("from.txt"))?;
processor.freeze_to_file(Path::new("out.r4f"))?; // Create frozen file
// Print out result
// This will print counts of warning and errors.
// It will also print diff between source and processed if diff option was
// given as builder pattern.
processor.print_result()?; Structs
Processor that parses(lexes) given input and print out to desginated output