/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ pub trait ThriftAnnotations: 'static { /// Returns the structured annotation `T` that is defined on this Thrift type, if the /// annotation exists. fn get_structured_annotation() -> Option { None } /// Returns the structured annotation `T` that is defined on the given field with id `field_id` /// on this Thrift type. /// /// Returns `None` if the field ID does not exist or if the given `T` does not exist as an /// annotation on the given field. /// /// For example, for the given Thrift file: /// ```thrift /// struct FieldAnnotation { /// 1: string payload; /// } /// /// struct Foo { /// @FieldAnnotation{ payload = "hello world" } /// 1: string username; /// } /// ``` /// /// The call /// ```ignore /// ::get_field_structured_annotation::(1) /// ``` /// will return /// ```ignore /// Some(FieldAnnotation { /// payload: "hello world".to_string(), /// }) /// ``` fn get_field_structured_annotation(_field_id: i16) -> Option { None } } pub struct NoThriftAnnotations; impl ThriftAnnotations for NoThriftAnnotations { fn get_structured_annotation() -> ::std::option::Option { None } } /// Identical to [ThriftAnnotations] but is implemented on the unit type (), for which /// `get_structured_annotation` and `get_field_structured_annotation` will always return `None`. /// /// This allows a method to take `` to express the logic of "give me a /// privacy aware Thrift struct OR a 'void' type to indicate no type". pub trait MaybeThriftAnnotations: 'static { fn get_structured_annotation() -> Option { None } fn get_field_structured_annotation(_field_id: i16) -> Option { None } } impl MaybeThriftAnnotations for S { fn get_structured_annotation() -> Option { ::get_structured_annotation::() } fn get_field_structured_annotation(field_id: i16) -> Option { ::get_field_structured_annotation::(field_id) } } impl MaybeThriftAnnotations for () {}