-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This does not implement accessors, just the enum definitions themselves. PiperOrigin-RevId: 591039910
- Loading branch information
1 parent
f75fe9e
commit f08a836
Showing
16 changed files
with
990 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2023 Google LLC. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
|
||
use crate::__internal::Private; | ||
use std::{ | ||
error::Error, | ||
fmt::{Debug, Display}, | ||
marker::PhantomData, | ||
}; | ||
|
||
/// Implemented by all generated enum types. | ||
/// | ||
/// # Safety | ||
/// - A `RepeatedView<Self>` or `RepeatedMut<Self>` must have the same internal | ||
/// representation as erased enums in the runtime. | ||
/// - For C++, this is `proto2::RepeatedField<c_int>` | ||
/// - For UPB, this is an array compatible with `int32` | ||
pub unsafe trait Enum { | ||
/// The name of the enum. | ||
const NAME: &'static str; | ||
} | ||
|
||
/// An integer value wasn't known for an enum while converting. | ||
pub struct UnknownEnumValue<T>(i32, PhantomData<T>); | ||
|
||
impl<T> UnknownEnumValue<T> { | ||
#[doc(hidden)] | ||
pub fn new(_private: Private, unknown_value: i32) -> Self { | ||
Self(unknown_value, PhantomData) | ||
} | ||
} | ||
|
||
impl<T> Debug for UnknownEnumValue<T> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_tuple("UnknownEnumValue").field(&self.0).finish() | ||
} | ||
} | ||
|
||
impl<T: Enum> Display for UnknownEnumValue<T> { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let val = self.0; | ||
let enum_name = T::NAME; | ||
write!(f, "{val} is not a known value for {enum_name}") | ||
} | ||
} | ||
|
||
impl<T: Enum> Error for UnknownEnumValue<T> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Protocol Buffers - Google's data interchange format | ||
// Copyright 2023 Google LLC. All rights reserved. | ||
// | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file or at | ||
// https://developers.google.com/open-source/licenses/bsd | ||
// LINT: LEGACY_NAMES | ||
|
||
// The names in this file are meant to test edge cases. | ||
syntax = "proto3"; | ||
|
||
package enums; | ||
|
||
import "devtools/staticanalysis/pipeline/analyzers/proto_best_practices/proto/optouts.proto"; | ||
|
||
option (proto_best_practices.file_optouts) = { | ||
categories: ENUM_DEFAULT_VALUE_NAME_CONFLICT | ||
categories: ENUM_VALUE_NAMES | ||
}; | ||
|
||
// This should result in an enum with these accessible values: | ||
// - Unknown = 0 | ||
// - Unrecognized = 0 | ||
// - Foo = 1 | ||
// - Bar = 2 | ||
// - DifferentNameAlias = 2 | ||
enum TestEnumWithDuplicateStrippedPrefixNames { | ||
option allow_alias = true; | ||
|
||
UNKNOWN = 0; | ||
TestEnumWithDuplicateStrippedPrefixNamesUNRECOGNIZED = 0; | ||
|
||
TestEnumWithDuplicateStrippedPrefixNames_FOO = 1; | ||
TEST_ENUM_WITH_DUPLICATE_STRIPPED_PREFIX_NAMES_FOO = 1; | ||
FOO = 1; | ||
|
||
TestEnumWithDuplicateStrippedPrefixNamesBAR = 2; | ||
TEST_ENUM_WITH_DUPLICATE_STRIPPED_PREFIX_NAMESBar = 2; | ||
BAR = 2; | ||
TEST_ENUM_WITH_DUPLICATE_STRIPPED_PREFIX_NAMES_DIFFERENT_NAME_ALIAS = 2; | ||
} | ||
|
||
// This should result in an enum with these accessible values: | ||
// - Unknown = 0 | ||
// - _2020 = 1 | ||
// - _2021 = 2 | ||
// - _2022 = 3 | ||
enum TestEnumWithNumericNames { | ||
TestEnumWithNumericNamesUNKNOWN = 0; | ||
TestEnumWithNumericNames_2020 = 1; | ||
TEST_ENUM_WITH_NUMERIC_NAMES_2021 = 2; | ||
TEST_ENUM_WITH_NUMERIC_NAMES2022 = 3; | ||
} | ||
|
||
// This should result in an enum with these accessible values: | ||
// - Unknown = 0 | ||
// - TestEnumValueNameSameAsEnum = 1 | ||
enum TestEnumValueNameSameAsEnum { | ||
TEST_ENUM_VALUE_NAME_SAME_AS_ENUM_UNKNOWN = 0; | ||
TEST_ENUM_VALUE_NAME_SAME_AS_ENUM = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.