Can I convert a string to enum without macros in Rust?

You should implement std::str::FromStr trait. use std::str::FromStr; #[derive(Debug, PartialEq)] enum Foo { Bar, Baz, Bat, Quux, } impl FromStr for Foo { type Err = (); fn from_str(input: &str) -> Result<Foo, Self::Err> { match input { “Bar” => Ok(Foo::Bar), “Baz” => Ok(Foo::Baz), “Bat” => Ok(Foo::Bat), “Quux” => Ok(Foo::Quux), _ => Err(()), } } } fn … Read more

How to enable enum inheritance

You cannot have an enum extend another enum, and you cannot “add” values to an existing enum through inheritance. However, enums can implement interfaces. What I would do is have the original enum implement a marker interface (i.e. no method declarations), then your client could create their own enum implementing the same interface. Then your … Read more

Typescript `enum` from JSON string

If you are using Typescript before the 2.4 release, there is a way to achieve that with enums by casting the values of your enum to any. An example of your first implementation enum Type { NEW = <any>”NEW”, OLD = <any>”OLD”, } interface Thing { type: Type } let thing:Thing = JSON.parse(‘{“type”: “NEW”}’); alert(thing.type … Read more

Hibernate mapping between PostgreSQL enum and Java enum

You can simply get these types via Maven Central using the Hypersistence Util dependency: <dependency> <groupId>io.hypersistence</groupId> <artifactId>hypersistence-utils-hibernate-55</artifactId> <version>${hibernate-types.version}</version> </dependency> If you easily map Java Enum to a PostgreSQL Enum column type using the following custom Type: public class PostgreSQLEnumType extends org.hibernate.type.EnumType { public void nullSafeSet( PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, … Read more

A more pythonic way to define an enum with dynamic members

Update Using JSONEnum at the bottom of When should I subclass EnumMeta instead of Enum?, you can do this: class Country(JSONEnum): _init_ = ‘abbr code country_name’ # remove if not using aenum _file=”some_file.json” _name=”alpha-2″ _value = { 1: (‘alpha-2’, None), 2: (‘country-code’, lambda c: int(c)), 3: (‘name’, None), } Original Answer It looks like you … Read more