Skip to content

Commit

Permalink
Add from_str() for trait Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
yestyle authored and stepancheg committed Oct 2, 2023
1 parent 25e302b commit e3bdc67
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
24 changes: 24 additions & 0 deletions protobuf-codegen/src/gen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,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!(
"\"{}\" => ::std::option::Option::Some({}),",
value.value.proto.name(),
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 +286,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
69 changes: 69 additions & 0 deletions protobuf/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,30 @@ pub mod field_descriptor_proto {
}
}

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

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

fn from_str(str: &str) -> ::std::option::Option<Label> {
match str {
"LABEL_OPTIONAL" => ::std::option::Option::Some(Label::LABEL_OPTIONAL),
"LABEL_REQUIRED" => ::std::option::Option::Some(Label::LABEL_REQUIRED),
"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 {
"SPEED" => ::std::option::Option::Some(OptimizeMode::SPEED),
"CODE_SIZE" => ::std::option::Option::Some(OptimizeMode::CODE_SIZE),
"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 {
"STRING" => ::std::option::Option::Some(CType::STRING),
"CORD" => ::std::option::Option::Some(CType::CORD),
"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 {
"JS_NORMAL" => ::std::option::Option::Some(JSType::JS_NORMAL),
"JS_STRING" => ::std::option::Option::Some(JSType::JS_STRING),
"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 {
"IDEMPOTENCY_UNKNOWN" => ::std::option::Option::Some(IdempotencyLevel::IDEMPOTENCY_UNKNOWN),
"NO_SIDE_EFFECTS" => ::std::option::Option::Some(IdempotencyLevel::NO_SIDE_EFFECTS),
"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 {
"FEATURE_NONE" => ::std::option::Option::Some(Feature::FEATURE_NONE),
"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 {
"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 {
"TYPE_UNKNOWN" => ::std::option::Option::Some(Kind::TYPE_UNKNOWN),
"TYPE_DOUBLE" => ::std::option::Option::Some(Kind::TYPE_DOUBLE),
"TYPE_FLOAT" => ::std::option::Option::Some(Kind::TYPE_FLOAT),
"TYPE_INT64" => ::std::option::Option::Some(Kind::TYPE_INT64),
"TYPE_UINT64" => ::std::option::Option::Some(Kind::TYPE_UINT64),
"TYPE_INT32" => ::std::option::Option::Some(Kind::TYPE_INT32),
"TYPE_FIXED64" => ::std::option::Option::Some(Kind::TYPE_FIXED64),
"TYPE_FIXED32" => ::std::option::Option::Some(Kind::TYPE_FIXED32),
"TYPE_BOOL" => ::std::option::Option::Some(Kind::TYPE_BOOL),
"TYPE_STRING" => ::std::option::Option::Some(Kind::TYPE_STRING),
"TYPE_GROUP" => ::std::option::Option::Some(Kind::TYPE_GROUP),
"TYPE_MESSAGE" => ::std::option::Option::Some(Kind::TYPE_MESSAGE),
"TYPE_BYTES" => ::std::option::Option::Some(Kind::TYPE_BYTES),
"TYPE_UINT32" => ::std::option::Option::Some(Kind::TYPE_UINT32),
"TYPE_ENUM" => ::std::option::Option::Some(Kind::TYPE_ENUM),
"TYPE_SFIXED32" => ::std::option::Option::Some(Kind::TYPE_SFIXED32),
"TYPE_SFIXED64" => ::std::option::Option::Some(Kind::TYPE_SFIXED64),
"TYPE_SINT32" => ::std::option::Option::Some(Kind::TYPE_SINT32),
"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 {
"CARDINALITY_UNKNOWN" => ::std::option::Option::Some(Cardinality::CARDINALITY_UNKNOWN),
"CARDINALITY_OPTIONAL" => ::std::option::Option::Some(Cardinality::CARDINALITY_OPTIONAL),
"CARDINALITY_REQUIRED" => ::std::option::Option::Some(Cardinality::CARDINALITY_REQUIRED),
"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 {
"SYNTAX_PROTO2" => ::std::option::Option::Some(Syntax::SYNTAX_PROTO2),
"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

0 comments on commit e3bdc67

Please sign in to comment.