Skip to content

Commit

Permalink
Remove even more unsused rust code from rspc and httpz
Browse files Browse the repository at this point in the history
 - Remove unused features by spacedrive from rspc and httpz
 - Greatly simplify conditional compilation for the remaining features
 - Fix a couple of type mistakes and some other clippy suggestions
 - Minor update some deps
  • Loading branch information
HeavenVolkoff committed Oct 7, 2024
1 parent bc882f4 commit 345c3d6
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 539 deletions.
58 changes: 22 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
[package]
authors = ["Oscar Beaumont <[email protected]>"]
categories = ["asynchronous", "web-programming"]
description = "A blazing fast and easy to use TRPC server for Rust."
name = "rspc"
version = "0.1.4"

authors = ["Oscar Beaumont <[email protected]>"]
description = "A blazing fast and easy to use TRPC server for Rust."
edition = "2021"
license = "MIT"

documentation = "https://docs.rs/rspc/latest/rspc"
edition = "2021"
include = ["/LICENCE", "/README.md", "/src"]
keywords = ["async", "rust-to-ts", "specta", "typesafe", "typescript"]
license = "MIT"
name = "rspc"
repository = "https://github.com/oscartbeaumont/rspc"
version = "0.1.4"

categories = ["asynchronous", "web-programming"]
include = ["/LICENCE", "/README.md", "/src"]
keywords = ["async", "rust-to-ts", "specta", "typesafe", "typescript"]

# /bin/sh RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features
[package.metadata."docs.rs"]
Expand All @@ -18,16 +21,13 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []
httpz = ["dep:httpz"]
tauri = ["dep:tauri"]
# openapi = ["dep:openapiv3"]
anyhow = ["dep:anyhow"]

alpha = [] # APIs that are not yet stable
unstable = [] # APIs where one line of code can blow up your whole app
alpha = [] # APIs that are not yet stable
tauri = ["dep:tauri"]
unstable = [] # APIs where one line of code can blow up your whole app

# Webservers
axum = ["dep:httpz", "httpz/axum", "httpz/tokio-ws"]
axum = ["dep:httpz"]

# Specta # TODO: Remove all of these with v1
bigdecimal = ["specta/bigdecimal"]
Expand All @@ -45,39 +45,25 @@ uhlc = ["specta/uhlc"]
uuid = ["specta/uuid"]

[dependencies]
# Inner Sub-crates
httpz = { path = "./httpz", optional = true }

futures = "0.3.28"
futures-channel = "0.3.28"
# Dependencies
futures = "0.3.31"
futures-channel = "0.3.31"
futures-locks = { version = "0.7.1", features = ["tokio"] }
nougat = "0.2.4"
pin-project = "1.0"
pin-project = "1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
specta = { version = "=2.0.0-rc.20", features = ["derive", "serde", "serde_json"] }
specta-datatype-from = { git = "https://github.com/specta-rs/specta", rev = "8509af0162" }
specta-serde = { version = "=0.0.7" }
specta-typescript = { version = "=0.0.7", features = ["function"] }
tauri = { version = "2.0.1", optional = true }
thiserror = "1.0"
tokio = { version = "1.40", features = ["macros", "rt", "sync"] }
tracing = { version = "0.1.37" }

anyhow = { version = "1.0", optional = true }
tauri = { version = "2.0.1", optional = true }

[dev-dependencies]
# Tests
async-stream = "0.3.6"

# Benchmark
criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] }
pprof = { version = "0.13.0", features = [
"criterion",
"flamegraph",
"frame-pointer",
"protobuf-codec"
] }
tokio = { version = "1.29.1", features = ["macros", "rt-multi-thread"] }

[workspace]
members = ["./httpz"]
36 changes: 7 additions & 29 deletions httpz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,18 @@ categories = [
]
keywords = ["async", "http", "httpz", "web", "websockets"]

[features]
default = []
ws = []

# Async runtimes
async-tungstenite = [] # TODO: Remove this?
tokio-ws = [
"async-tungstenite/tokio-runtime",
"dep:async-tungstenite",
"dep:base64",
"dep:sha1",
"dep:tokio",
"ws"
] # TODO: This feature shouldn't force tokio as the async runtime
# TODO: Support other async runtimes

# Webservers
axum = ["dep:axum"]

[dependencies]
# Webservers
axum = { version = "0.6.20", optional = true, features = [] }

