use nom::{
IResult,
character::streaming::*,
bytes::streaming::*,
};
use nom::branch::*;
use nom::combinator::*;
use nom::sequence::*;
use crate::Busy;
use super::{
Response,
SDCard,
Feedback,
StartSDWrite,
u32_str,
};
pub fn echo<'r>(input: &'r str) -> IResult<&'r str, Response> {
preceded(
pair(
tag_no_case("echo:"),
space0,
),
alt((
busy,
m21_sd_card_ok,
m23_m28_fresh_file,
normal_echo_content,
)),
)(input)
}
fn normal_echo_content<'r>(input: &'r str) -> IResult<&'r str, Response> {
map(
not_line_ending,
|s: &str| Response::Echo(s.to_string()),
)(input)
}
fn m21_sd_card_ok<'r>(input: &'r str) -> IResult<&'r str, Response> {
map(
preceded(
tuple((
tag("SD card ok"),
space0,
line_ending,
take_until("\nsize:"),
space0,
line_ending,
)),
u32_str(),
),
|size: u32| {
let sd_card = SDCard {
enabled: true,
size: Some(size),
};
Response::Ok(Some(Feedback::SDCard(sd_card)))
},
)(input)
}
fn m23_m28_fresh_file<'r>(input: &'r str) -> IResult<&'r str, Response> {
preceded(
tuple((
tag_no_case("Now fresh file:"),
not_line_ending,
line_ending,
)),
alt((
map(
preceded(
pair(
tag_no_case("Writing to file:"),
space0,
),
not_line_ending,
),
|filename: &str| {
let start_streaming = Feedback::StartSDWrite(StartSDWrite {
filename: filename.to_string(),
});
Response::Ok(Some(start_streaming))
},
),
map(
recognize(tuple((
tag("File opened:"),
not_line_ending,
line_ending,
tag("File selected"),
))),
|s: &str| Response::Debug(s.to_string())
),
map(
preceded(
tag_no_case("open failed, File:"),
not_line_ending,
),
|_| {
Response::Error("\
Failed to open sd card file. \
Check that SD card is inserted and enabled in firmware.\
".to_string())
},
),
)),
)(input)
}
fn busy<'r>(input: &'r str) -> IResult<&'r str, Response> {
preceded(
pair(
tag_no_case("busy:"),
space0,
),
alt((
value(
Response::Feedback(Feedback::Busy(Busy::Processing)),
tag_no_case("processing"),
),
value(
Response::Feedback(Feedback::Busy(Busy::PausedForUser)),
tag_no_case("paused for user"),
),
map(
not_line_ending,
|s: &str| Response::Feedback(Feedback::Busy(Busy::Other(s.to_string()))),
),
))
)(input)
}