Coverage Report

Created: 2022-07-04 16:17

src/requests/mod.rs
Line
Count
Source (jump to first uncovered line)
1
//! All requests that can be send to the API.
2
3
use bitflags::bitflags;
4
use serde::{ser::SerializeStruct, Serialize};
5
use serde_repr::Serialize_repr;
6
use serde_with::skip_serializing_none;
7
8
pub mod config;
9
pub mod custom;
10
pub mod filters;
11
pub mod general;
12
pub mod hotkeys;
13
pub mod inputs;
14
pub(crate) mod media_inputs;
15
pub(crate) mod outputs;
16
pub mod profiles;
17
pub(crate) mod recording;
18
pub(crate) mod replay_buffer;
19
pub(crate) mod scene_collections;
20
pub mod scene_items;
21
pub mod scenes;
22
pub mod sources;
23
pub(crate) mod streaming;
24
pub(crate) mod transitions;
25
pub mod ui;
26
pub(crate) mod virtual_cam;
27
28
pub(crate) enum ClientRequest<'a> {
29
    /// Response to [`crate::responses::ServerMessage::Hello`] message, should contain
30
    /// authentication string if authentication is required, along with Pub-sub subscriptions and
31
    /// other session parameters.
32
    Identify(Identify),
33
    /// Sent at any time after initial identification to update the provided session parameters.
34
    Reidentify(Reidentify),
35
    /// Client is making a request to obs-websocket. For example get current scene, create source.