# Core
async-tungstenite = { version = "0.28.0", optional = true }
base64 = { version = "0.22.1", optional = true }
async-tungstenite = { version = "0.28.0", features = ["tokio-runtime"] }
axum = { version = "0.6.20", features = [] }
base64 = { version = "0.22.1" }
form_urlencoded = "1.2.0"
futures = "0.3.28"
http = { version = "0.2.12", features = [] }
hyper = "0.14.27" # TODO: Remove this if possible or feature gate it. I think Axum needs it.
percent-encoding = { version = "2.3.0", optional = true, features = [] }
sha1 = { version = "0.10.5", optional = true }
hyper = "0.14.27" # TODO: Remove this if possible or feature gate it. I think Axum needs it.
percent-encoding = { version = "2.3.0", features = [] }
sha1 = { version = "0.10.5" }
thiserror = "1.0"
tokio = { version = "1.29.1", features = [], default-features = false, optional = true }
tokio = { version = "1.29.1", features = [], default-features = false }
tracing = { version = "0.1.37" }

[dev-dependencies]
Expand Down
20 changes: 5 additions & 15 deletions httpz/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<div align="center">
<h1>httpz</h1>
<p><b>Code once, support every Rust webserver!</b></p>
<a href="https://discord.gg/JgqH8b4ycw"><img src="https://img.shields.io/discord/1011665225809924136?style=flat-square" alt="Discord"></a>
<a href="https://crates.io/crates/httpz"><img src="https://img.shields.io/crates/d/httpz?style=flat-square" alt="Crates.io"></a>
<a href="/LICENSE"><img src="https://img.shields.io/crates/l/httpz?style=flat-square" alt="License"></a>
</div>
<div align="center"><h1>httpz</h1></div>

<br>

