Repl

Struct Repl 

Source
pub struct Repl<'a, T> {
    pub data: T,
    /* private fields */
}
Expand description

Main Repl Struct that contains all logic for the crate

Fields§

§data: T

struct to store data, used as an argument in all functions

Implementations§

Source§

impl<'a, T> Repl<'a, T>

Source

pub fn new(data: T) -> Self

builds a new Repl from the given data

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::new(());
Source

pub fn run(&mut self) -> Result<()>

runs the repl functions which have command "" will be called if none of the other commands are not called.

§Examples
use repl_framework::Repl;
Repl::default().with_function("", |_: &mut (), b| println!("{:?}", b)).run();
§Errors
  • reading from stdin fails
  • flushing stdout fails
Examples found in repository?
examples/reverse.rs (line 3)
2fn main() -> std::io::Result<()> {
3    Repl::default().with_function("", reverse).run()
4}
More examples
Hide additional examples
examples/cat.rs (line 7)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_prompt("read:> ")
5        .with_function("", cat)
6        .with_function("help", help)
7        .run()
8}
examples/custom_parser.rs (line 7)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_parser(|f| f.split('.').map(|f| f.to_owned()).collect())
5        .with_prompt("print:> ")
6        .with_function("", |_: &mut (), args| println!("{}", args.join("/")))
7        .run()
8}
examples/keystore.rs (line 10)
3fn main() -> std::io::Result<()> {
4    Repl::default()
5        // makes command with name "get" which calls Store::get when called
6        .with_function("get", Store::get)
7        .with_function("set", Store::set)
8        // commands with the name "" get called whenever the user inputs a commmand which is not declared
9        .with_function("", Store::unknown_command)
10        .run()
11}
Source

pub fn new_with_depreciated_parser(data: T) -> Self

builds a new Repl from the given data, use for compatibility reasons only, the new parser is better in pretty much every way possible

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::new_with_depreciated_parser(());
Source

pub fn get_input(&self) -> Result<Vec<String>>

same as take_arg, but returns the argument instead of storing it in self.argument

§Errors

this function returns an error if

  • reading from stdin fails
  • flushing stdout fails
Source

pub fn with_function(self, name: &'a str, func: fn(&mut T, Vec<String>)) -> Self

builder style method for adding a function. this function is chainable, use add_function if you don’t want it to be chainable.

§Example
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_function("hello", hello);

fn hello(_: &mut (), _: Vec<String>) {
    println!("hello");
}
Examples found in repository?
examples/reverse.rs (line 3)
2fn main() -> std::io::Result<()> {
3    Repl::default().with_function("", reverse).run()
4}
More examples
Hide additional examples
examples/cat.rs (line 5)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_prompt("read:> ")
5        .with_function("", cat)
6        .with_function("help", help)
7        .run()
8}
examples/custom_parser.rs (line 6)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_parser(|f| f.split('.').map(|f| f.to_owned()).collect())
5        .with_prompt("print:> ")
6        .with_function("", |_: &mut (), args| println!("{}", args.join("/")))
7        .run()
8}
examples/keystore.rs (line 6)
3fn main() -> std::io::Result<()> {
4    Repl::default()
5        // makes command with name "get" which calls Store::get when called
6        .with_function("get", Store::get)
7        .with_function("set", Store::set)
8        // commands with the name "" get called whenever the user inputs a commmand which is not declared
9        .with_function("", Store::unknown_command)
10        .run()
11}
Source

pub fn with_parser(self, parser: fn(String) -> Vec<String>) -> Self

builder style method for changing the parser. this function is chainable, use set_parser if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_parser(|raw| vec![raw]);
Examples found in repository?
examples/custom_parser.rs (line 4)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_parser(|f| f.split('.').map(|f| f.to_owned()).collect())
5        .with_prompt("print:> ")
6        .with_function("", |_: &mut (), args| println!("{}", args.join("/")))
7        .run()
8}
Source