36
    Request(Request<'a>),
37
    /// Client is making a batch of requests for obs-websocket. Requests are processed serially
38
    /// (in order) by the server.
39
    #[allow(dead_code)]
40
    RequestBatch(RequestBatch<'a>),
41
}
42
43
impl<'a> Serialize for ClientRequest<'a> {
44
364
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45
364
    where
46
364
        S: serde::Serializer,
47
364
    {
48
364
        #[derive(Serialize_repr)]
<<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::OpCode as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Line
Count
Source
48
364
        #[derive(Serialize_repr)]
Unexecuted instantiation: <<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::OpCode as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::OpCode as serde::ser::Serialize>::serialize::<_>
49
364
        #[repr(u8)]
50
364
        enum OpCode {
51
364
            /// The message sent by a newly connected client to obs-websocket in response to a
52
364
            /// `Hello`.
53
364
            Identify = 1,
54
364
            /// The message sent by an already-identified client to update identification
55
364
            /// parameters.
56
364
            Reidentify = 3,
57
364
            /// The message sent by a client to obs-websocket to perform a request.
58
364
            Request = 6,
59
364
            /// The message sent by a client to obs-websocket to perform a batch of requests.
60
364
            RequestBatch = 8,
61
364
        }
62
364
63
364
        fn write_state<S>(serializer: S, op: OpCode, d: &impl Serialize) -> Result<S::Ok, S::Error>
64
364
        where
65
364
            S: serde::Serializer,
66
364
        {
67
364
            let mut state = serializer.serialize_struct("ClientRequest", 2)
?0
;
68
364
            state.serialize_field("op", &op)
?0
;
69
364
            state.serialize_field("d", d)
?0
;
70
364
            state.end()
71
364
        }
<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>, obws::requests::Identify>
Line
Count
Source
63
18
        fn write_state<S>(serializer: S, op: OpCode, d: &impl Serialize) -> Result<S::Ok, S::Error>
64
18
        where
65
18
            S: serde::Serializer,
66
18
        {
67
18
            let mut state = serializer.serialize_struct("ClientRequest", 2)
?0
;
68
18
            state.serialize_field("op", &op)
?0
;
69
18
            state.serialize_field("d", d)
?0
;
70
18
            state.end()
71
18
        }
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>, obws::requests::Reidentify>
<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>, obws::requests::Request>
Line
Count
Source
63
346
        fn write_state<S>(serializer: S, op: OpCode, d: &impl Serialize) -> Result<S::Ok, S::Error>
64
346
        where
65
346
            S: serde::Serializer,
66
346
        {
67
346
            let mut state = serializer.serialize_struct("ClientRequest", 2)
?0
;
68
346
            state.serialize_field("op", &op)
?0
;
69
346
            state.serialize_field("d", d)
?0
;
70
346
            state.end()
71
346
        }
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>, obws::requests::RequestBatch>
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<_, _>
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::write_state::<_, _>
72
364
73
364
        match self {
74
18
            Self::Identify(value) => write_state(serializer, OpCode::Identify, value),
75
0
            Self::Reidentify(value) => write_state(serializer, OpCode::Reidentify, value),
76
346
            Self::Request(value) => write_state(serializer, OpCode::Request, value),
77
0
            Self::RequestBatch(value) => write_state(serializer, OpCode::RequestBatch, value),
78
        }
79
364
    }
<obws::requests::ClientRequest as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Line
Count
Source
44
364
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
45
364
    where
46
364
        S: serde::Serializer,
47
364
    {
48
364
        #[derive(Serialize_repr)]
49
364
        #[repr(u8)]
50
364
        enum OpCode {
51
364
            /// The message sent by a newly connected client to obs-websocket in response to a
52
364
            /// `Hello`.
53
364
            Identify = 1,
54
364
            /// The message sent by an already-identified client to update identification
55
364
            /// parameters.
56
364
            Reidentify = 3,
57
364
            /// The message sent by a client to obs-websocket to perform a request.
58
364
            Request = 6,
59
364
            /// The message sent by a client to obs-websocket to perform a batch of requests.
60
364
            RequestBatch = 8,
61
364
        }
62
364
63
364
        fn write_state<S>(serializer: S, op: OpCode, d: &impl Serialize) -> Result<S::Ok, S::Error>
64
364
        where
65
364
            S: serde::Serializer,
66
364
        {
67
364
            let mut state = serializer.serialize_struct("ClientRequest", 2)?;
68
364
            state.serialize_field("op", &op)?;
69
364
            state.serialize_field("d", d)?;
70
364
            state.end()
71
364
        }
72
364
73
364
        match self {
74
18
            Self::Identify(value) => write_state(serializer, OpCode::Identify, value),
75
0
            Self::Reidentify(value) => write_state(serializer, OpCode::Reidentify, value),
76
346
            Self::Request(value) => write_state(serializer, OpCode::Request, value),
77
0
            Self::RequestBatch(value) => write_state(serializer, OpCode::RequestBatch, value),
78
        }
79
364
    }
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::ClientRequest as serde::ser::Serialize>::serialize::<_>
80
}
81
82
/// Response to [`crate::responses::ServerMessage::Hello`] message, should contain
83
/// authentication string if authentication is required, along with Pub-sub subscriptions and other
84
/// session parameters.
85
#[skip_serializing_none]
86
18
#[derive(Serialize)]
<obws::requests::Identify as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Line
Count
Source
86
18
#[derive(Serialize)]
Unexecuted instantiation: <obws::requests::Identify as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::Identify as serde::ser::Serialize>::serialize::<_>
87
pub(crate) struct Identify {
88
    /// Version number that the client would like the obs-websocket server to use.
89
    #[serde(rename = "rpcVersion")]
90
    pub rpc_version: u32,
91
    #[serde(rename = "authentication")]
92
    pub authentication: Option<String>,
93
    /// Bit mask of event subscription items to subscribe to events and event categories at will. By
94
    /// default, all event categories are subscribed, except for events marked as high volume. High
95
    /// volume events must be explicitly subscribed to.
96
    #[serde(rename = "eventSubscriptions")]
97
    pub event_subscriptions: Option<EventSubscription>,
98
}
99
100
/// Sent at any time after initial identification to update the provided session parameters.
101
#[skip_serializing_none]
102
0
#[derive(Serialize)]
Unexecuted instantiation: <obws::requests::Reidentify as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Unexecuted instantiation: <obws::requests::Reidentify as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::Reidentify as serde::ser::Serialize>::serialize::<_>
103
pub(crate) struct Reidentify {
104
    #[serde(rename = "eventSubscriptions")]
105
    pub event_subscriptions: Option<EventSubscription>,
106
}
107
108
/// Client is making a request to obs-websocket. For example get current scene, create source.
109
346
#[derive(Serialize)]
<obws::requests::Request as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Line
Count
Source
109
346
#[derive(Serialize)]
Unexecuted instantiation: <obws::requests::Request as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::Request as serde::ser::Serialize>::serialize::<_>
110
pub(crate) struct Request<'a> {
111
    #[serde(rename = "requestId")]
112
    pub request_id: &'a str,
113
    #[serde(flatten)]
114
    pub ty: RequestType<'a>,
115
}
116
117
/// Client is making a batch of requests for obs-websocket. Requests are processed serially
118
/// (in order) by the server.
119
#[skip_serializing_none]
120
0
#[derive(Serialize)]
Unexecuted instantiation: <obws::requests::RequestBatch as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Unexecuted instantiation: <obws::requests::RequestBatch as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::RequestBatch as serde::ser::Serialize>::serialize::<_>
121
pub(crate) struct RequestBatch<'a> {
122
    #[serde(rename = "requestId")]
123
    pub request_id: &'a str,
124
    /// When true, the processing of requests will be halted on first failure. Returns only the
125
    /// processed requests in
