Skip to content

num42/swift-macro-caseconversion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CaseConversion

Adds as<Case> helpers for enum cases.

Usage

@CaseConversion
enum TestEnum {
  case firstCase
  case secondCase(string: String, bool: Bool)
  case thirdCase(argument: String)
  case fourthCase(subtypeArgument: SubType.String)
}

Generated:

enum TestEnum {
  case firstCase
  case secondCase(string: String, bool: Bool)
  case thirdCase(argument: String)
  case fourthCase(subtypeArgument: SubType.String)

    var asSecondCase: (string: String, bool: Bool)? {
      if case let .secondCase(string, bool) = self {
        (string, bool)
      } else {
        nil
      }
    }

    var asThirdCase: String? {
      if case let .thirdCase(argument) = self {
        argument
      } else {
        nil
      }
    }

    var asFourthCase: SubType.String? {
      if case let .fourthCase(subtypeArgument) = self {
        subtypeArgument
      } else {
        nil
      }
    }
}

Notes

Apply to enums only.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages