medea_control_api_proto/grpc/convert/
mod.rs

1//! Conversions between crate types and the ones generated from [gRPC] specs.
2//!
3//! [gRPC]: https://grpc.io
4
5mod api;
6mod callback;
7
8use derive_more::with_trait::{Display, Error, From};
9
10use super::CallbackUrlParseError;
11use crate::{
12    control::ParseFidError, endpoint::web_rtc_play::LocalSrcUriParseError,
13};
14
15/// Possible errors of deserializing types from [gRPC] spec.
16///
17/// [gRPC]: https://grpc.io
18#[derive(Debug, Display, Error, From)]
19pub enum ProtobufError {
20    /// Error of parsing a [`LocalSrcUri`] of a [`WebRtcPlay`] media
21    /// [`Element`].
22    ///
23    /// [`Element`]: crate::Element
24    /// [`LocalSrcUri`]: crate::endpoint::web_rtc_play::LocalSrcUri
25    /// [`WebRtcPlay`]: crate::endpoint::WebRtcPlay
26    #[display("Source URI parse error: {_0}")]
27    LocalSrcUriParseErr(LocalSrcUriParseError),
28
29    /// [`Element`] is expected to be of another type.
30    ///
31    /// [`Element`]: crate::Element
32    #[display("`{_1}` media element expected to be of type `{_0}`")]
33    #[from(ignore)]
34    ExpectedElement(&'static str, Box<str>),
35
36    /// [`Element`] is expected to be specified.
37    ///
38    /// [`Element`]: crate::Element
39    #[display("Expected media element, but none specified")]
40    NoElement,
41
42    /// [`Element`] is expected to be specified for a [`Fid`].
43    ///
44    /// [`Element`]: crate::Element
45    /// [`Fid`]: crate::Fid
46    #[display("Expected media element for `{_0}`, but none specified")]
47    #[from(ignore)]
48    NoElementForId(#[error(not(source))] Box<str>),
49
50    /// Error of parsing a [`CallbackUrl`].
51    ///
52    /// [`CallbackUrl`]: super::CallbackUrl
53    #[display("gRPC callback URL parse error: {_0}")]
54    CallbackUrlParseErr(CallbackUrlParseError),
55
56    /// Some [`Element`] specifies invalid [`Duration`].
57    ///
58    /// [`Duration`]: std::time::Duration
59    /// [`Element`]: crate::Element
60    #[display(
61        "`Element(id: {_0})` specifies field `{_1}` with invalid duration"
62    )]
63    #[from(ignore)]
64    InvalidDuration(Box<str>, &'static str),
65
66    /// Error of parsing a [`Fid`].
67    ///
68    /// [`Fid`]: crate::Fid
69    #[display("FID parse error: {_0}")]
70    ParseFidErr(ParseFidError),
71
72    /// Error of parsing a [`DateTime`].
73    ///
74    /// [`DateTime`]: time::OffsetDateTime
75    #[display("`DateTime` parse error: {_0}")]
76    TimeParseErr(time::error::Parse),
77
78    /// Such API call is unimplemented.
79    #[display("API call is unimplemented")]
80    Unimplemented,
81}
82
83impl From<ProtobufError> for tonic::Status {
84    fn from(err: ProtobufError) -> Self {
85        match &err {
86            ProtobufError::LocalSrcUriParseErr(_)
87            | ProtobufError::ExpectedElement(_, _)
88            | ProtobufError::NoElement
89            | ProtobufError::NoElementForId(_)
90            | ProtobufError::CallbackUrlParseErr(_)
91            | ProtobufError::InvalidDuration(_, _)
92            | ProtobufError::ParseFidErr(_)
93            | ProtobufError::TimeParseErr(_) => {
94                Self::invalid_argument(err.to_string())
95            }
96            ProtobufError::Unimplemented => {
97                Self::unimplemented(err.to_string())
98            }
99        }
100    }
101}