# Path: proto/marshal/rules/enums.py
# class EnumRule:
#     """A marshal for converting between integer values and enum values."""
# 
#     def __init__(self, enum_class: Type[enum.IntEnum]):
#         self._enum = enum_class
# 
#     def to_python(self, value, *, absent: bool = None):
#         if isinstance(value, int) and not isinstance(value, self._enum):
#             try:
#                 # Coerce the int on the wire to the enum value.
#                 return self._enum(value)
#             except ValueError:
#                 # Since it is possible to add values to enums, we do
#                 # not want to flatly error on this.
#                 #
#                 # However, it is useful to make some noise about it so
#                 # the user realizes that an unexpected value came along.
#                 warnings.warn(
#                     "Unrecognized {name} enum value: {value}".format(
#                         name=self._enum.__name__, value=value,
#                     )
#                 )
#         return value
# 
#     def to_proto(self, value):
#         # Accept enum values and coerce to the pure integer.
#         # This is not strictly necessary (protocol buffers can take these
#         # objects as they subclass int) but nevertheless seems like the
#         # right thing to do.
#         if isinstance(value, self._enum):
#             return value.value
# 
#         # If a string is provided that matches an enum value, coerce it
#         # to the enum value.
#         if isinstance(value, str):
#             return self._enum[value].value
# 
#         # We got a pure integer; pass it on.
#         return value


from unittest import mock
from proto.marshal.rules.enums import EnumRule
import warnings
import proto
# 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.



__protobuf__ = proto.module(package="test.marshal.enum")


def test_to_proto():
    class Foo(proto.Enum):
        FOO_UNSPECIFIED = 0
        BAR = 1
        BAZ = 2

    enum_rule = EnumRule(Foo)
    foo_a = enum_rule.to_proto(Foo.BAR)
    foo_b = enum_rule.to_proto(1)
    foo_c = enum_rule.to_proto("BAR")
    # We want to distinguish literal `1` from `Foo.BAR` here
    # (they are equivalent but not identical).
    assert foo_a is foo_b is foo_c is 1  # noqa: F632


def test_to_python():
    class Foo(proto.Enum):
        FOO_UNSPECIFIED = 0
        BAR = 1
        BAZ = 2


# Path: proto/marshal/rules/enums.py
# class EnumRule:
#     """A marshal for converting between integer values and enum values."""
# 
#     def __init__(self, enum_class: Type[enum.IntEnum]):
#         self._enum = enum_class
# 
#     def to_python(self, value, *, absent: bool = None):
#         if isinstance(value, int) and not isinstance(value, self._enum):
#             try:
#                 # Coerce the int on the wire to the enum value.
#                 return self._enum(value)
#             except ValueError:
#                 # Since it is possible to add values to enums, we do
#                 # not want to flatly error on this.
#                 #
#                 # However, it is useful to make some noise about it so
#                 # the user realizes that an unexpected value came along.
#                 warnings.warn(
#                     "Unrecognized {name} enum value: {value}".format(
#                         name=self._enum.__name__, value=value,
#                     )
#                 )
#         return value
# 
#     def to_proto(self, value):
#         # Accept enum values and coerce to the pure integer.
#         # This is not strictly necessary (protocol buffers can take these
#         # objects as they subclass int) but nevertheless seems like the
#         # right thing to do.
#         if isinstance(value, self._enum):
#             return value.value
# 
#         # If a string is provided that matches an enum value, coerce it
#         # to the enum value.
#         if isinstance(value, str):
#             return self._enum[value].value
# 
#         # We got a pure integer; pass it on.
#         return value


# Path: tests/test_marshal_types_enum.py
from unittest import mock
from proto.marshal.rules.enums import EnumRule
import warnings
import proto

# 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.



__protobuf__ = proto.module(package="test.marshal.enum")


def test_to_proto():
    class Foo(proto.Enum):
        FOO_UNSPECIFIED = 0
        BAR = 1
        BAZ = 2

    enum_rule = EnumRule(Foo)
    foo_a = enum_rule.to_proto(Foo.BAR)
    foo_b = enum_rule.to_proto(1)
    foo_c = enum_rule.to_proto("BAR")
    # We want to distinguish literal `1` from `Foo.BAR` here
    # (they are equivalent but not identical).
    assert foo_a is foo_b is foo_c is 1  # noqa: F632


def test_to_python():
    class Foo(proto.Enum):
        FOO_UNSPECIFIED = 0
        BAR = 1
        BAZ = 2


    enum_rule = EnumRule(Foo)