Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supports of conversion between string and enum #664

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions protobuf-codegen/src/gen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ impl<'a> EnumGen<'a> {
w.write_line("");
self.write_impl_default(w);
w.write_line("");
self.write_impl_display(w);
w.write_line("");
self.write_impl_self(w);
}

Expand Down Expand Up @@ -243,6 +245,28 @@ impl<'a> EnumGen<'a> {
);
}

fn write_impl_enum_fn_from_str(&self, w: &mut CodeWriter) {
w.def_fn(
&format!(
"from_str(str: &str) -> ::std::option::Option<{}>",
self.type_name
),
|w| {
w.match_expr("str", |w| {
let values = self.values_unique();
for value in values {
w.write_line(&format!(
"stringify!({}) => ::std::option::Option::Some({}),",
value.rust_name_inner(),
value.rust_name_outer()
));
}
w.write_line(&format!("_ => {}", EXPR_NONE));
});
},
);
}

fn write_impl_enum_const_values(&self, w: &mut CodeWriter) {
w.write_line(&format!("const VALUES: &'static [{}] = &[", self.type_name));
w.indented(|w| {
Expand All @@ -264,6 +288,8 @@ impl<'a> EnumGen<'a> {
w.write_line("");
self.write_impl_enum_fn_from_i32(w);
w.write_line("");
self.write_impl_enum_fn_from_str(w);
w.write_line("");
self.write_impl_enum_const_values(w);
},
);
Expand Down Expand Up @@ -410,4 +436,13 @@ impl<'a> EnumGen<'a> {
},
);
}

fn write_impl_display(&self, w: &mut CodeWriter) {
w.impl_for_block("::std::fmt::Display", &format!("{}", self.type_name), |w| {
w.def_fn(
"fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result",
|w| w.write_line(&format!("::std::fmt::Debug::fmt(self, f)")),
)
})
}
}
69 changes: 69 additions & 0 deletions protobuf/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,30 @@ pub mod field_descriptor_proto {
_ => ::std::option::Option::None
}
}

