Skip to content

Add glacier command for adding ICE snippets easily #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions parser/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::error::Error;
use crate::token::{Token, Tokenizer};

pub mod assign;
pub mod glacier;
pub mod nominate;
pub mod ping;
pub mod relabel;
Expand All @@ -17,6 +18,7 @@ pub enum Command<'a> {
Assign(Result<assign::AssignCommand, Error<'a>>),
Ping(Result<ping::PingCommand, Error<'a>>),
Nominate(Result<nominate::NominateCommand, Error<'a>>),
Glacier(Result<glacier::GlacierCommand, Error<'a>>),
None,
}

Expand Down Expand Up @@ -95,6 +97,11 @@ impl<'a> Input<'a> {
Command::Nominate,
&original_tokenizer,
));
success.extend(parse_single_command(
glacier::GlacierCommand::parse,
Command::Glacier,
&original_tokenizer,
));

if success.len() > 1 {
panic!(
Expand Down Expand Up @@ -133,6 +140,7 @@ impl<'a> Command<'a> {
Command::Assign(r) => r.is_ok(),
Command::Ping(r) => r.is_ok(),
Command::Nominate(r) => r.is_ok(),
Command::Glacier(r) => r.is_ok(),
Command::None => true,
}
}
Expand Down
99 changes: 99 additions & 0 deletions parser/src/command/glacier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! The glacier command parser.
//!
//! The grammar is as follows:
//!
//! ```text
//! Command:
//! - `@bot glacier add <code-source>?`
//! - `@bot glacier remove`
//!
//! <code-source>:
//! - "https://play.rust-lang.org/.*"
//! ```

use std::fmt;

use crate::error::Error;
use crate::token::{Token, Tokenizer};

#[derive(Debug, PartialEq)]
pub enum GlacierCommand {
Remove,
Add(CodeSource),
}

#[derive(Debug, PartialEq)]
pub enum CodeSource {
Post,
Url(String),
}

#[derive(Debug)]
pub enum ParseError {
UnknownSubCommand,
}

impl std::error::Error for ParseError {}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UnknownSubCommand => write!(f, "unknown command"),
}
}
}

impl GlacierCommand {
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<GlacierCommand>, Error<'a>> {
let mut toks = input.clone();
if let Some(Token::Word("glacier")) = toks.next_token()? {
if let Some(Token::Word("add")) = toks.peek_token()? {
toks.next_token()?;
Ok(Some(Self::Add(
if let Some(Token::Quote(url)) = toks.next_token()? {
CodeSource::Url(url.into())
} else {
CodeSource::Post
},
)))
} else if let Some(Token::Word("remove")) = toks.peek_token()? {
Ok(Some(Self::Remove))
} else {
Err(toks.error(ParseError::UnknownSubCommand))
}
} else {
Ok(None)
}
}
}

#[cfg(test)]
mod test {
use super::*;

fn parse<'a>(input: &'a str) -> Result<Option<GlacierCommand>, Error<'a>> {
let mut toks = Tokenizer::new(input);
Ok(GlacierCommand::parse(&mut toks)?)
}

#[test]
fn test_remove() {
assert_eq!(parse("glacier remove"), Ok(Some(GlacierCommand::Remove)));
}

#[test]
fn test_add_post() {
assert_eq!(
parse("glacier add"),
Ok(Some(GlacierCommand::Add(CodeSource::Post)))
);
}

#[test]
fn test_add_url() {
assert_eq!(
parse(r#"glacier add "https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a85913678bee64a3262db9a4a59463c2""#),
Ok(Some(GlacierCommand::Add(CodeSource::Url("https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a85913678bee64a3262db9a4a59463c2".into()))))
);
}
}