<commit_before>package ics

import (
	"errors"
	"io"

	"github.com/MJKWoolnough/parser"
)

type psuedoTokeniser []parser.Phrase

func (p *psuedoTokeniser) GetPhrase() (parser.Phrase, error) {
	if len(*p) == 0 {
		return parser.Phrase{
			Type: parser.PhraseError,
			Data: nil,
		}, io.ErrUnexpectedEOF
	}
	ph := (*p)[0]
	*p = (*p)[1:]
	return ph, nil
}

// AlarmType is an interface this is fulfilled by AlarmAudio, AlarmDisplay and
// AlarmEmail
type AlarmType interface {
	section
	Type() string
}

// Alarm is the encompassing type for the three alarm types
type Alarm struct {
	AlarmType
}

func (a *Alarm) decode(t tokeniser) error {
	var pt psuedoTokeniser
Loop:
	for {
		ph, err := t.GetPhrase()
		if err != nil {
			return err
		}
		pt = append(pt, ph)
		switch ph.Data[0].Data {
		case "BEGIN":
			return ErrInvalidStructure
		case "ACTION":
			if a.AlarmType != nil {
				return ErrInvalidStructure
			}
			switch ph.Data[len(ph.Data)-1].Data {
			case "AUDIO":
				a.AlarmType = new(AlarmAudio)
			case "DISPLAY":
				a.AlarmType = new(AlarmDisplay)
			case "EMAIL":
				a.AlarmType = new(AlarmEmail)
			}
		case "END":
			break Loop
		}
	}
	if a.AlarmType == nil {
		return ErrMissingAlarmAction
	}
	return a.AlarmType.decode(&pt)
}

func (a *Alarm) encode(w writer) {
	w.WriteString("BEGIN:VALARM\r\n")
	switch a.AlarmType.(type) {
	case *AlarmAudio:
		w.WriteString("ACTION:AUDIO\r\n")
	case *AlarmDisplay:
		w.WriteString("ACTION:DISPLAY\r\n")
	case *AlarmEmail:
		w.WriteString("ACTION:EMAIL\r\n")
	}
	a.encode(w)
	w.WriteString("END:VALARM\r\n")
}

func (a *Alarm) valid() error {
	switch a.AlarmType.(type) {
	case *AlarmAudio, *AlarmDisplay, *AlarmEmail:
		return a.AlarmType.valid()
	}
	return ErrInvalidAlarm
}

// Type returns the type of the alarm "AUDIO"<commit_msg>corrected encode call for alarms<commit_after>package ics

import (
	"errors"
	"io"

	"github.com/MJKWoolnough/parser"
)

type psuedoTokeniser []parser.Phrase

func (p *psuedoTokeniser) GetPhrase() (parser.Phrase, error) {
	if len(*p) == 0 {
		return parser.Phrase{
			Type: parser.PhraseError,
			Data: nil,
		}, io.ErrUnexpectedEOF
	}
	ph := (*p)[0]
	*p = (*p)[1:]
	return ph, nil
}

// AlarmType is an interface this is fulfilled by AlarmAudio, AlarmDisplay and
// AlarmEmail
type AlarmType interface {
	section
	Type() string
}

// Alarm is the encompassing type for the three alarm types
type Alarm struct {
	AlarmType
}

func (a *Alarm) decode(t tokeniser) error {
	var pt psuedoTokeniser
Loop:
	for {
		ph, err := t.GetPhrase()
		if err != nil {
			return err
		}
		pt = append(pt, ph)
		switch ph.Data[0].Data {
		case "BEGIN":
			return ErrInvalidStructure
		case "ACTION":
			if a.AlarmType != nil {
				return ErrInvalidStructure
			}
			switch ph.Data[len(ph.Data)-1].Data {
			case "AUDIO":
				a.AlarmType = new(AlarmAudio)
			case "DISPLAY":
				a.AlarmType = new(AlarmDisplay)
			case "EMAIL":
				a.AlarmType = new(AlarmEmail)
			}
		case "END":
			break Loop
		}
	}
	if a.AlarmType == nil {
		return ErrMissingAlarmAction
	}
	return a.AlarmType.decode(&pt)
}

func (a *Alarm) encode(w writer) {
	w.WriteString("BEGIN:VALARM\r\n")
	switch a.AlarmType.(type) {
	case *AlarmAudio:
		w.WriteString("ACTION:AUDIO\r\n")
	case *AlarmDisplay:
		w.WriteString("ACTION:DISPLAY\r\n")
	case *AlarmEmail:
		w.WriteString("ACTION:EMAIL\r\n")
	}
	a.AlarmType.encode(w)
	w.WriteString("END:VALARM\r\n")
}

func (a *Alarm) valid() error {
	switch a.AlarmType.(type) {
	case *AlarmAudio, *AlarmDisplay, *AlarmEmail:
		return a.AlarmType.valid()
	}
	return ErrInvalidAlarm
}

// Type returns the type of the alarm "AUDIO"
{'fraction_non_alphanumeric': 0.13763500415397398, 'fraction_numerical': 0.0027693159789531985, 'mean_word_length': 3.6189258312020463, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185426', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1410, 'n_words': 335}