vtubestudio/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(
3 missing_docs,
4 missing_debug_implementations,
5 rustdoc::broken_intra_doc_links,
6 rustdoc::private_intra_doc_links
7)]
8
9//! A library for interacting with the [VTube Studio API].
10//!
11//! This crate exposes a [`Client`] for making requests to the VTube Studio websocket API, and
12//! handles the work of mapping requests to responses (using [`tokio_tower::multiplex`]).
13//!
14//! The client wraps a set of configurable [`tower::Service`] middleware for handling the
15//! [authentication flow](crate::service::Authentication), [retries](crate::service::RetryPolicy),
16//! and [reconnects](crate::service::MakeApiService), and uses [`tokio_tungstenite`] as the
17//! underlying websocket transport by default.
18//!
19//! [VTube Studio API]: https://github.com/DenchiSoft/VTubeStudio
20//!
21//! # Basic usage
22//!
23//! The example below creates a [`Client`] using the provided [builder](ClientBuilder), which:
24//!
25//! * connects to `ws://localhost:8001` using [`tokio_tungstenite`](https://docs.rs/tokio_tungstenite)
26//! * authenticates with an existing token (if present and valid)
27//! * reconnects when disconnected, and retries the failed request on reconnection success
28//! * requests a new auth token on receiving an auth error, and retries the initial failed
29//! request on authentication success
30//!
31#![cfg_attr(feature = "tokio-tungstenite", doc = "```no_run")]
32#![cfg_attr(not(feature = "tokio-tungstenite"), doc = "```ignore")]
33#![doc = include_str!("../examples/readme.rs")]
34//! ```
35//!
36//! To send multiple outgoing requests at the same time without waiting for a request to come back,
37//! you can clone the [`Client`] per request (by default, the client wraps a
38//! [`tower::buffer::Buffer`] which adds an mpsc buffer in front of the underlying websocket
39//! transport).
40//!
41//! For an example of constructing a [`Client`] manually without the builder, check the
42//! [`no_middleware` example] in the repo.
43//!
44//! [`no_middleware` example]: https://github.com/walfie/vtubestudio-rs/blob/master/examples/no_middleware.rs
45//!
46//! # Events
47//!
48//! The [`ClientEventStream`] returned from the [`ClientBuilder`] will also return
49//! [`Event`](crate::data::Event)s if we subscribe to them.
50//!
51//! The example below demonstrates subscribing to [`TestEvent`](crate::data::TestEvent)s, which
52//! will be emitted every second.
53//!
54#![cfg_attr(feature = "tokio-tungstenite", doc = "```no_run")]
55#![cfg_attr(not(feature = "tokio-tungstenite"), doc = "```ignore")]
56#![doc = include_str!("../examples/events.rs")]
57//! ```
58//!
59//! # Project structure
60//!
61//! * [`client`] provides a high level API dealing with typed [`Request`]/[`Response`] types, which wraps a... ⏎
62//! * [`service`], a stack of [`tower::Service`]s that deal with
63//! [`RequestEnvelope`]/[`ResponseEnvelope`] pairs, and wraps a... ⏎
64//! * [`transport`], which describes the underlying websocket connection stream, using a... ⏎
65//! * [`codec`] to determine how to encode/decode websocket messages
66//!
67//! While the provided [`ClientBuilder`] should be sufficient for most users, each of these layers
68//! can be modified to add custom behavior if needed. E.g.,
69//!
70//! * using a different combination of tower middleware
71//! * using a different websocket library
72//! * adding custom request/response types (as an escape hatch, if new request types or fields are
73//! added to the API and you don't feel like waiting for them to be added to this library)
74//!
75//! [`Request`]: crate::data::Request
76//! [`Response`]: crate::data::Response
77//! [`RequestEnvelope`]: crate::data::RequestEnvelope
78//! [`ResponseEnvelope`]: crate::data::ResponseEnvelope
79//!
80//! # Optional features
81//!
82//! By default, the `tokio-tungstenite` feature is enabled, which includes helpers related to the
83//! [`tokio_tungstenite`] websocket library. This can be disabled in your `Cargo.toml` with
84//! `default-features = false`:
85//!
86//! ```toml
87//! [dependencies]
88#![doc = concat!("vtubestudio = { version = \"", env!("CARGO_PKG_VERSION"), "\", default-features = false }")]
89//! ```
90
91/// Utilities for creating [`Client`]s.
92pub mod client;
93
94/// [`Service`](tower::Service) middleware used by [`Client`].
95pub mod service;
96
97/// Transport ([`Sink`]/[`Stream`]) types.
98///
99/// [`Sink`]: futures_sink::Sink
100/// [`Stream`]: futures_util::Stream
101pub mod transport;
102
103/// Codecs for converting to/from websocket message types.
104pub mod codec;
105
106pub mod data;
107
108/// Types related to error handling.
109pub mod error;
110
111// Macro for enabling `doc_cfg` on docs.rs
112macro_rules! cfg_feature {
113 (
114 #![$meta:meta]
115 $($item:item)+
116 ) => {
117 $(
118 #[cfg($meta)]
119 #[cfg_attr(docsrs, doc(cfg($meta)))]
120 $item
121 )*
122 }
123}
124
125pub(crate) use cfg_feature;
126
127pub use crate::client::{Client, ClientBuilder, ClientEvent, ClientEventStream};
128pub use crate::error::{Error, ErrorKind, Result};
129
130#[cfg(doctest)]
131#[cfg_attr(feature = "tokio-tungstenite", doc = include_str!("../README.md"))]
132pub struct ReadmeDoctests;