126
    /// [`crate::responses::ServerMessage::RequestBatchResponse`].
127
    #[serde(rename = "haltOnFailure")]
128
    pub halt_on_failure: Option<bool>,
129
    #[serde(rename = "requests")]
130
    pub requests: &'a [RequestType<'a>],
131
    #[serde(rename = "executionType")]
132
    pub execution_type: Option<ExecutionType>,
133
}
134
135
bitflags! {
136
    /// Bit flags for possible event subscriptions, that can be enabled when connecting to the OBS
137
    /// instance.
138
0
    #[derive(Serialize)]
Unexecuted instantiation: <obws::requests::EventSubscription as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Unexecuted instantiation: <obws::requests::EventSubscription as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::EventSubscription as serde::ser::Serialize>::serialize::<_>
139
    #[serde(transparent)]
140
    pub struct EventSubscription: u32 {
141
        /// Subscription value used to disable all events.
142
        const NONE = 0;
143
        /// Subscription value to receive events in the `General` category.
144
        const GENERAL = 1 << 0;
145
        /// Subscription value to receive events in the `Config` category.
146
        const CONFIG = 1 << 1;
147
        /// Subscription value to receive events in the `Scenes` category.
148
        const SCENES = 1 << 2;
149
        /// Subscription value to receive events in the `Inputs` category.
150
        const INPUTS = 1 << 3;
151
        /// Subscription value to receive events in the `Transitions` category.
152
        const TRANSITIONS = 1 << 4;
153
        /// Subscription value to receive events in the `Filters` category.
154
        const FILTERS = 1 << 5;
155
        /// Subscription value to receive events in the `Outputs` category.
156
        const OUTPUTS = 1 << 6;
157
        /// Subscription value to receive events in the `SceneItems` category.
158
        const SCENE_ITEMS = 1 << 7;
159
        /// Subscription value to receive events in the `MediaInputs` category.
160
        const MEDIA_INPUTS = 1 << 8;
161
        /// Subscription value to receive the [`VendorEvent`] event.
162
        ///
163
        /// [`VendorEvent`]: crate::events::Event::VendorEvent
164
        const VENDORS = 1 << 9;
165
        /// Subscription value to receive events in the `Ui` category.
166
        const UI = 1 << 10;
167
168
        /// Helper to receive all non-high-volume events.
169
        const ALL = Self::GENERAL.bits
170
            | Self::CONFIG.bits
171
            | Self::SCENES.bits
172
            | Self::INPUTS.bits
173
            | Self::TRANSITIONS.bits
174
            | Self::FILTERS.bits
175
            | Self::OUTPUTS.bits
176
            | Self::SCENE_ITEMS.bits
177
            | Self::MEDIA_INPUTS.bits
178
            | Self::VENDORS.bits
179
            | Self::UI.bits;
180
181
        /// Subscription value to receive the [`InputVolumeMeters`] high-volume event.
182
        ///
183
        /// [`InputVolumeMeters`]: crate::events::Event::InputVolumeMeters
184
        const INPUT_VOLUME_METERS = 1 << 16;
185
        /// Subscription value to receive the [`InputActiveStateChanged`] high-volume event.
186
        ///
187
        /// [`InputActiveStateChanged`]: crate::events::Event::InputActiveStateChanged
188
        const INPUT_ACTIVE_STATE_CHANGED = 1 << 17;
189
        /// Subscription value to receive the [`InputShowStateChanged`] high-volume event.
190
        ///
191
        /// [`InputShowStateChanged`]: crate::events::Event::InputShowStateChanged
192
        const INPUT_SHOW_STATE_CHANGED = 1 << 18;
193
        /// Subscription value to receive the [`SceneItemTransformChanged`] high-volume event.
194
        ///
195
        /// [`SceneItemTransformChanged`]: crate::events::Event::SceneItemTransformChanged
196
        const SCENE_ITEM_TRANSFORM_CHANGED = 1 << 19;
197
198
    }
199
}
200
201
#[allow(dead_code)]
202
0
#[derive(Serialize_repr)]
Unexecuted instantiation: <obws::requests::ExecutionType as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Unexecuted instantiation: <obws::requests::ExecutionType as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::ExecutionType as serde::ser::Serialize>::serialize::<_>
203
#[repr(i8)]
204
pub(crate) enum ExecutionType {
205
    /// Not a request batch.
206
    None = -1,
207
    /// A request batch which processes all requests serially, as fast as possible.
208
    SerialRealtime = 0,
209
    /// A request batch type which processes all requests serially, in sync with the graphics
210
    /// thread. Designed to provide high accuracy for animations.
211
    SerialFrame = 1,
212
    /// A request batch type which processes all requests using all available threads in the thread
213
    /// pool.
214
    Parallel = 2,
215
}
216
217
pub(crate) enum RequestType<'a> {
218
    Config(self::config::Request<'a>),
219
    Filters(self::filters::Request<'a>),
220
    General(self::general::Request<'a>),
221
    Hotkeys(self::hotkeys::Request<'a>),
222
    Inputs(self::inputs::Request<'a>),
223
    MediaInputs(self::media_inputs::Request<'a>),
224
    Outputs(self::outputs::Request<'a>),
225
    Profiles(self::profiles::Request<'a>),
226
    Recording(self::recording::Request),
227
    ReplayBuffer(self::replay_buffer::Request),
228
    SceneCollections(self::scene_collections::Request<'a>),
229
    SceneItems(self::scene_items::Request<'a>),
230
    Scenes(self::scenes::Request<'a>),
231
    Sources(self::sources::Request<'a>),
232
    Streaming(self::streaming::Request<'a>),
233
    Transitions(self::transitions::Request<'a>),
234
    Ui(self::ui::Request<'a>),
235
    VirtualCam(self::virtual_cam::Request),
236
}
237
238
impl<'a> Serialize for RequestType<'a> {
239
346
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
240
346
    where
241
346
        S: serde::Serializer,
242
346
    {
243
346
        match self {
244
7
            Self::Config(req) => req.serialize(serializer),
245
29
            Self::Filters(req) => req.serialize(serializer),
246
21
            Self::General(req) => req.serialize(serializer),
247
3
            Self::Hotkeys(req) => req.serialize(serializer),
248
38
            Self::Inputs(req) => req.serialize(serializer),
249
4
            Self::MediaInputs(req) => req.serialize(serializer),
250
8
            Self::Outputs(req) => req.serialize(serializer),
251
27
            Self::Profiles(req) => req.serialize(serializer),
252
27
            Self::Recording(req) => req.serialize(serializer),
253
25
            Self::ReplayBuffer(req) => req.serialize(serializer),
254
4
            Self::SceneCollections(req) => req.serialize(serializer),
255
20
            Self::SceneItems(req) => req.serialize(serializer),
256
69
            Self::Scenes(req) => req.serialize(serializer),
257
3
            Self::Sources(req) => req.serialize(serializer),
258
1
            Self::Streaming(req) => req.serialize(serializer),
259
9
            Self::Transitions(req) => req.serialize(serializer),
260
28
            Self::Ui(req) => req.serialize(serializer),
261
23
            Self::VirtualCam(req) => req.serialize(serializer),
262
        }
263
346
    }
<obws::requests::RequestType as serde::ser::Serialize>::serialize::<serde::__private::ser::FlatMapSerializer<serde_json::ser::Compound<&mut alloc::vec::Vec<u8>, serde_json::ser::CompactFormatter>>>
Line
Count
Source
239
346
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
240
346
    where
241
346
        S: serde::Serializer,
242
346
    {
243
346
        match self {
244
7
            Self::Config(req) => req.serialize(serializer),
245
29
            Self::Filters(req) => req.serialize(serializer),
246
21
            Self::General(req) => req.serialize(serializer),
247
3
            Self::Hotkeys(req) => req.serialize(serializer),
248
38
            Self::Inputs(req) => req.serialize(serializer),
249
4
            Self::MediaInputs(req) => req.serialize(serializer),
250
8
            Self::Outputs(req) => req.serialize(serializer),
251
27
            Self::Profiles(req) => req.serialize(serializer),
252
27
            Self::Recording(req) => req.serialize(serializer),
253
25
            Self::ReplayBuffer(req) => req.serialize(serializer),
254
4
            Self::SceneCollections(req) => req.serialize(serializer),
255
20
            Self::SceneItems(req) => req.serialize(serializer),
256
69
            Self::Scenes(req) => req.serialize(serializer),
257
3
            Self::Sources(req) => req.serialize(serializer),
258
1
            Self::Streaming(req) => req.serialize(serializer),
259
9
            Self::Transitions(req) => req.serialize(serializer),
260
28
            Self::Ui(req) => req.serialize(serializer),
261
23
            Self::VirtualCam(req) => req.serialize(serializer),
262
        }
263
346
    }
Unexecuted instantiation: <obws::requests::RequestType as serde::ser::Serialize>::serialize::<&mut serde_json::ser::Serializer<&mut alloc::vec::Vec<u8>>>
Unexecuted instantiation: <obws::requests::RequestType as serde::ser::Serialize>::serialize::<_>
Unexecuted instantiation: <obws::requests::RequestType as serde::ser::Serialize>::serialize::<_>
264
}