dscfg_proto/
lib.rs

1//! Protocol specification for dynamic shared configuration
2//!
3//! This crate contains types used in both client and server,
4//! so they aren't duplicated. This ensures consistency - if
5//! a change is made, it will immediately affect both client and the
6//! server.
7//!
8//! Features of this crate:
9//!
10//! * `client` - derives traits needed by the client
11//! * `server` - derives traits needed by the server
12//!
13//! The purpose of having those features is to not slow down
14//! compilation by compiling unneeded code as well as not increase
15//! the binary size unneccessarily.
16
17pub extern crate serde_json;
18extern crate serde;
19#[macro_use]
20extern crate serde_derive;
21
22/// Reexport for `serde_json`
23///
24/// This is mainly useful to avoid having to specify another dependency.
25/// It's also somewhat nicer to write `json::Value` instead of `serde_json::Value`.
26pub mod json {
27    pub use ::serde_json::*;
28}
29
30/// Request sent from client to server.
31///
32/// This enum represents possible requests accepted by the server.
33/// See the documentation of its variants to understand possible requests.
34///
35/// This enum is parametric over value type in order to skip conversions
36/// to `json::Value` when sending the request.
37#[cfg_attr(feature = "client", derive(Serialize))]
38#[cfg_attr(feature = "server", derive(Deserialize))]
39pub enum Request<Val = json::Value> {
40    /// Sets the value of `key` to `value`
41    ///
42    /// There's no response, but if the client is subscribed
43    /// with the `key`, it will get the notification.
44    Set { key: String, value: Val },
45
46    /// Gets the value of the `key`
47    ///
48    /// Response of type `Value` follows this request.
49    Get { key: String },
50
51    /// Requests notifications when any of the keys change.
52    ///
53    /// If `notify_now` is set to `true`, the client is
54    /// also notified immediately after the response.
55    ///
56    /// The response is either `OperationOk`, if the cliet was
57    /// subscribed or `Ignored`, if the client was already subscribed.
58    Subscribe { key: String, notify_now: bool },
59
60    /// Requests the server to stop notifying the client
61    ///
62    /// Note that this isn't necessary if the client is going to
63    /// disconnect - the subscribtions of the client are automatically
64    /// cleared on disconnect.
65    ///
66    /// If the unsubscribe operation was performed, `OperationOk`
67    /// response is sent. If the client wasn't subscribed,
68    /// `Ignored` is sent.
69    Unsubscribe { key: String },
70}
71
72/// Response or notification sent to the client.
73#[cfg_attr(feature = "server", derive(Serialize))]
74#[cfg_attr(feature = "client", derive(Deserialize))]
75pub enum Response<Val = json::Value> {
76    /// Informs the client about the value for certain key.
77    Value { key: String, value: Val },
78
79    /// Informs the client that the operation was performed.
80    OperationOk,
81
82    /// Informs the client that the operation failed.
83    OperationFailed,
84
85    /// Informs the client that the operation didn't have to be
86    /// performed.
87    ///
88    /// This happens if the client attempts to subscribe to key
89    /// he's already subscribed to, or unsubscribe from key he
90    /// isn't subscribed to.
91    Ignored,
92}