From ad49703714eea8c9e9abd0c806feb140c42c9a50 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Fri, 22 Sep 2023 13:20:47 +0000 Subject: [PATCH] split proto irc defs into its own crate (#13) Reviewed-on: https://git.vilunov.me/lavina/lavina/pulls/13 --- Cargo.lock | 13 ++++++++ Cargo.toml | 32 +++++++++++++++---- crates/proto-irc/Cargo.toml | 14 ++++++++ .../irc => crates/proto-irc/src}/client.rs | 0 .../irc/mod.rs => crates/proto-irc/src/lib.rs | 7 ++-- crates/proto-irc/src/prelude.rs | 3 ++ .../irc => crates/proto-irc/src}/server.rs | 8 +++-- crates/proto-irc/src/testkit.rs | 16 ++++++++++ .../irc => crates/proto-irc/src}/user.rs | 12 +++---- src/projections/irc/mod.rs | 11 ++++--- src/protos/mod.rs | 1 - 11 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 crates/proto-irc/Cargo.toml rename {src/protos/irc => crates/proto-irc/src}/client.rs (100%) rename src/protos/irc/mod.rs => crates/proto-irc/src/lib.rs (98%) create mode 100644 crates/proto-irc/src/prelude.rs rename {src/protos/irc => crates/proto-irc/src}/server.rs (98%) create mode 100644 crates/proto-irc/src/testkit.rs rename {src/protos/irc => crates/proto-irc/src}/user.rs (67%) diff --git a/Cargo.lock b/Cargo.lock index ad25c33..4b7207c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -763,6 +763,7 @@ dependencies = [ "nom", "nonempty", "prometheus", + "proto-irc", "quick-xml", "regex", "reqwest", @@ -1151,6 +1152,18 @@ dependencies = [ "thiserror", ] +[[package]] +name = "proto-irc" +version = "0.0.1-dev" +dependencies = [ + "anyhow", + "assert_matches", + "futures-util", + "nom", + "nonempty", + "tokio", +] + [[package]] name = "quick-xml" version = "0.30.0" diff --git a/Cargo.toml b/Cargo.toml index 7b855e3..7f6593c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,33 +1,51 @@ +[workspace] +members = [ + ".", + "crates/proto-irc" +] + +[workspace.package] +version = "0.0.1-dev" + +[workspace.dependencies] +nom = "7.1.3" +assert_matches = "1.5.0" +tokio = { version = "1.24.1", features = ["full"] } # async runtime +futures-util = "0.3.25" +anyhow = "1.0.68" # error utils +nonempty = "0.8.1" + [package] name = "lavina" -version = "0.0.1-dev" +version.workspace = true edition = "2021" publish = false [dependencies] -anyhow = "1.0.68" # error utils +anyhow.workspace = true figment = { version = "0.10.8", features = ["env", "toml"] } # configuration files hyper = { version = "1.0.0-rc.3,<1.0.0-rc.4", features = ["server", "http1"] } # http server http-body-util = "0.1.0-rc.3" serde = { version = "1.0.152", features = ["rc", "serde_derive"] } serde_json = "1.0.93" -tokio = { version = "1.24.1", features = ["full"] } # async runtime +tokio.workspace = true tracing = "0.1.37" # logging & tracing api tracing-subscriber = "0.3.16" -futures-util = "0.3.25" +futures-util.workspace = true prometheus = { version = "0.13.3", default-features = false } regex = "1.7.1" lazy_static = "1.4.0" -nom = "7.1.3" -nonempty = "0.8.1" +nom.workspace = true +nonempty.workspace = true tokio-rustls = "0.24.1" rustls-pemfile = "1.0.2" quick-xml = { version = "0.30.0", features = ["async-tokio"] } derive_more = "0.99.17" uuid = { version = "1.3.0", features = ["v4"] } sqlx = { version = "0.7.0-alpha.2", features = ["sqlite", "migrate"] } +proto-irc = { path = "crates/proto-irc" } [dev-dependencies] -assert_matches = "1.5.0" +assert_matches.workspace = true regex = "1.7.1" reqwest = { version = "0.11", default-features = false } diff --git a/crates/proto-irc/Cargo.toml b/crates/proto-irc/Cargo.toml new file mode 100644 index 0000000..e2225a7 --- /dev/null +++ b/crates/proto-irc/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "proto-irc" +edition = "2021" +version.workspace = true + +[dependencies] +nom.workspace = true +nonempty.workspace = true +tokio.workspace = true +futures-util.workspace = true +anyhow.workspace = true + +[dev-dependencies] +assert_matches.workspace = true diff --git a/src/protos/irc/client.rs b/crates/proto-irc/src/client.rs similarity index 100% rename from src/protos/irc/client.rs rename to crates/proto-irc/src/client.rs diff --git a/src/protos/irc/mod.rs b/crates/proto-irc/src/lib.rs similarity index 98% rename from src/protos/irc/mod.rs rename to crates/proto-irc/src/lib.rs index 0997a79..cd1f64f 100644 --- a/src/protos/irc/mod.rs +++ b/crates/proto-irc/src/lib.rs @@ -1,10 +1,13 @@ //! Client-to-Server IRC protocol. pub mod client; +mod prelude; pub mod server; +#[cfg(test)] +mod testkit; pub mod user; -use std::io::Result; use crate::prelude::Str; +use std::io::Result; use nom::{ branch::alt, @@ -101,7 +104,7 @@ mod test { use assert_matches::*; use super::*; - use crate::util::testkit::*; + use crate::testkit::*; #[test] fn test_chan_global() { diff --git a/crates/proto-irc/src/prelude.rs b/crates/proto-irc/src/prelude.rs new file mode 100644 index 0000000..04c4d62 --- /dev/null +++ b/crates/proto-irc/src/prelude.rs @@ -0,0 +1,3 @@ +use std::sync::Arc; + +pub type Str = Arc; diff --git a/src/protos/irc/server.rs b/crates/proto-irc/src/server.rs similarity index 98% rename from src/protos/irc/server.rs rename to crates/proto-irc/src/server.rs index 194c756..1bead65 100644 --- a/src/protos/irc/server.rs +++ b/crates/proto-irc/src/server.rs @@ -3,7 +3,7 @@ use tokio::io::AsyncWrite; use tokio::io::AsyncWriteExt; use super::*; -use crate::protos::irc::user::PrefixedNick; +use crate::user::PrefixedNick; /// Server-to-client message. #[derive(Clone, Debug, PartialEq, Eq)] @@ -287,7 +287,9 @@ impl ServerMessageBody { chan.write_async(writer).await?; writer.write_all(b" :").await?; for member in members { - writer.write_all(member.prefix.to_string().as_bytes()).await?; + writer + .write_all(member.prefix.to_string().as_bytes()) + .await?; writer.write_all(member.nick.as_bytes()).await?; writer.write_all(b" ").await?; } @@ -387,7 +389,7 @@ mod test { use assert_matches::*; use super::*; - use crate::util::testkit::*; + use crate::testkit::*; #[test] fn test_server_message_notice() { diff --git a/crates/proto-irc/src/testkit.rs b/crates/proto-irc/src/testkit.rs new file mode 100644 index 0000000..57949d0 --- /dev/null +++ b/crates/proto-irc/src/testkit.rs @@ -0,0 +1,16 @@ +use std::future::Future; +use std::task::{Context, Poll}; + +use futures_util::task::noop_waker_ref; +use tokio::pin; + +pub fn sync_future(future: impl Future) -> anyhow::Result { + let waker = noop_waker_ref(); + let mut context = Context::from_waker(waker); + pin!(future); + if let Poll::Ready(a) = future.poll(&mut context) { + Ok(a) + } else { + Err(anyhow::Error::msg("Future has suspended")) + } +} diff --git a/src/protos/irc/user.rs b/crates/proto-irc/src/user.rs similarity index 67% rename from src/protos/irc/user.rs rename to crates/proto-irc/src/user.rs index e426812..3998fc2 100644 --- a/src/protos/irc/user.rs +++ b/crates/proto-irc/src/user.rs @@ -1,6 +1,5 @@ use super::*; use std::fmt; -use crate::core::player::PlayerId; #[derive(Clone, Debug, PartialEq, Eq)] pub enum Prefix { @@ -23,10 +22,9 @@ pub struct PrefixedNick { impl PrefixedNick { pub fn from_str(nick: Str) -> PrefixedNick { - PrefixedNick { prefix: Prefix::Empty, nick } + PrefixedNick { + prefix: Prefix::Empty, + nick, + } } - - pub fn from_player_id(id: PlayerId) -> PrefixedNick { - PrefixedNick { prefix: Prefix::Empty, nick: id.into_inner() } - } -} \ No newline at end of file +} diff --git a/src/projections/irc/mod.rs b/src/projections/irc/mod.rs index 43e580b..96dc877 100644 --- a/src/projections/irc/mod.rs +++ b/src/projections/irc/mod.rs @@ -5,6 +5,7 @@ use futures_util::future::join_all; use nonempty::nonempty; use nonempty::NonEmpty; use prometheus::{IntCounter, IntGauge, Registry as MetricsRegistry}; + use serde::Deserialize; use tokio::io::{AsyncBufReadExt, AsyncWrite, AsyncWriteExt, BufReader, BufWriter}; use tokio::net::tcp::{ReadHalf, WriteHalf}; @@ -15,10 +16,10 @@ use crate::core::player::*; use crate::core::repo::Storage; use crate::core::room::{RoomId, RoomInfo, RoomRegistry}; use crate::prelude::*; -use crate::protos::irc::client::{client_message, ClientMessage}; -use crate::protos::irc::server::{AwayStatus, ServerMessage, ServerMessageBody}; -use crate::protos::irc::{Chan, Recipient}; -use crate::protos::irc::user::PrefixedNick; +use proto_irc::client::{client_message, ClientMessage}; +use proto_irc::server::{AwayStatus, ServerMessage, ServerMessageBody}; +use proto_irc::{Chan, Recipient}; +use proto_irc::user::{Prefix, PrefixedNick}; use crate::util::Terminator; #[cfg(test)] @@ -669,7 +670,7 @@ async fn produce_on_join_cmd_messages( } .write_async(writer) .await?; - let prefixed_members: Vec = room_info.members.iter().map(|member| PrefixedNick::from_player_id(member.clone())).collect(); + let prefixed_members: Vec = room_info.members.iter().map(|member| PrefixedNick::from_str(member.clone().into_inner())).collect(); let non_empty_members: NonEmpty = NonEmpty::from_vec(prefixed_members).unwrap_or(nonempty![PrefixedNick::from_str(user.nickname.clone())]); ServerMessage { diff --git a/src/protos/mod.rs b/src/protos/mod.rs index 25f58e2..c8c39c3 100644 --- a/src/protos/mod.rs +++ b/src/protos/mod.rs @@ -1,3 +1,2 @@ //! Definitions of wire protocols to be used in implementations of projections. -pub mod irc; pub mod xmpp;