| 1 | enum Traffic { |
| 2 | rx_bytes |
| 3 | tx_bytes |
| 4 | } |
| 5 | |
| 6 | fn (t Traffic) str() string { |
| 7 | return match t { |
| 8 | .rx_bytes { 'rx-bytes' } |
| 9 | .tx_bytes { 'tx-bytes' } |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | fn Traffic.from_string(s string) ?Traffic { |
| 14 | return match s { |
| 15 | 'rx-bytes' { .rx_bytes } |
| 16 | 'tx-bytes' { .tx_bytes } |
| 17 | else { none } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn test_main() { |
| 22 | traffic := Traffic.from_string('rx-bytes') or { return } |
| 23 | assert Traffic.rx_bytes.str() == 'rx-bytes' |
| 24 | assert '${traffic}' == 'rx-bytes' |
| 25 | assert '${Traffic.rx_bytes}' == 'rx-bytes' |
| 26 | } |
| 27 | |