Skip to content

Commit

Permalink
workspace builds with all features enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
babymotte committed Jun 28, 2022
1 parent f98be20 commit 8d34d71
Show file tree
Hide file tree
Showing 26 changed files with 251 additions and 191 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]

members = ["libworterbuch", "worterbuch", "worterbuch-cli"]

[patch.crates-io]
libworterbuch = { path = "./libworterbuch" }
6 changes: 2 additions & 4 deletions libworterbuch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ keywords = ["message", "broker", "data", "base", "pubsub"]
categories = ["database"]

[features]
default = ["client", "tcp"]
client = []
server = []
default = []
tcp = ["async", "tokio/net"]
ws = ["async", "web"]
graphql = ["async", "web", "futures-channel", "url"]
Expand All @@ -22,7 +20,7 @@ web = ["futures-util", "tokio-tungstenite"]

[dependencies]
log = "0.4.17"
tokio = { version = "1.19.2" }
tokio = { version = "1.19.2", features = ["sync", "rt"] }
serde = { version = "1.0.137", features = ["derive"] }
serde_json = { version = "1.0.81" }

Expand Down
37 changes: 8 additions & 29 deletions libworterbuch/Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
[tasks.test-server]
[tasks.test-all-async]
command = "cargo"
args = ["test", "--no-default-features", "--features", "server,tcp,ws,graphql"]
args = ["test", "--no-default-features", "--features", "tcp,ws,graphql"]

[tasks.test-server-sync]
[tasks.test-sync]
command = "cargo"
args = ["test", "--no-default-features", "--features", "server"]

[tasks.test-client-tcp]
command = "cargo"
args = ["test", "--no-default-features", "--features", "client,tcp"]

[tasks.test-client-ws]
command = "cargo"
args = ["test", "--no-default-features", "--features", "client,ws"]

[tasks.test-client-graphql]
command = "cargo"
args = ["test", "--no-default-features", "--features", "client,graphql"]

[tasks.test-client-sync]
command = "cargo"
args = ["test", "--no-default-features", "--features", "client"]
args = ["test", "--no-default-features"]

[tasks.test-features]
dependencies = [
"test-server",
"test-server-sync",
"test-client-tcp",
"test-client-ws",
"test-client-graphql",
"test-client-sync",
]
dependencies = ["test-all-async", "test-sync"]

[tasks.docker]

[tasks.docker]
[tasks.install]
61 changes: 61 additions & 0 deletions libworterbuch/src/client/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use crate::error::ConfigResult;
use std::env;

#[derive(Debug, Clone, PartialEq)]
pub struct Config {
pub proto: String,
pub host_addr: String,
pub port: u16,
}

impl Config {
pub fn load_env(&mut self) -> ConfigResult<()> {
if let Ok(val) = env::var("WORTERBUCH_PROTO") {
self.proto = val;
}

if let Ok(val) = env::var("WORTERBUCH_HOST_ADDRESS") {
self.host_addr = val;
}

if let Ok(val) = env::var("WORTERBUCH_PORT") {
self.port = val.parse()?;
}

Ok(())
}

pub fn new() -> ConfigResult<Self> {
let mut config = Config::default();
config.load_env()?;
Ok(config)
}
}

impl Default for Config {
fn default() -> Self {
#[cfg(any(feature = "ws", feature = "graphql"))]
let _proto = "ws".to_owned();
#[cfg(feature = "tcp")]
let _proto = "tcp".to_owned();
#[cfg(not(any(feature = "tcp", feature = "ws", feature = "graphql")))]
let _proto = "".to_owned();

let host_addr = "localhost".to_owned();

#[cfg(feature = "graphql")]
let _port = 4243;
#[cfg(feature = "ws")]
let _port = 8080;
#[cfg(feature = "tcp")]
let _port = 4242;
#[cfg(not(any(feature = "tcp", feature = "ws", feature = "graphql")))]
let _port = 0;

Config {
proto: _proto,
host_addr,
port: _port,
}
}
}
2 changes: 1 addition & 1 deletion libworterbuch/src/client/gql.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::ServerMessage as SM, error::ConnectionResult, client::Connection};
use crate::{client::Connection, codec::ServerMessage as SM, error::ConnectionResult};
use futures_channel::mpsc::{self, UnboundedSender};
use futures_util::StreamExt;
use serde_json::Value;
Expand Down
4 changes: 2 additions & 2 deletions libworterbuch/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub mod config;
#[cfg(feature = "graphql")]
pub mod gql;
#[cfg(feature = "tcp")]
pub mod tcp;
#[cfg(feature = "ws")]
pub mod ws;

