Code Explainers

Code explainers tagged #enums

typescript
export const OrderStatus = {
  Pending: 'pending',
  Paid: 'paid',
  Shipped: 'shipped',

A type-safe order state machine in TypeScript

state-machine union-types type-guards
Intermediate 8 steps
rust
use axum::{
    http::{header, StatusCode},
    response::{IntoResponse, Response},
    Json,

Turning errors into RFC 7807 responses in Axum

error-handling enums trait-implementation
Intermediate 7 steps
rust
use std::collections::VecDeque;
 
#[derive(Debug)]
pub struct Hunk {

Applying a diff hunk in Rust

enums error-handling pattern-matching
Intermediate 8 steps
rust
#[derive(Debug, PartialEq)]
enum State {
    FieldStart,
    InUnquoted,

Parsing a CSV line with a state machine

state machine parsing enums
Intermediate 9 steps
rust
use std::convert::TryFrom;
 
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HttpStatus {

Converting HTTP codes with TryFrom in Rust

enums error-handling trait-implementation
Intermediate 8 steps
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    Locked,
    Unlocked,

A turnstile state machine in Rust

state machine enums pattern matching
Intermediate 9 steps
rust
use serde::{Deserialize, Serialize};
 
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]

Tagged enums with serde in Rust

enums serialization json
Intermediate 6 steps
rust
use std::fmt;
 
#[derive(Debug)]
pub enum ConfigError {

Building a custom error type in Rust

error-handling enums trait-implementation
Intermediate 9 steps
rust
use std::fmt;
 
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Money {

Formatting money with Rust's Display trait

trait-implementation integer-arithmetic enums
Intermediate 10 steps
ruby
class Subscription < ApplicationRecord
  belongs_to :account
 
  enum :status, {

Modeling subscription lifecycle in Rails

enums scopes state-management
Intermediate 8 steps
rust
use std::collections::HashMap;
use std::fmt;
 
#[derive(Debug)]

Parsing a config file with typed errors in Rust

error-handling enums parsing
Intermediate 9 steps
rust
use axum::{
    http::StatusCode,
    response::{IntoResponse, Response},
    Json,

How a custom error type maps to HTTP in Axum

error-handling enums trait-implementation
Intermediate 7 steps
ruby
class Survey < ApplicationRecord
  has_many :questions, dependent: :destroy
  has_many :respondents, dependent: :nullify
 

Nested survey forms in Rails

nested-attributes validations associations
Intermediate 9 steps
rust
use std::error::Error;
use std::fmt;
 
#[derive(Debug)]

Building a typed error enum in Rust

error-handling enums trait-implementation
Intermediate 9 steps