Skip to main content

fbthrift_git/
metadata.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17pub trait ThriftAnnotations: 'static {
18    /// Returns the structured annotation `T` that is defined on this Thrift type, if the
19    /// annotation exists.
20    fn get_structured_annotation<T: Sized + 'static>() -> Option<T> {
21        None
22    }
23
24    /// Returns the structured annotation `T` that is defined on the given field with id `field_id`
25    /// on this Thrift type.
26    ///
27    /// Returns `None` if the field ID does not exist or if the given `T` does not exist as an
28    /// annotation on the given field.
29    ///
30    /// For example, for the given Thrift file:
31    /// ```thrift
32    /// struct FieldAnnotation {
33    ///     1: string payload;
34    /// }
35    ///
36    /// struct Foo {
37    ///     @FieldAnnotation{ payload = "hello world" }
38    ///     1: string username;
39    /// }
40    /// ```
41    ///
42    /// The call
43    /// ```ignore
44    /// <Foo as ThriftAnnotations>::get_field_structured_annotation::<FieldAnnotation>(1)
45    /// ```
46    /// will return
47    /// ```ignore
48    /// Some(FieldAnnotation {
49    ///     payload: "hello world".to_string(),
50    /// })
51    /// ```
52    fn get_field_structured_annotation<T: Sized + 'static>(_field_id: i16) -> Option<T> {
53        None
54    }
55}
56
57/// Identical to [ThriftAnnotations] but is implemented on the unit type (), for which
58/// `get_structured_annotation` and `get_field_structured_annotation` will always return `None`.
59///
60/// This allows a method to take `<T: MaybeThriftAnnotations>` to express the logic of "give me a
61/// privacy aware Thrift struct OR a 'void' type to indicate no type".
62pub trait MaybeThriftAnnotations: 'static {
63    fn get_structured_annotation<T: Sized + 'static>() -> Option<T> {
64        None
65    }
66
67    fn get_field_structured_annotation<T: Sized + 'static>(_field_id: i16) -> Option<T> {
68        None
69    }
70}
71
72impl<S: ThriftAnnotations> MaybeThriftAnnotations for S {
73    fn get_structured_annotation<T: Sized + 'static>() -> Option<T> {
74        <S as ThriftAnnotations>::get_structured_annotation::<T>()
75    }
76
77    fn get_field_structured_annotation<T: Sized + 'static>(field_id: i16) -> Option<T> {
78        <S as ThriftAnnotations>::get_field_structured_annotation::<T>(field_id)
79    }
80}
81
82impl MaybeThriftAnnotations for () {}