Trait Parse

Source
pub trait Parse:
    Clone
    + Send
    + Sync {
    // Required method
    fn new_parser() -> impl SendCreateParserState<Output = Self>;
}
Expand description

Data that can be parsed incrementally.

You can derive this trait for unit values, unit enums or structs or implement it manually for custom types.

§Example

use kalosm_sample::*;

// You can derive parse for structs with named fields that implement Parse
#[derive(Parse, Clone)]
struct Person {
    name: String,
    favorite_color: Color,
}

// You can derive parse for enums with only unit variants
#[derive(Parse, Clone)]
enum Color {
    Red,
    Blue,
    Green,
    Yellow,
    Orange,
    Purple,
    Pink,
    Black,
}

// Or you can implement parse manually for custom types
#[derive(Clone)]
struct MyStruct(i64, String);

impl Parse for MyStruct {
    // The only method on parse is new_parser, which returns a parser that outputs the current type
    fn new_parser() -> impl kalosm_sample::SendCreateParserState<Output = Self> {
        let number_parser = i64::new_parser();
        let string_parser = StringParser::new(0..=usize::MAX);
        kalosm_sample::LiteralParser::new("MyStruct(")
            .ignore_output_then(number_parser)
            .then_literal(", ")
            .then(string_parser)
            .then_literal(")")
            .map_output(|(a, b)| Self(a, b))
    }
}

Required Methods§

Source

fn new_parser() -> impl SendCreateParserState<Output = Self>

Create a new parser that parses the current type and can be sent between threads.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Parse for i8

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for i16

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for i32

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for i64

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for u8

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for u16

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for u32

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for u64

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl Parse for String

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl<T: Parse + Clone + Send + Sync> Parse for Vec<T>

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl<T: Parse> Parse for Option<T>

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl<T: Parse> Parse for Box<T>

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Source§

impl<const N: usize, T: Parse + Clone + Send + Sync> Parse for [T; N]

Source§

fn new_parser() -> impl SendCreateParserState<Output = Self>

Implementors§

Source§

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Parse for Sentence<MIN_LENGTH, MAX_LENGTH>

Source§

impl<const MIN_LENGTH: usize, const MAX_LENGTH: usize> Parse for Word<MIN_LENGTH, MAX_LENGTH>