blob: 6cb3489c4e9407dd2b33d665bd1b8947a36b231d [file] [log] [blame] [edit]
{{/*
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
*/}}
{{- define "EnumDeclaration" -}}
{{- range .DocComments }}
///{{ . }}
{{- end }}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
{{- if .IsStrict }}
#[repr({{ .UnderlyingType }})]
{{- else }}
#[non_exhaustive]
{{- end }}
pub enum {{ .Name }} {
{{- range .Members }}
{{- range .DocComments }}
///{{ . }}
{{- end }}
{{ .Name }}{{ if $.IsStrict }} = {{ .Value }}{{ end }},
{{- end }}
{{- if .IsFlexible }}
#[deprecated = "Use `{{ .Name }}::unknown()` to construct and `{{ .Name }}Unknown!()` to exhaustively match."]
#[doc(hidden)]
__Unknown({{ .UnderlyingType }}),
{{- end }}
}
{{- if .IsFlexible }}
/// Pattern that matches an unknown `{{ .Name }}` member.
#[macro_export]
macro_rules! {{ .Name }}Unknown {
() => { _ };
}
{{- end }}
impl {{ .Name }} {
#[inline]
pub fn from_primitive(prim: {{ .UnderlyingType }}) -> Option<Self> {
match prim {
{{- range .Members }}
{{ .Value }} => Some(Self::{{ .Name }}),
{{- end }}
_ => None,
}
}
{{ if .IsStrict }}
#[inline]
pub const fn into_primitive(self) -> {{ .UnderlyingType }} {
self as {{ .UnderlyingType }}
}
#[deprecated = "Strict enums should not use `is_unknown`"]
#[inline]
pub fn is_unknown(&self) -> bool {
false
}
{{- else }}
#[inline]
pub fn from_primitive_allow_unknown(prim: {{ .UnderlyingType }}) -> Self {
match prim {
{{- range .Members }}
{{ .Value }} => Self::{{ .Name }},
{{- end }}
#[allow(deprecated)]
x => Self::__Unknown(x),
}
}
#[inline]
pub fn unknown() -> Self {
#[allow(deprecated)]
Self::__Unknown({{ .UnknownValueForTmpl | printf "%#x" }})
}
#[inline]
pub const fn into_primitive(self) -> {{ .UnderlyingType }} {
match self {
{{- range .Members }}
Self::{{ .Name }} => {{ .Value }},
{{- end }}
#[allow(deprecated)]
Self::__Unknown(x) => x,
}
}
#[inline]
pub fn is_unknown(&self) -> bool {
match self {
#[allow(deprecated)]
Self::__Unknown(_) => true,
{{- if .Members }}
_ => false,
{{- end }}
}
}
{{- end }}
}
fidl_enum! {
name: {{ .Name }},
prim_ty: {{ .UnderlyingType }},
{{- if .IsStrict }}
strict: true,
min_member: {{ .MinMember }},
{{- else }}
flexible: true,
{{- end }}
}
{{ end }}