use crate::error::ConnectionError;

use super::codec::ServerMessage;
use super::error::ConnectionResult;
use crate::error::ConnectionError;
use tokio::sync::broadcast;

pub trait Connection {
Expand Down
154 changes: 78 additions & 76 deletions libworterbuch/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use crate::codec::{
ErrorCode, KeyLength, MessageType, MetaData, MetaDataLength, NumKeyValuePairs, PathLength,
RequestPattern, RequestPatternLength, ValueLength,
};
#[cfg(feature = "graphql")]
use futures_channel::mpsc::TrySendError;
use std::{fmt, io, net::AddrParseError, num::ParseIntError, string::FromUtf8Error};
use tokio::sync::mpsc::error::SendError;
#[cfg(feature = "web")]
use tokio_tungstenite::tungstenite;
#[cfg(feature = "graphql")]
use url::ParseError;

#[derive(Debug)]
pub enum DecodeError {
Expand Down Expand Up @@ -146,6 +153,18 @@ impl fmt::Display for ConfigError {
}
}

impl From<ParseIntError> for ConfigError {
fn from(e: ParseIntError) -> Self {
ConfigError::InvalidPort(e)
}
}

impl From<AddrParseError> for ConfigError {
fn from(e: AddrParseError) -> Self {
ConfigError::InvalidAddr(e)
}
}

pub type ConfigResult<T> = std::result::Result<T, ConfigError>;

pub trait Context<T, E: std::error::Error> {
Expand Down Expand Up @@ -205,100 +224,83 @@ impl<T, V: fmt::Debug + 'static + Send + Sync> Context<T, SendError<V>>

pub type WorterbuchResult<T> = std::result::Result<T, WorterbuchError>;

#[cfg(feature = "client")]
pub use client::*;
use tokio::sync::mpsc::error::SendError;
#[cfg(feature = "client")]
mod client {
use super::EncodeError;
use core::fmt;
#[cfg(feature = "graphql")]
use futures_channel::mpsc::TrySendError;
use std::{fmt::Debug, io};
use tokio::sync::mpsc::error::SendError;
#[derive(Debug)]
pub enum ConnectionError {
IoError(io::Error),
EncodeError(EncodeError),
SendError(Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "web")]
use tokio_tungstenite::tungstenite;
WebsocketError(tungstenite::Error),
TrySendError(Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "graphql")]
use url::ParseError;

#[derive(Debug)]
pub enum ConnectionError {
IoError(io::Error),
EncodeError(EncodeError),
SendError(Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "web")]
WebsocketError(tungstenite::Error),
TrySendError(Box<dyn std::error::Error + Send + Sync>),
#[cfg(feature = "graphql")]
JsonError(serde_json::Error),
#[cfg(feature = "graphql")]
ParseError(ParseError),
}
JsonError(serde_json::Error),
#[cfg(feature = "graphql")]
ParseError(ParseError),
}

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

impl fmt::Display for ConnectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IoError(e) => fmt::Display::fmt(&e, f),
Self::EncodeError(e) => fmt::Display::fmt(&e, f),
Self::SendError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "graphql")]
Self::JsonError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "web")]
Self::WebsocketError(e) => fmt::Display::fmt(&e, f),
Self::TrySendError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "graphql")]
Self::ParseError(e) => fmt::Display::fmt(&e, f),
}
impl std::error::Error for ConnectionError {}

