from typing import List


class Message:
    @classmethod
    def parse(cls, string: str):
        if string.startswith(':'):
            prefix, string = string.split(' ', 1)
            prefix = prefix[1:]
        else:
            prefix = None

        command, string = string.split(' ', 1)
        parameters = []

        while len(string) != 0:
            if string[0] == ':':
                parameters.append(string[1:])
                string = ''
            elif ' ' in string:
                parameter, string = string.split(' ', 1)
                parameters.append(parameter)
            else:
                parameters.append(string)
                string = ''

        return cls(prefix, command, parameters)

    def __init__(self, prefix: str = None, command: str = '', parameters: List[str] = None):
        self.prefix = prefix
        self.command = command
        self.parameters = parameters or []

    def __str__(self):
        string = ''

        if self.prefix:
            string += ':' + self.prefix

        if len(self.command) > 0:
            if len(string) > 0:
                string += ' '

            string += self.command

        if len(self.parameters) > 0:
            if len(string) > 0:
                string += ' '

            def parameter_to_string(item):
                index = item[0]
                parameter = item[1]

                if index + 1 == len(self.parameters) and (len(parameter) == 0 or ' ' in parameter or parameter.startswith(':')):
                    return ':' + parameter
                return parameter

            string += ' '.join(map(parameter_to_string, enumerate(self.parameters)))

        return string

@@ -84,16 +84,352 @@
 : str):%0A
+        %22%22%22%0A        Parse an IRC line into a Message instance%0A%0A        %3E%3E%3E message = Message.parse(':doe!doe@example.com PRIVMSG #example :Hello World')%0A        %3E%3E%3E message.prefix%0A        'doe!doe@example.com'%0A        %3E%3E%3E message.command%0A        'PRIVMSG'%0A        %3E%3E%3E message.parameters%0A        %5B'#example', 'Hello World'%5D%0A        %22%22%22%0A%0A
         
@@ -1281,16 +1281,197 @@
 (self):%0A
+        %22%22%22%0A        Converts a Message instance into a string%0A%0A        %3E%3E%3E message = Message(command='CAP', parameters=%5B'LS'%5D)%0A        %3E%3E%3E str(message)%0A        'CAP LS'%0A        %22%22%22%0A
         

add doctests for Message