fn from_str(str: &str) -> ::std::option::Option<Type> {
match str {
stringify!(TYPE_DOUBLE) => ::std::option::Option::Some(Type::TYPE_DOUBLE),
stringify!(TYPE_FLOAT) => ::std::option::Option::Some(Type::TYPE_FLOAT),
stringify!(TYPE_INT64) => ::std::option::Option::Some(Type::TYPE_INT64),
stringify!(TYPE_UINT64) => ::std::option::Option::Some(Type::TYPE_UINT64),
stringify!(TYPE_INT32) => ::std::option::Option::Some(Type::TYPE_INT32),
stringify!(TYPE_FIXED64) => ::std::option::Option::Some(Type::TYPE_FIXED64),
stringify!(TYPE_FIXED32) => ::std::option::Option::Some(Type::TYPE_FIXED32),
stringify!(TYPE_BOOL) => ::std::option::Option::Some(Type::TYPE_BOOL),
stringify!(TYPE_STRING) => ::std::option::Option::Some(Type::TYPE_STRING),
stringify!(TYPE_GROUP) => ::std::option::Option::Some(Type::TYPE_GROUP),
stringify!(TYPE_MESSAGE) => ::std::option::Option::Some(Type::TYPE_MESSAGE),
stringify!(TYPE_BYTES) => ::std::option::Option::Some(Type::TYPE_BYTES),
stringify!(TYPE_UINT32) => ::std::option::Option::Some(Type::TYPE_UINT32),
stringify!(TYPE_ENUM) => ::std::option::Option::Some(Type::TYPE_ENUM),
stringify!(TYPE_SFIXED32) => ::std::option::Option::Some(Type::TYPE_SFIXED32),
stringify!(TYPE_SFIXED64) => ::std::option::Option::Some(Type::TYPE_SFIXED64),
stringify!(TYPE_SINT32) => ::std::option::Option::Some(Type::TYPE_SINT32),
stringify!(TYPE_SINT64) => ::std::option::Option::Some(Type::TYPE_SINT64),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Type] = &[
Type::TYPE_DOUBLE,
Expand Down Expand Up @@ -2317,6 +2341,15 @@ pub mod field_descriptor_proto {
}
}

fn from_str(str: &str) -> ::std::option::Option<Label> {
match str {
stringify!(LABEL_OPTIONAL) => ::std::option::Option::Some(Label::LABEL_OPTIONAL),
stringify!(LABEL_REQUIRED) => ::std::option::Option::Some(Label::LABEL_REQUIRED),
stringify!(LABEL_REPEATED) => ::std::option::Option::Some(Label::LABEL_REPEATED),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Label] = &[
Label::LABEL_OPTIONAL,
Label::LABEL_REQUIRED,
Expand Down Expand Up @@ -4908,6 +4941,15 @@ pub mod file_options {
}
}

fn from_str(str: &str) -> ::std::option::Option<OptimizeMode> {
match str {
stringify!(SPEED) => ::std::option::Option::Some(OptimizeMode::SPEED),
stringify!(CODE_SIZE) => ::std::option::Option::Some(OptimizeMode::CODE_SIZE),
stringify!(LITE_RUNTIME) => ::std::option::Option::Some(OptimizeMode::LITE_RUNTIME),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [OptimizeMode] = &[
OptimizeMode::SPEED,
OptimizeMode::CODE_SIZE,
Expand Down Expand Up @@ -5668,6 +5710,15 @@ pub mod field_options {
}
}

fn from_str(str: &str) -> ::std::option::Option<CType> {
match str {
stringify!(STRING) => ::std::option::Option::Some(CType::STRING),
stringify!(CORD) => ::std::option::Option::Some(CType::CORD),
stringify!(STRING_PIECE) => ::std::option::Option::Some(CType::STRING_PIECE),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [CType] = &[
CType::STRING,
CType::CORD,
Expand Down Expand Up @@ -5726,6 +5777,15 @@ pub mod field_options {
}
}

fn from_str(str: &str) -> ::std::option::Option<JSType> {
match str {
stringify!(JS_NORMAL) => ::std::option::Option::Some(JSType::JS_NORMAL),
stringify!(JS_STRING) => ::std::option::Option::Some(JSType::JS_STRING),
stringify!(JS_NUMBER) => ::std::option::Option::Some(JSType::JS_NUMBER),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [JSType] = &[
JSType::JS_NORMAL,
JSType::JS_STRING,
Expand Down Expand Up @@ -6678,6 +6738,15 @@ pub mod method_options {
}
}

fn from_str(str: &str) -> ::std::option::Option<IdempotencyLevel> {
match str {
stringify!(IDEMPOTENCY_UNKNOWN) => ::std::option::Option::Some(IdempotencyLevel::IDEMPOTENCY_UNKNOWN),
stringify!(NO_SIDE_EFFECTS) => ::std::option::Option::Some(IdempotencyLevel::NO_SIDE_EFFECTS),
stringify!(IDEMPOTENT) => ::std::option::Option::Some(IdempotencyLevel::IDEMPOTENT),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [IdempotencyLevel] = &[
IdempotencyLevel::IDEMPOTENCY_UNKNOWN,
IdempotencyLevel::NO_SIDE_EFFECTS,
Expand Down
4 changes: 4 additions & 0 deletions protobuf/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ pub trait Enum: Eq + Sized + Copy + fmt::Debug + Default + Send + Sync + 'static
/// Return `None` if value is unknown.
fn from_i32(v: i32) -> Option<Self>;

/// Try to create an enum from `&str` value.
/// Return `None` if str is unknown.
fn from_str(s: &str) -> Option<Self>;

/// All enum values for enum type.
const VALUES: &'static [Self] = &[];
}
8 changes: 8 additions & 0 deletions protobuf/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,14 @@ pub mod code_generator_response {
}
}

fn from_str(str: &str) -> ::std::option::Option<Feature> {
match str {
stringify!(FEATURE_NONE) => ::std::option::Option::Some(Feature::FEATURE_NONE),
stringify!(FEATURE_PROTO3_OPTIONAL) => ::std::option::Option::Some(Feature::FEATURE_PROTO3_OPTIONAL),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Feature] = &[
Feature::FEATURE_NONE,
Feature::FEATURE_PROTO3_OPTIONAL,
Expand Down
7 changes: 7 additions & 0 deletions protobuf/src/well_known_types/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,13 @@ impl crate::Enum for NullValue {
}
}

fn from_str(str: &str) -> ::std::option::Option<NullValue> {
match str {
stringify!(NULL_VALUE) => ::std::option::Option::Some(NullValue::NULL_VALUE),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [NullValue] = &[
NullValue::NULL_VALUE,
];
Expand Down
43 changes: 43 additions & 0 deletions protobuf/src/well_known_types/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,31 @@ pub mod field {
}
}

fn from_str(str: &str) -> ::std::option::Option<Kind> {
match str {
stringify!(TYPE_UNKNOWN) => ::std::option::Option::Some(Kind::TYPE_UNKNOWN),
stringify!(TYPE_DOUBLE) => ::std::option::Option::Some(Kind::TYPE_DOUBLE),
stringify!(TYPE_FLOAT) => ::std::option::Option::Some(Kind::TYPE_FLOAT),
stringify!(TYPE_INT64) => ::std::option::Option::Some(Kind::TYPE_INT64),
stringify!(TYPE_UINT64) => ::std::option::Option::Some(Kind::TYPE_UINT64),
stringify!(TYPE_INT32) => ::std::option::Option::Some(Kind::TYPE_INT32),
stringify!(TYPE_FIXED64) => ::std::option::Option::Some(Kind::TYPE_FIXED64),
stringify!(TYPE_FIXED32) => ::std::option::Option::Some(Kind::TYPE_FIXED32),
stringify!(TYPE_BOOL) => ::std::option::Option::Some(Kind::TYPE_BOOL),
stringify!(TYPE_STRING) => ::std::option::Option::Some(Kind::TYPE_STRING),
stringify!(TYPE_GROUP) => ::std::option::Option::Some(Kind::TYPE_GROUP),
stringify!(TYPE_MESSAGE) => ::std::option::Option::Some(Kind::TYPE_MESSAGE),
stringify!(TYPE_BYTES) => ::std::option::Option::Some(Kind::TYPE_BYTES),
stringify!(TYPE_UINT32) => ::std::option::Option::Some(Kind::TYPE_UINT32),
stringify!(TYPE_ENUM) => ::std::option::Option::Some(Kind::TYPE_ENUM),
stringify!(TYPE_SFIXED32) => ::std::option::Option::Some(Kind::TYPE_SFIXED32),
stringify!(TYPE_SFIXED64) => ::std::option::Option::Some(Kind::TYPE_SFIXED64),
stringify!(TYPE_SINT32) => ::std::option::Option::Some(Kind::TYPE_SINT32),
stringify!(TYPE_SINT64) => ::std::option::Option::Some(Kind::TYPE_SINT64),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Kind] = &[
Kind::TYPE_UNKNOWN,
Kind::TYPE_DOUBLE,
Expand Down Expand Up @@ -697,6 +722,16 @@ pub mod field {
}
}

fn from_str(str: &str) -> ::std::option::Option<Cardinality> {
match str {
stringify!(CARDINALITY_UNKNOWN) => ::std::option::Option::Some(Cardinality::CARDINALITY_UNKNOWN),
stringify!(CARDINALITY_OPTIONAL) => ::std::option::Option::Some(Cardinality::CARDINALITY_OPTIONAL),
stringify!(CARDINALITY_REQUIRED) => ::std::option::Option::Some(Cardinality::CARDINALITY_REQUIRED),
stringify!(CARDINALITY_REPEATED) => ::std::option::Option::Some(Cardinality::CARDINALITY_REPEATED),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Cardinality] = &[
Cardinality::CARDINALITY_UNKNOWN,
Cardinality::CARDINALITY_OPTIONAL,
Expand Down Expand Up @@ -1272,6 +1307,14 @@ impl crate::Enum for Syntax {
}
}

fn from_str(str: &str) -> ::std::option::Option<Syntax> {
match str {
stringify!(SYNTAX_PROTO2) => ::std::option::Option::Some(Syntax::SYNTAX_PROTO2),
stringify!(SYNTAX_PROTO3) => ::std::option::Option::Some(Syntax::SYNTAX_PROTO3),
_ => ::std::option::Option::None
}
}

const VALUES: &'static [Syntax] = &[
Syntax::SYNTAX_PROTO2,
Syntax::SYNTAX_PROTO3,
Expand Down