Skip to content

Commit

Permalink
rename type to Chapter from ChapterSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Oct 9, 2024
1 parent fede02d commit d594695
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
14 changes: 7 additions & 7 deletions internal/cast/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import (
)

type Audio struct {
Name string `json:"-"`
Title string `json:"title"`
FileSize int64 `json:"file_size"`
Duration uint64 `json:"duration"`
Chapters []*ChapterSegment `json:"chapters,omitempty"`
Name string `json:"-"`
Title string `json:"title"`
FileSize int64 `json:"file_size"`
Duration uint64 `json:"duration"`
Chapters []*Chapter `json:"chapters,omitempty"`

rawDuration time.Duration
modTime time.Time
Expand Down Expand Up @@ -119,7 +119,7 @@ func (au *Audio) SaveMeta(rootDir string) error {
return nil
}

func (au *Audio) UpdateChapter(fpath string, chs []*ChapterSegment) error {
func (au *Audio) UpdateChapter(fpath string, chs []*Chapter) error {
// XXX: check the fpath is valid
f, err := os.OpenFile(fpath, os.O_RDWR, 0666)
if err != nil {
Expand Down Expand Up @@ -226,7 +226,7 @@ func (au *Audio) readMP3(r io.ReadSeeker) error {
for _, frame := range tag.GetFrames("CHAP") {
chapterFrame, ok := frame.(id3v2.ChapterFrame)
if ok {
au.Chapters = append(au.Chapters, &ChapterSegment{
au.Chapters = append(au.Chapters, &Chapter{
Title: chapterFrame.Title.Text,
Start: uint64(chapterFrame.StartTime.Seconds()),
})
Expand Down
10 changes: 5 additions & 5 deletions internal/cast/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

type ChapterSegment struct {
type Chapter struct {
Title string `json:"title"`
Start uint64 `json:"start"`
}
Expand Down Expand Up @@ -34,11 +34,11 @@ func convertStringToStart(str string) (uint64, error) {
return h*3600 + m*60 + s, nil
}

func (chs *ChapterSegment) String() string {
func (chs *Chapter) String() string {
return fmt.Sprintf("%s %s", convertStartToString(chs.Start), chs.Title)
}

func (chs *ChapterSegment) UnmarshalYAML(b []byte) error {
func (chs *Chapter) UnmarshalYAML(b []byte) error {
str := strings.TrimSpace(string(b))
stuff := strings.SplitN(str, " ", 2)
if len(stuff) != 2 {
Expand All @@ -48,13 +48,13 @@ func (chs *ChapterSegment) UnmarshalYAML(b []byte) error {
if err != nil {
return fmt.Errorf("invalid chapter format: %s, %w", str, err)
}
*chs = ChapterSegment{
*chs = Chapter{
Title: stuff[1],
Start: start,
}
return nil
}

func (chs *ChapterSegment) MarshalYAML() ([]byte, error) {
func (chs *Chapter) MarshalYAML() ([]byte, error) {
return []byte(chs.String()), nil
}
18 changes: 9 additions & 9 deletions internal/cast/chapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ func TestChapterSegment_MarshalYAML(t *testing.T) {
tests := []struct {
name string
chapter struct {
Segment *cast.ChapterSegment
Segment *cast.Chapter
}
want string
}{{
name: "simple",
chapter: struct{ Segment *cast.ChapterSegment }{
Segment: &cast.ChapterSegment{
chapter: struct{ Segment *cast.Chapter }{
Segment: &cast.Chapter{
Title: "Chapter 1",
Start: 0,
},
},
want: "segment: 0:00 Chapter 1\n",
}, {
name: "with hours",
chapter: struct{ Segment *cast.ChapterSegment }{
Segment: &cast.ChapterSegment{
chapter: struct{ Segment *cast.Chapter }{
Segment: &cast.Chapter{
Title: "Chapter 2",
Start: 3600,
},
Expand All @@ -55,19 +55,19 @@ func TestChapterSegment_UnmarshalYAML(t *testing.T) {
tests := []struct {
name string
input string
want cast.ChapterSegment
want cast.Chapter
wantErr bool
}{{
name: "simple",
input: "0:00 Chapter 1",
want: cast.ChapterSegment{
want: cast.Chapter{
Title: "Chapter 1",
Start: 0,
},
}, {
name: "with hours",
input: "1:00:00 Chapter 2",
want: cast.ChapterSegment{
want: cast.Chapter{
Title: "Chapter 2",
Start: 3600,
},
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestChapterSegment_UnmarshalYAML(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got cast.ChapterSegment
var got cast.Chapter
err := yaml.Unmarshal([]byte(tt.input), &got)
if err != nil {
if !tt.wantErr {
Expand Down
16 changes: 8 additions & 8 deletions internal/cast/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@ type Episode struct {
}

type EpisodeFrontMatter struct {
AudioFile string `yaml:"audio"`
Title string `yaml:"title"`
Date string `yaml:"date"`
Subtitle string `yaml:"subtitle"`
Chapters []*ChapterSegment `yaml:"chapters,omitempty"`
AudioFile string `yaml:"audio"`
Title string `yaml:"title"`
Date string `yaml:"date"`
Subtitle string `yaml:"subtitle"`
Chapters []*Chapter `yaml:"chapters,omitempty"`

audio *Audio
pubDate time.Time
Expand Down Expand Up @@ -422,9 +422,9 @@ const chaperTmplStr = `<ul class="chapters">
{{- end -}}</ul>
`

var chaptertmpl = template.Must(template.New("chapters").Parse(chaperTmplStr))
var chapterTmpl = template.Must(template.New("chapters").Parse(chaperTmplStr))

func buildChaptersBody(chapters []*ChapterSegment) (string, error) {
func buildChaptersBody(chapters []*Chapter) (string, error) {
data := []struct {
Title string
Start string
Expand All @@ -441,7 +441,7 @@ func buildChaptersBody(chapters []*ChapterSegment) (string, error) {
}

var buf bytes.Buffer
if err := chaptertmpl.Execute(&buf, chapters); err != nil {
if err := chapterTmpl.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
Expand Down

0 comments on commit d594695

Please sign in to comment.