Skip to content

Commit

Permalink
review include module
Browse files Browse the repository at this point in the history
  • Loading branch information
museun committed Sep 23, 2020
1 parent fcfe625 commit 07b5cce
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 133 deletions.
39 changes: 5 additions & 34 deletions examples/async_io_demo.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// NOTE: this demo requires `--feature async-io`.
mod include;

use anyhow::Context;
use twitchchat::{commands, connector, runner::AsyncRunner, UserConfig};

use crate::include::{channels_to_join, get_user_config, handle_message};
use twitchchat::{
commands, connector, messages,
runner::{AsyncRunner, Status},
UserConfig,
};
// this is a helper module to reduce code deduplication
mod include;
use crate::include::{channels_to_join, get_user_config, main_loop};

async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Result<AsyncRunner> {
// create a connector using ``async_io``, this connects to Twitch.
Expand All @@ -29,37 +24,13 @@ async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Resul
// these two methods return whether the connection was closed early.
// we'll ignore it for this demo
println!("attempting to join '{}'", channel);
let _ = runner.join(&channel).await?;
let _ = runner.join(channel).await?;
println!("joined '{}'!", channel);
}

Ok(runner)
}

async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
handle_message(msg).await;
}

// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}

Ok(())
}

fn main() -> anyhow::Result<()> {
// create a user configuration
let user_config = get_user_config()?;
Expand Down
32 changes: 4 additions & 28 deletions examples/async_std_demo.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// NOTE: this demo requires `--features="async-std async-std/attributes"`.
mod include;
use anyhow::Context;

use crate::include::{channels_to_join, get_user_config, handle_message};
use twitchchat::{
commands, connector, messages,
runner::{AsyncRunner, Status},
UserConfig,
};

// this is a helper module to reduce code deduplication
mod include;
use crate::include::{channels_to_join, get_user_config, main_loop};

async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Result<AsyncRunner> {
// create a connector using ``async_std``, this connects to Twitch.
// you can provide a different address with `custom`
Expand All @@ -35,30 +35,6 @@ async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Resul
Ok(runner)
}

async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
handle_message(msg).await;
}

// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}

Ok(())
}

#[async_std::main]
async fn main() -> anyhow::Result<()> {
// create a user configuration
Expand Down
33 changes: 30 additions & 3 deletions examples/include/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Context;
use twitchchat::{messages, UserConfig};
#![allow(dead_code)]
use anyhow::Context as _;
use twitchchat::{messages, AsyncRunner, Status, UserConfig};

// some helpers for the demo
fn get_env_var(key: &str) -> anyhow::Result<String> {
Expand Down Expand Up @@ -32,7 +33,33 @@ pub fn channels_to_join() -> anyhow::Result<Vec<String>> {
Ok(channels)
}

pub async fn handle_message(msg: messages::Commands<'_>) {
// a 'main loop'
pub async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
handle_message(msg).await;
}

// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}

Ok(())
}

// you can generally ignore the lifetime for these types.
async fn handle_message(msg: messages::Commands<'_>) {
use messages::Commands::*;
// All sorts of messages
match msg {
Expand Down
16 changes: 9 additions & 7 deletions examples/simple_bot.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// note this uses `smol`. you can use `tokio` or `async_std` or `async_io` if you prefer.
mod include;
use anyhow::Context as _;
use std::collections::HashMap;

// this is a helper module to reduce code deduplication
// extensions to the Privmsg type
use crate::include::{channels_to_join, get_user_config};
use twitchchat::PrivmsgExt as _;
use twitchchat::{
messages::{Commands, Privmsg},
runner::{AsyncRunner, NotifyHandle, Status},
UserConfig,
};

// this is a helper module to reduce code deduplication
mod include;
use crate::include::{channels_to_join, get_user_config};

use std::collections::HashMap;

fn main() -> anyhow::Result<()> {
// you'll need a user configuration
let user_config = get_user_config()?;
Expand All @@ -24,12 +26,12 @@ fn main() -> anyhow::Result<()> {
.with_command("!hello", |args: Args| {
let output = format!("hello {}!", args.msg.name());
// We can 'reply' to this message using a writer + our output message
args.writer.reply(&args.msg, &output).unwrap();
args.writer.reply(args.msg, &output).unwrap();
})
.with_command("!uptime", move |args: Args| {
let output = format!("its been running for {:.2?}", start.elapsed());
// We can send a message back (without quoting the sender) using a writer + our output message
args.writer.say(&args.msg, &output).unwrap();
args.writer.say(args.msg, &output).unwrap();
})
.with_command("!quit", move |args: Args| {
// because we're using sync stuff, turn async into sync with smol!
Expand Down
38 changes: 5 additions & 33 deletions examples/smol_demo.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
// NOTE: this demo requires `--feature smol`.
mod include;
use anyhow::Context;
use twitchchat::{commands, connector, runner::AsyncRunner, UserConfig};

use crate::include::{channels_to_join, get_user_config, handle_message};
use twitchchat::{
commands, connector, messages,
runner::{AsyncRunner, Status},
UserConfig,
};
// this is a helper module to reduce code deduplication
mod include;
use crate::include::{channels_to_join, get_user_config, main_loop};

async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Result<AsyncRunner> {
// create a connector using ``smol``, this connects to Twitch.
Expand All @@ -28,37 +24,13 @@ async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Resul
// these two methods return whether the connection was closed early.
// we'll ignore it for this demo
println!("attempting to join '{}'", channel);
let _ = runner.join(&channel).await?;
let _ = runner.join(channel).await?;
println!("joined '{}'!", channel);
}

Ok(runner)
}

async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
handle_message(msg).await;
}

// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}

Ok(())
}

fn main() -> anyhow::Result<()> {
let fut = async move {
// create a user configuration
Expand Down
32 changes: 4 additions & 28 deletions examples/tokio_demo.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// NOTE: this demo requires `--features="tokio/full tokio-util"`.
mod include;
use anyhow::Context;

use crate::include::{channels_to_join, get_user_config, handle_message};
use twitchchat::{
commands, connector, messages,
runner::{AsyncRunner, Status},
UserConfig,
};

// this is a helper module to reduce code deduplication
mod include;
use crate::include::{channels_to_join, get_user_config, main_loop};

async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Result<AsyncRunner> {
// create a connector using ``tokio``, this connects to Twitch.
// you can provide a different address with `custom`
Expand All @@ -35,30 +35,6 @@ async fn connect(user_config: &UserConfig, channels: &[String]) -> anyhow::Resul
Ok(runner)
}

async fn main_loop(mut runner: AsyncRunner) -> anyhow::Result<()> {
loop {
match runner.next_message().await? {
// this is the parsed message -- across all channels (and notifications from Twitch)
Status::Message(msg) => {
handle_message(msg).await;
}

// you signaled a quit
Status::Quit => {
println!("we signaled we wanted to quit");
break;
}
// the connection closed normally
Status::Eof => {
println!("we got a 'normal' eof");
break;
}
}
}

Ok(())
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
// create a user configuration
Expand Down

0 comments on commit 07b5cce

Please sign in to comment.