github.com/puellanivis/breton@v0.2.16/lib/net/hls/m3u8/resolution.go (about)

     1  package m3u8
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strconv"
     7  )
     8  
     9  // Resolution represents a video resolution of Width x Height.
    10  type Resolution struct {
    11  	Width  int
    12  	Height int
    13  }
    14  
    15  // TextUnmarshal implements encoding.TextUnmarshaler.
    16  func (r *Resolution) TextUnmarshal(value []byte) error {
    17  	fields := bytes.Split(value, []byte{'x'})
    18  
    19  	if len(fields) != 2 {
    20  		return fmt.Errorf("invalid resolution: %q", value)
    21  	}
    22  
    23  	width, err := strconv.Atoi(string(fields[0]))
    24  	if err != nil {
    25  		return fmt.Errorf("invalid width: %q: %v", fields[0], err)
    26  	}
    27  
    28  	height, err := strconv.Atoi(string(fields[1]))
    29  	if err != nil {
    30  		return fmt.Errorf("invalid height: %q: %v", fields[1], err)
    31  	}
    32  
    33  	r.Width = width
    34  	r.Height = height
    35  
    36  	return nil
    37  }
    38  
    39  // TextMarshal implements encoding.TextMarshaler.
    40  func (r *Resolution) TextMarshal() ([]byte, error) {
    41  	return []byte(r.String()), nil
    42  }
    43  
    44  // String implements fmt.Stringer.
    45  func (r *Resolution) String() string {
    46  	return fmt.Sprintf("%dx%d", r.Width, r.Height)
    47  }