feat(src): Actually allow access to the parsed values via getters
This commit is contained in:
parent
227cfe5e97
commit
732037c4a9
42
src/lib.rs
42
src/lib.rs
|
@ -72,26 +72,42 @@
|
|||
//! 1. `function() print(`
|
||||
//! 1. `Hi!`
|
||||
//! 1. `) end`
|
||||
use std::fmt::Display;
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
use parsing::{Rule, TrinitryParser};
|
||||
use pest::error::Error;
|
||||
|
||||
mod parsing;
|
||||
|
||||
pub struct Command {
|
||||
pub struct TrinitryInvokation {
|
||||
command: String,
|
||||
arguments: Vec<String>,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub fn new(input: &str) -> Result<Self, Error<Rule>> {
|
||||
let parsed = TrinitryParser::new(input)?;
|
||||
impl TrinitryInvokation {
|
||||
pub fn new(input: &str) -> Result<Self, <Self as FromStr>::Err> {
|
||||
input.parse()
|
||||
}
|
||||
|
||||
pub fn command(&self) -> &str {
|
||||
&self.command
|
||||
}
|
||||
|
||||
pub fn arguments(&self) -> &[String] {
|
||||
&self.arguments
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for TrinitryInvokation {
|
||||
type Err = Error<Rule>;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let parsed = TrinitryParser::new(s)?;
|
||||
Ok(Self::from(parsed))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TrinitryParser<'_>> for Command {
|
||||
impl From<TrinitryParser<'_>> for TrinitryInvokation {
|
||||
fn from(parsed: TrinitryParser) -> Self {
|
||||
let command = {
|
||||
let command: Vec<_> = parsed.0.clone().find_tagged("command").collect();
|
||||
|
@ -143,7 +159,7 @@ impl From<TrinitryParser<'_>> for Command {
|
|||
}
|
||||
}
|
||||
|
||||
impl Display for Command {
|
||||
impl Display for TrinitryInvokation {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if self.arguments.is_empty() {
|
||||
f.write_str(&self.command)
|
||||
|
@ -161,12 +177,12 @@ mod tests;
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::Command;
|
||||
use crate::TrinitryInvokation;
|
||||
|
||||
#[test]
|
||||
fn parse_cmd() {
|
||||
let string = "quit";
|
||||
let p = Command::new(string).unwrap_or_else(|e| {
|
||||
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| {
|
||||
panic!("{}", e);
|
||||
});
|
||||
assert_eq!(&p.command, "quit");
|
||||
|
@ -176,7 +192,7 @@ mod test {
|
|||
#[test]
|
||||
fn parse_arg_clean() {
|
||||
let string = r##"lua print("Hi")"##;
|
||||
let p = Command::new(string).unwrap_or_else(|e| {
|
||||
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| {
|
||||
panic!("{}", e);
|
||||
});
|
||||
assert_eq!(&p.command, "lua");
|
||||
|
@ -186,7 +202,7 @@ mod test {
|
|||
#[test]
|
||||
fn parse_arg_quote() {
|
||||
let string = r##"write "some 'file' name""##;
|
||||
let p = Command::new(string).unwrap_or_else(|e| {
|
||||
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| {
|
||||
panic!("{}", e);
|
||||
});
|
||||
assert_eq!(&p.command, "write");
|
||||
|
@ -196,7 +212,7 @@ mod test {
|
|||
#[test]
|
||||
fn parse_arg_single_quote() {
|
||||
let string = r##"write 'some "file" name'"##;
|
||||
let p = Command::new(string).unwrap_or_else(|e| {
|
||||
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| {
|
||||
panic!("{}", e);
|
||||
});
|
||||
assert_eq!(&p.command, "write");
|
||||
|
@ -206,7 +222,7 @@ mod test {
|
|||
#[test]
|
||||
fn parse_arg_multi() {
|
||||
let string = r##"write 'some "file" name' "other name" last"##;
|
||||
let p = Command::new(string).unwrap_or_else(|e| {
|
||||
let p = TrinitryInvokation::new(string).unwrap_or_else(|e| {
|
||||
panic!("{}", e);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue