crux_cli/codegen/serde_generate/
error.rs1#![allow(dead_code)]
5
6use serde::{de, ser};
7use std::fmt;
8use thiserror::Error;
9
10use super::format::ContainerFormat;
11
12pub type Result<T, E = Error> = std::result::Result<T, E>;
14
15#[derive(Clone, Debug, Error, PartialEq)]
17pub enum Error {
18 #[error("{0}")]
19 Custom(String),
20 #[error("Not supported: {0}")]
21 NotSupported(&'static str),
22 #[error("Failed to deserialize {0}")]
23 Deserialization(&'static str),
24 #[error("In container {0}, recorded value for serialization format {1:?} failed to deserialize into {2}")]
25 UnexpectedDeserializationFormat(&'static str, ContainerFormat, &'static str),
26 #[error("Incompatible formats detected: {0} {1}")]
27 Incompatible(String, String),
28 #[error("Incomplete tracing detected")]
29 UnknownFormat,
30 #[error("Incomplete tracing detected inside container: {0}")]
31 UnknownFormatInContainer(String),
32 #[error("Missing variants detected for specific enums: {0:?}")]
33 MissingVariants(Vec<String>),
34}
35
36impl ser::Error for Error {
37 fn custom<T: fmt::Display>(msg: T) -> Self {
38 Error::Custom(format!("Failed to serialize value: \"{}\"", msg))
39 }
40}
41
42impl de::Error for Error {
43 fn custom<T: fmt::Display>(msg: T) -> Self {
44 Error::Custom(format!("Failed to deserialize value: \"{}\"", msg))
45 }
46}
47
48impl Error {
49 pub fn explanation(&self) -> String {
51 use Error::*;
52
53 match self {
54 Custom(_) => {
55 r#"
56An error was returned by a Serde trait during (de)serialization tracing. In practice, this happens when
57user-provided code 'impl<'de> Deserialize<'de> for Foo { .. }' rejects a candidate value of type `Foo`
58provided by serde-reflection.
59
60To fix this, add a call `tracer.trace_value(foo, &mut samples)` so that a correct value `foo` is
61recorded *before* `tracer.trace_type` is called.
62"#.to_string()
63 }
64 NotSupported(_) => {
65 r#"
66An unsupported callback was called during (de)serialization tracing. In practice, this happens when an
67unsupported Serde attribute is used. Attributes specific to self-describing formats (JSON, YAML, TOML)
68are generally not supported. This includes: `#[serde(flatten)]`, `#[serde(tag = "type")]`,
69`#[serde(tag = "t", content = "c")]`, and `#[serde(untagged)]`.
70
71To fix this, avoid unsupported Serde attributes or use custom (de)serialize implementations with different
72behaviors depending on the Serde callback `(De)Serializer::is_human_readable()`.
73"#.to_string()
74 }
75 Deserialization(_) => {
76 r#"
77This internal error should not be surfaced during tracing.
78"#.to_string()
79 }
80 UnexpectedDeserializationFormat(_, _, _) => {
81 r#"
82A value recorded by `trace_value` fails to deserialize as expected during a `trace_type` call. This can
83happen if custom implementations of the Serialize and Deserialize traits do not agree.
84
85Verify the implementations of Serialize and Deserialize for the given format.
86"#.to_string()
87 }
88
89 Incompatible(_, _) => {
90 r#"
91Two formats computed for the same entity do not match. This can happen if custom implementations of
92the Serialize and Deserialize traits do not agree, e.g. if one uses `Vec<u8>` and the other one uses `&[u8]`
93(implying bytes) --- see the crate `serde_bytes` for more context in this particular example.
94
95Verify the implementations of Serialize and Deserialize for the given format.
96"#.to_string()
97 }
98 UnknownFormat => {
99 r#"
100This internal error is returned should not be surfaced during tracing.
101"#.to_string()
102 }
103 UnknownFormatInContainer(name) => {
104 format!(r#"
105A final registry was requested but some formats are still unknown within the container {}. This can
106happen if `tracer.trace_value` was called on a value `foo` which does not reveal some of the underlying
107formats. E.g. if a field `x` of struct `Foo` has type `Option<String>` and `foo` is value of type
108`Foo` such that `foo.x == None`, then tracing the value `foo` may result in a format `Option<Unknown>`
109for the field `x`. The same applies to empty vectors and empty maps.
110
111To fix this, avoid `trace_value` and prefer `trace_type` when possible, or make sure to trace at
112least one value `foo` such that `foo.x` is not empty.
113"#,
114 name)
115 }
116 MissingVariants(names) => {
117 format!(r#"
118A registry was requested with `tracer.registry()` but some variants have not been analyzed yet
119inside the given enums {:?}.
120
121To fix this, make sure to call `tracer.trace_type<T>(..)` at least once for each enum type `T` in the
122corpus of definitions. You may also use `tracer.registry_unchecked()` for debugging.
123"#,
124 names)
125 }
126 }
127 }
128}