This project is a 🚧 work in progress 🚧. Currently it is designed around the goals of [rspc](https://rspc.otbeaumont.me) but feel free to reach to me if you want to collaborate on using it in your own project.
This project is designed around the goals of [spacedrive](https://github.com/spacedriveapp/spacedrive)'s fork of [rspc](https://github.com/spacedriveapp/rspc) and there is no intention to expand it besides that.

## Usage

Expand Down Expand Up @@ -40,19 +34,15 @@ HttpServer::new({
// and so on...
```

Check out the rest of the [examples](/examples)!

## Features

- Write your HTTP handler once and support [Axum](https://github.com/tokio-rs/axum), [Actix Web](https://actix.rs/), [Poem](https://github.com/poem-web/poem), [Rocket](https://rocket.rs), [Warp](https://github.com/seanmonstar/warp) and more.
- Support for websockets on compatible webservers.
- Write your HTTP handler once and support [Axum](https://github.com/tokio-rs/axum).
- Support for websockets.

## Projects using httpz

httpz is primarily designed to make life easier for library authors. It allows a library author to write and test a HTTP endpoint once and know it will work for all major Rust HTTP servers.
httpz is primarily designed to make life easier for library authors. It allows a library author to write and test a HTTP endpoint once and know it will work for Axum.

Libraries using httpz:

- [rspc](https://github.com/oscartbeaumont/rspc)

If you are interested in using httpz and have questions jump in [the Discord](https://discord.gg/4V9M5sksw8)!
1 change: 0 additions & 1 deletion httpz/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ pub enum Error {
Utf8,
/// TODO
#[error("UTF-8 encoding error")]
#[cfg(feature = "tokio-ws")]
TungsteniteError(#[from] async_tungstenite::tungstenite::Error),
}
2 changes: 0 additions & 2 deletions httpz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod server;
mod servers;

/// is the module containing code related to handling incoming websockets.
#[cfg(feature = "ws")]
pub mod ws;

pub use endpoint::*;
Expand All @@ -21,6 +20,5 @@ pub use request::*;
pub use response::*;
pub use server::*;
pub use servers::*;

pub use form_urlencoded;
pub use http;
3 changes: 0 additions & 3 deletions httpz/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Server {
/// support for [Axum](https://github.com/tokio-rs/axum)
#[cfg(feature = "axum")]
Axum,
}

Expand All @@ -11,7 +10,6 @@ impl Server {
#[allow(unreachable_patterns)]
pub fn to_str(&self) -> &'static str {
match self {
#[cfg(feature = "axum")]
Self::Axum => "axum",
_ => unreachable!(),
}
Expand All @@ -21,7 +19,6 @@ impl Server {
#[allow(unreachable_patterns)]
pub fn supports_websockets(&self) -> bool {
match self {
#[cfg(feature = "axum")]
Self::Axum => true,
_ => unreachable!(),
}
Expand Down
1 change: 0 additions & 1 deletion httpz/src/servers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/// support for [Axum](https://github.com/tokio-rs/axum)
#[cfg(feature = "axum")]
pub mod axum;
53 changes: 0 additions & 53 deletions httpz/src/ws/fallback_ws.rs

This file was deleted.

25 changes: 1 addition & 24 deletions httpz/src/ws/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ pub enum Message {
/// The payload here must have a length less than 125 bytes
Pong(Vec<u8>),
/// A close message with the optional close frame.
#[cfg(feature = "tokio-ws")]
Close(Option<async_tungstenite::tungstenite::protocol::CloseFrame<'static>>),
/// Raw frame. Note, that you're not going to get this value while reading the message.
#[cfg(feature = "tokio-ws")]
Frame(async_tungstenite::tungstenite::protocol::frame::Frame),
}

Expand Down Expand Up @@ -66,7 +64,6 @@ impl Message {
}

/// Indicates whether a message ia s close message.
#[cfg(feature = "tokio-ws")]
pub fn is_close(&self) -> bool {
matches!(*self, Message::Close(_))
}
Expand All @@ -78,9 +75,7 @@ impl Message {
Message::Binary(ref data) | Message::Ping(ref data) | Message::Pong(ref data) => {
data.len()
}
#[cfg(feature = "tokio-ws")]
Message::Close(ref data) => data.as_ref().map(|d| d.reason.len()).unwrap_or(0),
#[cfg(feature = "tokio-ws")]
Message::Frame(ref frame) => frame.len(),
}
}
Expand All @@ -96,11 +91,8 @@ impl Message {
match self {
Message::Text(string) => string.into_bytes(),
Message::Binary(data) | Message::Ping(data) | Message::Pong(data) => data,
#[cfg(feature = "tokio-ws")]
Message::Close(None) => Vec::new(),
#[cfg(feature = "tokio-ws")]
Message::Close(Some(frame)) => frame.reason.into_owned().into_bytes(),
#[cfg(feature = "tokio-ws")]
Message::Frame(frame) => frame.into_data(),
}
}
Expand All @@ -112,11 +104,9 @@ impl Message {
Message::Binary(data) | Message::Ping(data) | Message::Pong(data) => {
Ok(String::from_utf8(data).map_err(|_| Error::Utf8)?)
}
#[cfg(feature = "tokio-ws")]

Message::Close(None) => Ok(String::new()),
#[cfg(feature = "tokio-ws")]
Message::Close(Some(frame)) => Ok(frame.reason.into_owned()),
#[cfg(feature = "tokio-ws")]
Message::Frame(frame) => Ok(frame.into_string().map_err(|_| Error::Utf8)?),
}
}
Expand All @@ -129,11 +119,8 @@ impl Message {
Message::Binary(ref data) | Message::Ping(ref data) | Message::Pong(ref data) => {
Ok(str::from_utf8(data).map_err(|_| Error::Utf8)?)
}
#[cfg(feature = "tokio-ws")]
Message::Close(None) => Ok(""),
#[cfg(feature = "tokio-ws")]
Message::Close(Some(ref frame)) => Ok(&frame.reason),
#[cfg(feature = "tokio-ws")]
Message::Frame(ref frame) => Ok(frame.to_text().map_err(|_| Error::Utf8)?),
}
}
Expand Down Expand Up @@ -187,37 +174,27 @@ impl fmt::Display for Message {
}
}

#[cfg(feature = "tokio-ws")]
impl From<async_tungstenite::tungstenite::Message> for Message {
fn from(msg: async_tungstenite::tungstenite::Message) -> Self {
match msg {
async_tungstenite::tungstenite::Message::Text(string) => Message::Text(string),
async_tungstenite::tungstenite::Message::Binary(data) => Message::Binary(data),
async_tungstenite::tungstenite::Message::Ping(data) => Message::Ping(data),
async_tungstenite::tungstenite::Message::Pong(data) => Message::Pong(data),
#[cfg(feature = "tokio-ws")]
async_tungstenite::tungstenite::Message::Close(frame) => Message::Close(frame),
#[cfg(not(feature = "tokio-ws"))]
async_tungstenite::tungstenite::Message::Close(frame) => unreachable!(),
#[cfg(feature = "tokio-ws")]
async_tungstenite::tungstenite::Message::Frame(frame) => Message::Frame(frame),
#[cfg(not(feature = "tokio-ws"))]
async_tungstenite::tungstenite::Message::Frame(frame) => unreachable!(),
}
}
}

#[cfg(feature = "tokio-ws")]
impl From<Message> for async_tungstenite::tungstenite::Message {
fn from(v: Message) -> Self {
match v {
Message::Text(string) => Self::Text(string),
Message::Binary(data) => Self::Binary(data),
Message::Ping(data) => Self::Ping(data),
Message::Pong(data) => Self::Pong(data),
#[cfg(feature = "tokio-ws")]
Message::Close(frame) => Self::Close(frame),
#[cfg(feature = "tokio-ws")]
Message::Frame(frame) => Self::Frame(frame),
}
}
Expand Down
Loading

0 comments on commit 345c3d6

Please sign in to comment.