// Copyright (c) Facebook, Inc. and its affiliates // SPDX-License-Identifier: MIT OR Apache-2.0 use serde::{de::DeserializeOwned, Deserialize}; use serde_reflection::{Format, FormatHolder, Samples, Tracer, TracerConfig}; #[derive(Deserialize)] enum E { Unit, } #[derive(Deserialize)] struct Unit; #[derive(Deserialize)] struct NewType(u64); #[derive(Deserialize)] struct Tuple(u64, u32); #[derive(Deserialize)] #[serde(rename = "FooStruct")] #[allow(dead_code)] struct Struct { a: u64, } fn test_type(expected_name: &'static str) where T: DeserializeOwned, { // this crate assert_eq!(serde_name::trace_name::(), Some(expected_name)); // serde-reflection let mut tracer = Tracer::new(TracerConfig::default()); let samples = Samples::new(); let (mut ident, _samples) = tracer.trace_type::(&samples).unwrap(); ident.normalize().unwrap(); assert_eq!(ident, Format::TypeName(expected_name.into())); } #[test] fn test_serde_name_and_reflection() { test_type::("E"); test_type::("Unit"); test_type::("NewType"); test_type::("Tuple"); test_type::("FooStruct"); assert_eq!(serde_name::trace_name::(), None); assert_eq!(serde_name::trace_name::<(E, E)>(), None); }