Debian Codegraph @

src/encoding/error.rs

raw symbols SCIP
1 #[cfg(feature = "std")]
2 use std::sync::Arc;
3
4 use thiserror::Error;
5
6 use crate::state_tracker;
7
8 /// An enumeration of potential errors that appear during bencode encoding.
9 #[derive(Debug, Clone, Error)]
10 #[non_exhaustive]
11 pub enum Error {
12 /// Error that occurs if the serialized structure contains invalid semantics.
13 #[cfg(feature = "std")]
14 #[error("malformed content discovered: {source}")]
15 MalformedContent {
16 source: Arc<dyn std::error::Error + Send + Sync>,
17 },
18
19 /// Error that occurs if the serialized structure contains invalid semantics.
20 #[cfg(not(feature = "std"))]
21 #[error("malformed content discovered")]
22 MalformedContent,
23
24 /// Error in the bencode structure (e.g. a missing field end separator).
25 #[error("bencode encoding corrupted")]
26 StructureError {
27 source: state_tracker::StructureError,
28 },
29 }
30
31 impl Error {
32 /// Raised when there is a general error while deserializing a type.
33 /// The message should not be capitalized and should not end with a period.
34 ///
35 /// Note that, when building with no_std, this method accepts any type as
36 /// its argument.
37 #[cfg(feature = "std")]
38 pub fn malformed_content<SourceT>(source: SourceT) -> Self
39 where
40 SourceT: std::error::Error + Send + Sync + 'static,
41 {
42 let error = Arc::new(source);
43 Error::MalformedContent { source: error }
44 }
45
46 #[cfg(not(feature = "std"))]
47 pub fn malformed_content<T>(_cause: T) -> Self {
48 Error::MalformedContent
49 }
50 }
51
52 impl From<state_tracker::StructureError> for Error {
53 fn from(error: state_tracker::StructureError) -> Self {
54 Error::StructureError { source: error }
55 }
56 }
57
58 #[test]
59 fn encoding_errors_are_sync_send() {
60 use crate::encoding::error::Error;
61 fn is_send<T: Send>() {}
62 fn is_sync<T: Sync>() {}
63 is_send::<Error>();
64 is_sync::<Error>();
65 }