s_text_input_f_parser/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use chumsky::prelude::*;
use s_text_input_f::{Block, Blocks};

#[derive(Debug)]
pub struct CorrectBlocks {
    pub blocks: Blocks,
    pub answer: Vec<Vec<String>>,
}
impl FromIterator<CorrectBlock> for CorrectBlocks {
    fn from_iter<T: IntoIterator<Item = CorrectBlock>>(iter: T) -> Self {
        let (blocks, answer) = iter.into_iter().map(|x| (x.block, x.answer)).unzip();
        Self { blocks, answer }
    }
}

#[derive(Debug)]
pub struct CorrectBlock {
    pub block: Block,
    pub answer: Vec<String>,
}
impl From<paragraph::CorrectParagraph> for CorrectBlock {
    fn from(value: paragraph::CorrectParagraph) -> Self {
        Self {
            block: Block::Paragraph(value.input),
            answer: value.answer,
        }
    }
}
impl From<one_of::CorrectOneOf> for CorrectBlock {
    fn from(value: one_of::CorrectOneOf) -> Self {
        Self {
            block: Block::OneOf(value.variants),
            answer: vec![value.correct.to_string()],
        }
    }
}
impl From<any_of::CorrectAnyOf> for CorrectBlock {
    fn from(value: any_of::CorrectAnyOf) -> Self {
        Self {
            block: Block::AnyOf(value.variants),
            answer: value.correct.into_iter().map(|x| x.to_string()).collect(),
        }
    }
}

pub fn parse_paragraph(input: &str) -> Result<paragraph::CorrectParagraph, Vec<Simple<char>>> {
    paragraph::paragraph_parser()
        .then_ignore(end())
        .parse(input)
}
pub fn parse_one_of(input: &str) -> Result<one_of::CorrectOneOf, Vec<Simple<char>>> {
    one_of::one_of_parser().then_ignore(end()).parse(input)
}
pub fn parse_any_of(input: &str) -> Result<any_of::CorrectAnyOf, Vec<Simple<char>>> {
    any_of::any_of_parser().then_ignore(end()).parse(input)
}

pub mod any_of;
pub mod one_of;
pub mod paragraph;

pub fn parse_block(input: &str) -> Result<CorrectBlock, Vec<Simple<char>>> {
    block_parser().then_ignore(end()).parse(input)
}

fn block_parser() -> impl Parser<char, CorrectBlock, Error = Simple<char>> {
    choice((
        any_of::any_of_parser().map(CorrectBlock::from),
        one_of::one_of_parser().map(CorrectBlock::from),
        paragraph::paragraph_parser().map(CorrectBlock::from),
        todo(),
    ))
}

pub fn parse_blocks(input: &str) -> Result<CorrectBlocks, Vec<Simple<char>>> {
    blocks_parser().then_ignore(end()).parse(input)
}
fn blocks_parser() -> impl Parser<char, CorrectBlocks, Error = Simple<char>> {
    block_parser()
        .separated_by(just('\n').repeated().at_least(1))
        .at_least(1)
        .collect()
}