pub fn with_data(self, data: T) -> Self

builder style method for changing the data. this function is chainable, use set_data if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_data(());
Source

pub fn with_prompt(self, prompt: &'a str) -> Self

builder style method for changing the prompt. this function is chainable, use set_prompt if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_prompt("+>");
// the repl will display +> after every message now, instead of the default ">>>"
Examples found in repository?
examples/cat.rs (line 4)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_prompt("read:> ")
5        .with_function("", cat)
6        .with_function("help", help)
7        .run()
8}
More examples
Hide additional examples
examples/custom_parser.rs (line 5)
2fn main() -> std::io::Result<()> {
3    Repl::default()
4        .with_parser(|f| f.split('.').map(|f| f.to_owned()).collect())
5        .with_prompt("print:> ")
6        .with_function("", |_: &mut (), args| println!("{}", args.join("/")))
7        .run()
8}
Source

pub fn with_exit_command(self, exit: &'a str) -> Self

builder style method for changing the exit command. this function is chainable, use set_exit_command if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_exit_command("close");
// the repl will close if you give "close" as input now
Source

pub fn with_exit_message(self, exit_message: &'a str) -> Self

builder style method for changing the exit message. this function is chainable, use set_exit_message if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_exit_message("repl closed!");
// the repl will display "repl closed!" on termination
Source

pub fn with_empty_argument_message( self, empty_argument_message: &'a str, ) -> Self

builder style method for changing the exit message. this function is chainable, use set_exit_message if you don’t want it to be chainable.

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default().with_empty_argument_message("empty arg :(");
// the repl will display "empty arg :(" on termination
Source

pub fn add_function(&mut self, name: &'a str, func: fn(&mut T, Vec<String>))

adds function to Repl, not chainable, use with_function if you want chaining

§Examples
use repl_framework::Repl;
let mut repl: Repl<()> = Repl::default();
repl.add_function("hello", hello);
fn hello(_: &mut (), _: Vec<String>) {
    println!("Hello!")
}
Source

pub fn set_data(&mut self, data: T)

sets data to argument, NOT chainable, use with_data if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<i32> = Repl::default();
repl.set_data(100);
Source

pub fn set_prompt(&mut self, prompt: &'a str)

sets prompt to argument, NOT chainable, use with_prompt if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<()> = Repl::default();
repl.set_prompt(":>");
Source

pub fn set_exit_command(&mut self, exit: &'a str)

sets exit command to argument, NOT chainable, use with_exit_command if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<()> = Repl::default();
repl.set_exit_command("close!");
Source

pub fn set_exit_message(&mut self, exit_message: &'a str)

sets exit message to argument, NOT chainable, use with_exit_message if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<()> = Repl::default();
repl.set_exit_message("bye!");
Source

pub fn set_empty_argument_message(&mut self, empty_argument_message: &'a str)

sets exit message to argument, NOT chainable, use with_exit_message if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<()> = Repl::default();
repl.set_empty_argument_message("empty argument list!");
Source

pub fn set_parser(&mut self, parser: fn(String) -> Vec<String>)

sets parser function to argument, NOT chainable, use with_parser if you want chaining.

§Examples
use repl_framework::Repl;
let mut repl: Repl<i32> = Repl::default();
repl.set_parser(|raw| vec![raw]);

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for Repl<'a, T>

Source§

fn clone(&self) -> Repl<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for Repl<'a, T>

Source§

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

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

impl<T: Default> Default for Repl<'_, T>

Source§

fn default() -> Self

builds a new Repl from the given data

§Examples
use repl_framework::Repl;
let repl: Repl<()> = Repl::default();

Auto Trait Implementations§

§

impl<'a, T> Freeze for Repl<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for Repl<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Repl<'a, T>
where T: Send,

§

impl<'a, T> Sync for Repl<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Repl<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for Repl<'a, T>
where T: UnwindSafe,

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> 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.