]> git.lizzy.rs Git - go-anidb.git/blobdiff - misc/episodelist.go
misc: Allow adding/removing arbitrary episodes to an EpisodeList
[go-anidb.git] / misc / episodelist.go
index 7f7d0abd8e2edc752587cbb36919c6ad9885575a..61be68bf0257717d16854bf415440ea053c5c452 100644 (file)
@@ -25,6 +25,19 @@ func RangesToList(ranges ...*EpisodeRange) EpisodeList {
        return EpisodeList(ranges)
 }
 
+func ContainerToList(ec EpisodeContainer) EpisodeList {
+       switch v := ec.(type) {
+       case *Episode:
+               return EpisodeToList(v)
+       case *EpisodeRange:
+               return RangesToList(v)
+       case EpisodeList:
+               return v
+       default:
+               panic("unimplemented")
+       }
+}
+
 // Converts the EpisodeList into the AniDB API list format.
 func (el EpisodeList) String() string {
        scales := map[EpisodeType]int{}
@@ -196,3 +209,16 @@ func (el EpisodeList) Less(i, j int) bool {
 func (el EpisodeList) Swap(i, j int) {
        el[i], el[j] = el[j], el[i]
 }
+
+func (el *EpisodeList) Add(ec EpisodeContainer) {
+       *el = append(*el, ContainerToList(ec)...)
+       *el = el.Simplify()
+}
+
+func (el *EpisodeList) Sub(ep *Episode) {
+       el2 := make(EpisodeList, 0, len(*el))
+       for _, r := range *el {
+               el2 = append(el2, r.Split(ep)...)
+       }
+       *el = append(*el, el2.Simplify()...)
+}