2023-10-24 12:30:48 +11:00
|
|
|
use crate::nom_help;
|
2023-10-23 14:25:33 +11:00
|
|
|
use nom::branch::alt;
|
2023-10-24 12:30:48 +11:00
|
|
|
use nom::bytes::complete::{tag, take_until};
|
2023-10-23 14:25:33 +11:00
|
|
|
use nom::combinator::opt;
|
2023-10-24 12:30:48 +11:00
|
|
|
use nom::multi::many1;
|
2023-10-23 14:25:33 +11:00
|
|
|
use nom::IResult;
|
2023-10-23 16:17:50 +11:00
|
|
|
use std::collections::HashMap;
|
2023-10-24 12:30:48 +11:00
|
|
|
use tracing::{debug, trace};
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
pub fn parse(s: &str) -> HashMap<String, VehicleCommandEndpoint> {
|
2023-10-23 14:25:33 +11:00
|
|
|
// Seek all the way to: var commands = map[string]*Command{\n
|
|
|
|
// Afterwards has the first map entry.
|
2023-10-23 16:17:50 +11:00
|
|
|
let (s, _) = seek_to_map(s).unwrap();
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
let (_, entries) = many1(map_entry)(s).unwrap();
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
entries
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| (e.endpoint.clone(), e))
|
|
|
|
.collect()
|
|
|
|
}
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
pub fn seek_to_map(s: &str) -> IResult<&str, ()> {
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("seek to map", s);
|
2023-10-23 16:17:50 +11:00
|
|
|
let tag_str = "var commands = map[string]*Command{\n";
|
|
|
|
// There's gotta be a nom function to these two lines.
|
|
|
|
let (s, _) = take_until(tag_str)(s)?;
|
|
|
|
let (s, _) = tag(tag_str)(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("seek to map done", s);
|
2023-10-23 16:17:50 +11:00
|
|
|
Ok((s, ()))
|
2023-10-23 14:25:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-10-23 16:17:50 +11:00
|
|
|
pub struct VehicleCommandEndpoint {
|
|
|
|
pub endpoint: String,
|
|
|
|
pub help: String,
|
|
|
|
pub requires_auth: bool,
|
|
|
|
pub requires_fleet: bool,
|
2023-10-23 14:25:33 +11:00
|
|
|
}
|
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
fn map_entry(s: &str) -> IResult<&str, VehicleCommandEndpoint> {
|
2023-10-23 14:25:33 +11:00
|
|
|
// "unlock": &Command{
|
|
|
|
// help: "Unlock vehicle",
|
|
|
|
// requiresAuth: true,
|
|
|
|
// requiresFleetAPI: false,
|
|
|
|
// args: []Argument{
|
|
|
|
// Argument{name: "TEMP", help: "Desired temperature (e.g., 70f or 21c; defaults to Celsius)"},
|
|
|
|
// Argument{name: "ROLE", help: "One of: owner, driver"},
|
|
|
|
// },
|
|
|
|
// handler: func(ctx context.Context, acct *account.Account, car *vehicle.Vehicle, args map[string]string) error {
|
|
|
|
// return car.Unlock(ctx)
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("--- map entry ---", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
|
|
|
|
// endpoint
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("endpoint", s);
|
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
|
|
|
let (s, endpoint) = nom_help::quoted_string(s)?;
|
|
|
|
let (s, _) = nom_help::until_eol(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
|
|
|
|
// help
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("help", s);
|
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("help:")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
|
|
|
let (s, help) = nom_help::quoted_string(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag(",")(s)?;
|
|
|
|
|
|
|
|
// requiresAuth
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("requiresAuth", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, requires_auth) = bool_field_or_false(s, "requiresAuth:")?;
|
|
|
|
|
|
|
|
// requiresFleetAPI
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("requiresFleetAPI", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, requires_fleet) = bool_field_or_false(s, "requiresFleetAPI:")?;
|
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
// Required args
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("required args", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, required_args) = args(s, "args: []Argument{")?;
|
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
// Optional args
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("optional args", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, optional_args) = args(s, "optional: []Argument{")?;
|
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
// Ignore the handler, as there's not really much data we can take out of it.
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = take_until("},")(s)?;
|
|
|
|
let (s, _) = tag("},")(s)?;
|
|
|
|
|
|
|
|
// And the end of the record...
|
|
|
|
let (s, _) = take_until("},")(s)?;
|
|
|
|
let (s, _) = tag("},")(s)?;
|
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
let map_entry = VehicleCommandEndpoint {
|
|
|
|
endpoint: endpoint.to_string(),
|
|
|
|
help: help.to_string(),
|
|
|
|
requires_auth,
|
|
|
|
requires_fleet,
|
|
|
|
};
|
|
|
|
debug!(?map_entry);
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-23 16:17:50 +11:00
|
|
|
Ok((s, map_entry))
|
2023-10-23 14:25:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn str_to_bool(s: &str) -> IResult<&str, bool> {
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("bool", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, bool_str) = alt((tag("true"), tag("false")))(s)?;
|
|
|
|
let bool = match bool_str {
|
|
|
|
"true" => true,
|
|
|
|
"false" => false,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("bool afterwards", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
Ok((s, bool))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the field isn't there, assume false.
|
|
|
|
fn bool_field<'a>(field_tag: &str) -> impl Fn(&'a str) -> IResult<&'a str, bool> + '_ {
|
|
|
|
return move |s: &str| -> IResult<&'a str, bool> {
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag(field_tag)(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, value) = str_to_bool(s)?;
|
|
|
|
let (s, _) = tag(",")(s)?;
|
|
|
|
|
|
|
|
Ok((s, value))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bool_field_or_false<'a>(s: &'a str, field_tag: &str) -> IResult<&'a str, bool> {
|
|
|
|
let (s, value) = opt(bool_field(field_tag))(s)?;
|
|
|
|
return Ok((s, value.unwrap_or(false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Arg {
|
|
|
|
name: String,
|
|
|
|
help: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn args<'a>(s: &'a str, field_tag: &str) -> IResult<&'a str, Vec<Arg>> {
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("args", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, maybe_field) = opt(tag(field_tag))(s)?;
|
|
|
|
if maybe_field.is_none() {
|
|
|
|
trace!("no arg record");
|
|
|
|
return Ok((s, vec![]));
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, args) = many1(arg)(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("},")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("args afterwards", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
Ok((s, args))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn arg(s: &str) -> IResult<&str, Arg> {
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("arg", s);
|
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("Argument{")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("name:")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
|
|
|
let (s, name) = nom_help::quoted_string(s)?;
|
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag(",")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("help:")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
|
|
|
let (s, help) = nom_help::quoted_string(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = opt(tag(","))(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
let (s, _) = nom_help::ignore_whitespace(s)?;
|
2023-10-23 14:25:33 +11:00
|
|
|
let (s, _) = tag("},")(s)?;
|
2023-10-24 12:30:48 +11:00
|
|
|
nom_help::short_trace("arg afterwards", s);
|
2023-10-23 14:25:33 +11:00
|
|
|
Ok((
|
|
|
|
s,
|
|
|
|
Arg {
|
|
|
|
name: name.to_string(),
|
|
|
|
help: help.to_string(),
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|