1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Facebook, Inc. and its affiliates
// SPDX-License-Identifier: MIT OR Apache-2.0

use serde::de::Visitor;
use thiserror::Error;

/// Compute the Serde name of a container.
pub fn trace_name<'de, T>() -> Option<&'static str>
where
    T: serde::de::Deserialize<'de>,
{
    match T::deserialize(NameTracer) {
        Err(NameTracerError(name)) => name,
        _ => unreachable!(),
    }
}

/// Minimal instrumented implementation of `serde::de::Deserializer`
/// This always returns a `NameTracerError` as soon as we have learnt the name
/// of the type (or the absence of name) from Serde.
struct NameTracer;

/// Custom error value used to report the result of the analysis.
#[derive(Clone, Debug, Error, PartialEq)]
#[error("{0:?}")]
struct NameTracerError(Option<&'static str>);

impl serde::de::Error for NameTracerError {
    fn custom<T: std::fmt::Display>(_msg: T) -> Self {
        unreachable!();
    }
}

macro_rules! declare_deserialize {
    ($method:ident) => {
        fn $method<V>(self, _visitor: V) -> std::result::Result<V::Value, NameTracerError>
        where
            V: Visitor<'de>,
        {
            Err(NameTracerError(None))
        }
    };
}

impl<'de> serde::de::Deserializer<'de> for NameTracer {
    type Error = NameTracerError;

    declare_deserialize!(deserialize_any);
    declare_deserialize!(deserialize_identifier);
    declare_deserialize!(deserialize_ignored_any);
    declare_deserialize!(deserialize_bool);
    declare_deserialize!(deserialize_i8);
    declare_deserialize!(deserialize_i16);
    declare_deserialize!(deserialize_i32);
    declare_deserialize!(deserialize_i64);
    declare_deserialize!(deserialize_i128);
    declare_deserialize!(deserialize_u8);
    declare_deserialize!(deserialize_u16);
    declare_deserialize!(deserialize_u32);
    declare_deserialize!(deserialize_u64);
    declare_deserialize!(deserialize_u128);
    declare_deserialize!(deserialize_f32);
    declare_deserialize!(deserialize_f64);
    declare_deserialize!(deserialize_char);
    declare_deserialize!(deserialize_str);
    declare_deserialize!(deserialize_string);
    declare_deserialize!(deserialize_bytes);
    declare_deserialize!(deserialize_byte_buf);
    declare_deserialize!(deserialize_option);
    declare_deserialize!(deserialize_unit);
    declare_deserialize!(deserialize_seq);
    declare_deserialize!(deserialize_map);

    fn deserialize_tuple<V>(
        self,
        _len: usize,
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(None))
    }

    fn deserialize_unit_struct<V>(
        self,
        name: &'static str,
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(Some(name)))
    }

    fn deserialize_newtype_struct<V>(
        self,
        name: &'static str,
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(Some(name)))
    }

    fn deserialize_tuple_struct<V>(
        self,
        name: &'static str,
        _len: usize,
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(Some(name)))
    }

    fn deserialize_struct<V>(
        self,
        name: &'static str,
        _fields: &'static [&'static str],
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(Some(name)))
    }

    fn deserialize_enum<V>(
        self,
        name: &'static str,
        _variants: &'static [&'static str],
        _visitor: V,
    ) -> std::result::Result<V::Value, NameTracerError>
    where
        V: Visitor<'de>,
    {
        Err(NameTracerError(Some(name)))
    }

    fn is_human_readable(&self) -> bool {
        false
    }
}