Generate Swift structs to represent all MAVLink messages

def generate_messages(outf, msgs):
    """Generate Swift structs to represent all MAVLink messages"""

    print("Generating Messages")

    t.write(outf, """

// MARK: MAVLink messages


/**
    Message protocol describes common for all MAVLink messages properties and methods requirements
*/
public protocol Message: MAVLinkEntity {

    /**
        Initialize Message from received data

        - Warning: Throws `ParserError` or `ParserEnumError` if any parsing error occurred
    */
    init(data: NSData) throws

    /// Array of tuples with fields name, offset, type and description information
    static var fieldsInfo: [(String, Int, String, String)] { get }

    /// All fields names and values of current Message
    var allFields: [(String, Any)] { get }
}


/**
    Message protocol default implementations
*/
extension Message {
    public static var typeDebugDescription: String {
        // It seems Xcode 7 beta does not support default protocol implementations for type methods.
        // Calling this method without specific implementations inside messages will cause following error in Xcode 7.0 beta (7A120f):
        // "Command failed due to signal: Illegal instruction: 4"
        let fields = "\\n\\t".join(fieldsInfo.map { "\($0.0): \($0.2): \($0.3)" })
        return "Struct \(typeName): \(typeDescription)\\nFields:\\n\\t\(fields)"
    }

    public var description: String {
        let describeField: ((String, Any)) -> String = { (let name, var value) in
            value = value is String ? "\\"\(value)\\"" : value
            return "\(name): \(value)"
        }
        let fieldsDescription = ", ".join(allFields.map(describeField))
        return "\(self.dynamicType)(\(fieldsDescription))"
    }

    public var debugDescription: String {
        let describeFieldVerbose: ((String, Any)) -> String = { (let name, var value) in
            value = value is String ? "\\"\(value)\\"" : value
            let (_, _, _, description) = Self.fieldsInfo.filter { $0.0 == name }.first!
            return "\(name) = \(value) : \(description)"
        }
        let fieldsDescription = "\\n\\t".join(allFields.map(describeFieldVerbose))
        return "\(Self.typeName): \(Self.typeDescription)\\nFields:\\n\\t\(fieldsDescription)"
    }

    public var allFields: [(String, Any)] {
        var result: [(String, Any)] = []
        let mirror = reflect(self)
        for i in 0..<mirror.count {
            result.append((mirror[i].0, mirror[i].1.value))
        }
        return result
    }
}

""")

    for msg in msgs:
        t.write(outf, """
${formatted_description}public struct ${swift_name}: Message {
${{fields:${formatted_description}\tpublic let ${swift_name}: ${return_type}\n}}

    public init(data: NSData) throws {
${{ordered_fields:\t\tself.${swift_name} = ${initial_value}\n}}
    }
}

extension ${swift_name} {
    public static var typeName = "${name}"
    public static var typeDescription = "${message_description}"
    public static var typeDebugDescription: String {
        let fields = "\\n\\t".join(fieldsInfo.map { "\($0.0): \($0.2): \($0.3)" })
        return "Struct \(typeName): \(typeDescription)\\nFields:\\n\\t\(fields)"
    }
    public static var fieldsInfo = [${fields_info}]
}

""", msg)