Functions

The following functions are available globally.

  • Creates a Fragment formatted specifically for Swift computed properties

    Declaration

    Swift

    @inlinable
    public func computedPropertySpec(
        _ name: String,
        access: Access = .internal,
        isStatic: Bool = false,
        returnValue: String,
        @CodeBuilder _ body: () -> CodeRepresentable
    ) -> CodeRepresentable

    Parameters

    name

    The name of the computed property

    access

    The access level of the the computed property

    isStatic

    Bool flag that determines whether or not the computed property is static

    returnValue

    The return value of the function

    body

    Fragments that represent the body of the computed property

    Return Value

    A GroupFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for Swift control flow statements

    Example:

    fileSpec("   ") {
       controlFlowSpec("if word == \"That\"") {
           statement("print(\"Hello, \\(word)\")")
       }
       end()
    }
    

    renders:

    if word == "That" {
        print("Hello, \(word)")
    }
    

    Declaration

    Swift

    @inlinable
    public func controlFlowSpec(_ statement: String, @CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    statement

    String representing the logic for the control flow.

    builder

    Fragments that represent the body of the control flow.

    Return Value

    A MultiLineFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for else if Swift control flow statements

    Example:

    fileSpec("   ") {
       controlFlowSpec("if word == \"That\"") {
           statement("print(\"word is That\")")
       }
       elseIfSpec("word == \"This\"") {
           statement("print(\"word is This\")")
       }
       end()
    }
    

    renders:

    if word == "That" {
        print("word is That")
    } else if word == "This" {
        print("word is This")
    }
    

    Declaration

    Swift

    @inlinable
    public func elseIfSpec(_ statement: String, @CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    statement

    String representing the Bool logic for the else if control flow.

    builder

    Fragments that represent the body of the else if control flow.

    Return Value

    A MultiLineFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for else Swift control flow statements

    Example:

    fileSpec("   ") {
       controlFlowSpec("if word == \"That\"") {
           statement("print(\"word is That\")")
       }
       elseIfSpec("word == \"This\"") {
           statement("print(\"word is This\")")
       }
       elseSpec {
           statement("print(\"word is neither That or This\")")
       }
       end()
    }
    

    renders:

    if word == "That" {
        print("word is That")
    } else if word == "This" {
        print("word is This")
    } else {
       print("word is neither That or This")
    }
    

    Declaration

    Swift

    @inlinable
    public func elseSpec(@CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    builder

    Fragments that represent the body of the else control flow.

    Return Value

    A MultiLineFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for guard Swift control flow statements

    Example:

    fileSpec("   ") {
        guardSpec(
            statements: {
                statement("x == 0")
                statement("y == 0")
            },
            elseBlock: {
                statement(
                    """
                   print("Bool failed")
                   """
                )
                statement("return")
            }
        )
    }
    

    renders:

    guard
        x == 0,
        y == 0
    else {
        print("Bool failed")
        return
    }
    

    Declaration

    Swift

    @inlinable
    public func guardSpec(@CodeBuilder statements: () -> CodeRepresentable, @CodeBuilder elseBlock: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    statements

    The Fragments that represent the guard control flow’s body.

    elseBlock

    The Fragment that represent a guard control flow’s else block.

    Return Value

    A GroupFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for do Swift control flow statements

    Example:

    fileSpec("   ") {
        doSpec {
            statement("let realm = try Realm()")
            statement("print(realm)")
        }
        end()
    }
    

    renders:

    do {
        let realm = try Realm()
        print(realm)
    }
    

    Declaration

    Swift

    @inlinable
    public func doSpec(@CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    builder

    The Fragments that represent the do control flow’s body.

    Return Value

    A GroupFragment typed as CodeRepresentable

  • Creates a Fragment formatted specifically for catch Swift control flow statements

    Example:

    fileSpec("   ") {
        doSpec {
            statement("let realm = try Realm()")
            statement("print(realm)")
        }
        catchSpec(statement: "let error") {
            statement(#"print("failed")"#)
            statement("print(error.localizedDescription)")
        }
        end()
    }
    

    renders:

    do {
        let realm = try Realm()
        print(realm)
    } catch let error {
        print("failed")
        print(error.localziedDescription)
    }
    

    Declaration

    Swift

    @inlinable
    public func catchSpec(statement: String? = nil, @CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    statement

    The catch statement

    builder

    The Fragments that represent the catch control flow’s body.

    Return Value

    A MultiLineFragment typed as CodeRepresentable

  • Create a CodeFragment formatted for documentation

    Declaration

    Swift

    @inlinable
    public func documentationSpec(
        _ content: String,
        format: Documentation.Format = .singleLine,
        parameters: [Parameter] = [],
        returns returnsValue: String? = nil,
        tag: String? = nil
    ) -> CodeRepresentable

    Parameters

    content

    The content of the documentation

    format

    Determines whether or not the content will use single line or multiline documentaion notation

    parameters

    • content : The content of the documentation
    • format : Determines whether or not the content will use single line or multiline documentaion notation
    • returnsValue : The return value documentation
    • tag : The tag link to this documentation

    returnsValue

    The return value documentation

    tag

    The tag link to this documentation

    Return Value

    A Documentation typed as CodeRepresentable

  • Creates a File that represents the Swift code built from the Fragments.

    Declaration

    Swift

    @inlinable
    public func fileSpec(fileName: String, indent: String, @CodeBuilder _ builder: () -> CodeRepresentable) -> File

    Parameters

    fileName

    The name of the Swift file to be created

    indent

    Whitespace indentation used to render Swift code

    builder

    Creates Fragments that build the String representing Swift code.

    Return Value

    A File instance

  • Creates a Fragment formatted specifically for Swift functions

    Declaration

    Swift

    @inlinable
    public func functionSpec(
        _ name: String,
        access: Access = .internal,
        isStatic: Bool = false,
        keywords: Set<FunctionKeyword> = [],
        genericSignature: String? = nil,
        arguments: [Argument] = [],
        returnValue: String? = nil,
        @CodeBuilder _ builder: () -> CodeRepresentable
    ) -> CodeRepresentable

    Parameters

    name

    The name of the function

    access

    The access level of the function

    isStatic

    Bool flag that determines whether or not the function is a static method

    throwsError

    Bool flag that determines whether or not the function throws

    genericSignature

    The generic signature of the function

    arguments

    The arguments of the function

    returnValue

    The return value of the function

    builder

    Fragments that represent the body of the function

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a SingleLineFragment out of a String

    Declaration

    Swift

    @inlinable
    public func statement(_ statement: String) -> Fragment

    Parameters

    statement

    The string content of the SingleLineFragment

    Return Value

    A SingleLineFragment typed as a Fragment

  • Adds a line break

    Declaration

    Swift

    @inlinable
    public func lineBreak() -> Fragment

    Return Value

    A SingleLineFragment typed as a Fragment

  • Ends scope

    Declaration

    Swift

    @inlinable
    public func end() -> Fragment

    Return Value

    A SingleLineFragment typed as a Fragment

  • Creates a Fragment formatted specifically as an initializer.

    Declaration

    Swift

    @inlinable
    public func initializerSpec(
        access: Access = .internal,
        documentation: Documentation? = nil,
        arguments: [Argument],
        throwsError: Bool,
        @CodeBuilder _ body: () -> CodeRepresentable
    ) -> CodeRepresentable

    Parameters

    access

    The access level of the initializer

    documentation

    The documentation of the initializer

    arguments

    The arguments of the initializer

    throwsError

    Bool flag indicating if the initializer throws or not

    body

    The body of the initializer

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a Fragment formatted specifically as an initializer with no custom body other than assigning arguments.

    Declaration

    Swift

    @inlinable
    public func initializerSpec(
        access: Access = .internal,
        documentation: Documentation? = nil,
        arguments: [Argument],
        throwsError: Bool = false
    ) -> CodeRepresentable

    Parameters

    access

    The access level of the initializer

    documentation

    The documentation of the initializer

    arguments

    The arguments of the initializer

    throwsError

    Bool flag indicating if the initializer throws or not

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Transforms the String into SingleLineFragments and wraps the array in a Code.fragments enum case.

    The content is separated by a newline, removes trailing whitespace and transformed into an array of SingleFragments

    Declaration

    Swift

    @inlinable
    public func rawSpec(_ content: String) -> CodeRepresentable

    Parameters

    content

    A String that represents Swift code.

    Return Value

    A Code.fragments instance as CodeRepresentable

  • Creates a Fragment formatted specifically for defining a Swift type

    Declaration

    Swift

    @inlinable
    public func typeSpec(
        _ name: String,
        access: Access = .internal,
        type: DataType,
        inheritingFrom parents: [String] = [],
        @CodeBuilder _ builder: () -> CodeRepresentable = { Code.none }
    ) -> CodeRepresentable

    Parameters

    name

    The name of the type being defined

    access

    The access level of the type

    type

    Determines whether the type being defined is a class, enum or struct

    parents

    The parent class/protocols the type inherits or conforms to

    builder

    Fragments that represent the body of the type’s definition

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a Fragment formatted specifically for defining a Swift class type

    Declaration

    Swift

    @inlinable
    public func classSpec(_ name: String, access: Access = .internal, inheritingFrom parents: [String] = [], @CodeBuilder _ builder: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    name

    The name of the class being defined

    access

    The access level of the class

    parents

    The parent class/protocols the class inherits or conforms to

    builder

    Fragments that represent the body of the class’s definition

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a Fragment formatted specifically for defining a Swift class type

    Declaration

    Swift

    @inlinable
    public func structSpec(_ name: String, access: Access = .internal, inheritingFrom protocols: [String] = [], @CodeBuilder _ body: () -> CodeRepresentable) -> CodeRepresentable

    Parameters

    name

    The name of the struct being defined

    access

    The access level of the struct

    protocols

    The protocols the struct conforms to

    body

    Fragments that represent the body of the struct’s definition

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a Fragment formatted specifically for defining a Swift enum type with either normal enum cases, associated value cases, or both

    Declaration

    Swift

    @inlinable
    public func enumSpec(
        access: Access = .internal,
        enumSpec: Enum,
        inheritingFrom protocols: [String] = [],
        @CodeBuilder _ body: () -> CodeRepresentable = { Code.none }
    ) -> CodeRepresentable

    Parameters

    access

    The access level of the enum

    enumSpec

    The enum specification

    protocols

    The protocols the enum conforms to

    body

    Fragments that represent the body of the enum’s definition. This should not contain cases.

    Return Value

    A GroupFragment typed as a CodeRepresentable

  • Creates a Fragment formatted specifically for defining a Swift raw value enum

    Declaration

    Swift

    @inlinable
    public func rawValueEnumSpec<T>(
        access: Access = .internal,
        enumSpec: RawValueEnum<T>,
        inheritingFrom protocols: [String] = [],
        @CodeBuilder _ body: () -> CodeRepresentable = { Code.none }
    ) -> CodeRepresentable

    Parameters

    access

    The access level of the enum

    enumSpec

    The RawValueEnum specification

    protocols

    The protocols the enum conforms to

    body

    Fragments that represent the body of the enum’s definition. This should not contain cases.

    Return Value

    A GroupFragment typed as a CodeRepresentable