Skip to content

Commit

Permalink
make channel-lists feature additive (fixes #257)
Browse files Browse the repository at this point in the history
  • Loading branch information
aatxe committed Mar 18, 2024
1 parent ecf411c commit ab0d1dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ members = [ "./", "irc-proto/" ]


[features]
default = ["ctcp", "tls-native", "toml_config"]
default = ["ctcp", "tls-native", "channel-lists", "toml_config"]
ctcp = []
nochanlists = []
channel-lists = []

json_config = ["serde", "serde/derive", "serde_derive", "serde_json"]
toml_config = ["serde", "serde/derive", "serde_derive", "toml"]
Expand Down
56 changes: 29 additions & 27 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_join(&self, _: &str, _: &str) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_join(&self, src: &str, chan: &str) {
if let Some(vec) = self.chanlists.write().get_mut(&chan.to_owned()) {
if !src.is_empty() {
Expand All @@ -684,10 +684,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_part(&self, _: &str, _: &str) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_part(&self, src: &str, chan: &str) {
if let Some(vec) = self.chanlists.write().get_mut(&chan.to_owned()) {
if !src.is_empty() {
Expand All @@ -698,10 +698,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_quit(&self, _: &str) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_quit(&self, src: &str) {
if src.is_empty() {
return;
Expand All @@ -714,10 +714,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_nick_change(&self, _: &str, _: &str) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_nick_change(&self, old_nick: &str, new_nick: &str) {
if old_nick.is_empty() || new_nick.is_empty() {
return;
Expand All @@ -731,10 +731,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_mode(&self, _: &str, _: &[Mode<ChannelMode>]) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_mode(&self, chan: &str, modes: &[Mode<ChannelMode>]) {
for mode in modes {
match *mode {
Expand All @@ -750,10 +750,10 @@ impl ClientState {
}
}

#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
fn handle_namreply(&self, _: &[String]) {}

#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
fn handle_namreply(&self, args: &[String]) {
if args.len() == 4 {
let chan = &args[2];
Expand Down Expand Up @@ -996,8 +996,8 @@ impl Client {
}

/// Gets a list of currently joined channels. This will be `None` if tracking is disabled
/// altogether via the `nochanlists` feature.
#[cfg(not(feature = "nochanlists"))]
/// altogether by disabling the `channel-lists` feature.
#[cfg(feature = "channel-lists")]
pub fn list_channels(&self) -> Option<Vec<String>> {
Some(
self.state
Expand All @@ -1009,13 +1009,14 @@ impl Client {
)
}

#[cfg(feature = "nochanlists")]
/// Always returns `None` since `channel-lists` feature is disabled.
#[cfg(not(feature = "channel-lists"))]
pub fn list_channels(&self) -> Option<Vec<String>> {
None
}

/// Gets a list of [`Users`](./data/user/struct.User.html) in the specified channel. If the
/// specified channel hasn't been joined or the `nochanlists` feature is enabled, this function
/// specified channel hasn't been joined or the `channel-lists` feature is disabled, this function
/// will return `None`.
///
/// For best results, be sure to request `multi-prefix` support from the server. This will allow
Expand All @@ -1033,12 +1034,13 @@ impl Client {
/// # Ok(())
/// # }
/// ```
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
pub fn list_users(&self, chan: &str) -> Option<Vec<User>> {
self.state.chanlists.read().get(&chan.to_owned()).cloned()
}

#[cfg(feature = "nochanlists")]
/// Always returns `None` since `channel-lists` feature is disabled.
#[cfg(not(feature = "channel-lists"))]
pub fn list_users(&self, _: &str) -> Option<Vec<User>> {
None
}
Expand Down Expand Up @@ -1092,7 +1094,7 @@ mod test {
use std::{collections::HashMap, default::Default, thread, time::Duration};

use super::Client;
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
use crate::client::data::User;
use crate::{
client::data::Config,
Expand Down Expand Up @@ -1356,7 +1358,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn channel_tracking_names() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n";
let mut client = Client::from_config(Config {
Expand All @@ -1370,7 +1372,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn channel_tracking_names_part() -> Result<()> {
use crate::proto::command::Command::PART;

Expand All @@ -1392,7 +1394,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn user_tracking_names() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n";
let mut client = Client::from_config(Config {
Expand All @@ -1409,7 +1411,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn user_tracking_names_join() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n\
:test2!test@test JOIN #test\r\n";
Expand All @@ -1432,7 +1434,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn user_tracking_names_kick() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n\
:owner!test@test KICK #test test\r\n";
Expand All @@ -1450,7 +1452,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn user_tracking_names_part() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n\
:owner!test@test PART #test\r\n";
Expand All @@ -1468,7 +1470,7 @@ mod test {
}

#[tokio::test]
#[cfg(not(feature = "nochanlists"))]
#[cfg(feature = "channel-lists")]
async fn user_tracking_names_mode() -> Result<()> {
let value = ":irc.test.net 353 test = #test :+test ~owner &admin\r\n\
:test!test@test MODE #test +o test\r\n";
Expand Down Expand Up @@ -1497,7 +1499,7 @@ mod test {
}

#[tokio::test]
#[cfg(feature = "nochanlists")]
#[cfg(not(feature = "channel-lists"))]
async fn no_user_tracking() -> Result<()> {
let value = ":irc.test.net 353 test = #test :test ~owner &admin\r\n";
let mut client = Client::from_config(Config {
Expand Down

0 comments on commit ab0d1dd

Please sign in to comment.