'Write a "rich" string with multiple formats to a worksheet cell.
Args:
row:          The cell row (zero indexed).
col:          The cell column (zero indexed).
string_parts: String and format pairs.
cell_format:  Optional Format object.
Returns:
0:  Success.
-1: Row or column is out of worksheet bounds.
-2: String truncated to 32k characters.
-3: 2 consecutive formats used.'

@convert_cell_args
def write_rich_string(self, row, col, *args):

    tokens = list(args)
    cell_format = None
    str_length = 0
    string_index = 0
    if self._check_dimensions(row, col):
        return (-1)
    if isinstance(tokens[(-1)], Format):
        cell_format = tokens.pop()
    fh = StringIO()
    self.rstring = XMLwriter()
    self.rstring._set_filehandle(fh)
    default = Format()
    fragments = []
    previous = 'format'
    pos = 0
    for token in tokens:
        if (not isinstance(token, Format)):
            if (previous != 'format'):
                fragments.append(default)
                fragments.append(token)
            else:
                fragments.append(token)
            str_length += len(token)
            previous = 'string'
        else:
            if ((previous == 'format') and (pos > 0)):
                return (-3)
            fragments.append(token)
            previous = 'format'
        pos += 1
    if (not isinstance(fragments[0], Format)):
        self.rstring._xml_start_tag('r')
    for token in fragments:
        if isinstance(token, Format):
            self.rstring._xml_start_tag('r')
            self._write_font(token)
        else:
            attributes = []
            if (re.search('^\\s', token) or re.search('\\s$', token)):
                attributes.append(('xml:space', 'preserve'))
            self.rstring._xml_data_element('t', token, attributes)
            self.rstring._xml_end_tag('r')
    string = self.rstring.fh.getvalue()
    if (str_length > self.xls_strmax):
        return (-2)
    if (not self.constant_memory):
        string_index = self.str_table._get_shared_string_index(string)
    else:
        string_index = string
    if (self.constant_memory and (row > self.previous_row)):
        self._write_single_row(row)
    self.table[row][col] = cell_string_tuple(string_index, cell_format)
    return 0