impl fmt::Display for ConnectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IoError(e) => fmt::Display::fmt(&e, f),
Self::EncodeError(e) => fmt::Display::fmt(&e, f),
Self::SendError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "graphql")]
Self::JsonError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "web")]
Self::WebsocketError(e) => fmt::Display::fmt(&e, f),
Self::TrySendError(e) => fmt::Display::fmt(&e, f),
#[cfg(feature = "graphql")]
Self::ParseError(e) => fmt::Display::fmt(&e, f),
}
}
}

pub type ConnectionResult<T> = std::result::Result<T, ConnectionError>;
pub type ConnectionResult<T> = std::result::Result<T, ConnectionError>;

impl From<EncodeError> for ConnectionError {
fn from(e: EncodeError) -> Self {
ConnectionError::EncodeError(e)
}
impl From<EncodeError> for ConnectionError {
fn from(e: EncodeError) -> Self {
ConnectionError::EncodeError(e)
}
}

impl From<io::Error> for ConnectionError {
fn from(e: io::Error) -> Self {
ConnectionError::IoError(e)
}
impl From<io::Error> for ConnectionError {
fn from(e: io::Error) -> Self {
ConnectionError::IoError(e)
}
}

impl<T: Debug + 'static + Send + Sync> From<SendError<T>> for ConnectionError {
fn from(e: SendError<T>) -> Self {
ConnectionError::SendError(Box::new(e))
}
impl<T: fmt::Debug + 'static + Send + Sync> From<SendError<T>> for ConnectionError {
fn from(e: SendError<T>) -> Self {
ConnectionError::SendError(Box::new(e))
}
}

#[cfg(feature = "web")]
impl From<tungstenite::Error> for ConnectionError {
fn from(e: tungstenite::Error) -> Self {
ConnectionError::WebsocketError(e)
}
#[cfg(feature = "web")]
impl From<tungstenite::Error> for ConnectionError {
fn from(e: tungstenite::Error) -> Self {
ConnectionError::WebsocketError(e)
}
}

#[cfg(feature = "graphql")]
impl From<ParseError> for ConnectionError {
fn from(e: ParseError) -> Self {
ConnectionError::ParseError(e)
}
#[cfg(feature = "graphql")]
impl From<ParseError> for ConnectionError {
fn from(e: ParseError) -> Self {
ConnectionError::ParseError(e)
}
}

#[cfg(feature = "graphql")]
impl From<serde_json::Error> for ConnectionError {
fn from(e: serde_json::Error) -> Self {
ConnectionError::JsonError(e)
}
#[cfg(feature = "graphql")]
impl From<serde_json::Error> for ConnectionError {
fn from(e: serde_json::Error) -> Self {
ConnectionError::JsonError(e)
}
}

#[cfg(feature = "graphql")]
impl<T: 'static + Send + Sync> From<TrySendError<T>> for ConnectionError {
fn from(e: TrySendError<T>) -> Self {
ConnectionError::TrySendError(Box::new(e))
}
#[cfg(feature = "graphql")]
impl<T: 'static + Send + Sync> From<TrySendError<T>> for ConnectionError {
fn from(e: TrySendError<T>) -> Self {
ConnectionError::TrySendError(Box::new(e))
}
}
5 changes: 1 addition & 4 deletions libworterbuch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
pub mod client;
pub mod codec;
pub mod config;
pub mod error;

#[cfg(feature = "client")]
pub mod client;
2 changes: 1 addition & 1 deletion worterbuch-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ws = ["libworterbuch/ws"]
graphql = ["libworterbuch/graphql"]

[dependencies]
libworterbuch = { path = "../libworterbuch", features = ["client"] }
libworterbuch = { version = "0.1.1" }
tokio = { version = "1.19.2", features = ["full"] }
dotenv = "0.15.0"
anyhow = "1.0.57"
Expand Down
Loading

0 comments on commit 8d34d71

Please sign in to comment.