aboutsummaryrefslogtreecommitdiffstats
path: root/cover.go
blob: dd6a2120cf742329aaac46f3303d8ffa21933511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package lulu

import (
	"encoding/json"
	"fmt"
	"strconv"
)

//go:generate go run github.com/sam-rba/string-enumer -t Unit -t CoverValidationStatus --text -o ./cover_gen.go .

// Unit is a unit of length measurement.
type Unit string

const (
	Points      Unit = "pt"
	Millimeters Unit = "mm"
	Inches      Unit = "inch"
)

// CoverValidationStatus is the status of a validation job for a cover
// file that is running on the server.
type CoverValidationStatus string

const (
	CoverStatusNull        CoverValidationStatus = "NULL"        // file validation is not started yet
	CoverStatusNormalizing CoverValidationStatus = "NORMALIZING" // file validation is still running
	CoverStatusNormalized  CoverValidationStatus = "NORMALIZED"  // file validation finished without any errors
	CoverStatusError       CoverValidationStatus = "ERROR"       // file is invalid, list of errors is included in the response
)

func (s CoverValidationStatus) IsFinal() bool {
	switch s {
	case CoverStatusNormalized, CoverStatusError:
		return true
	}
	return false
}

// coverDimensionsReq is the json body of a /cover-dimensions/ request.
type coverDimensionsReq struct {
	Mfg    PkgId `json:"pod_package_id"`
	NPages uint  `json:"interior_page_count"`
	Unit   Unit  `json:"unit"`
}

// validateCoverReq is the json body of a /validate-cover/ request.
type validateCoverReq struct {
	SrcUrl string `json:"source_url"`
	Mfg    PkgId  `json:"pod_package_id"`
	NPages uint   `json:"interior_page_count"`
}

type CoverDimensions struct {
	Width, Height float64
	Unit          Unit
}

func (cd *CoverDimensions) UnmarshalJSON(data []byte) error {
	s := string(data)
	var alias struct {
		Width, Height string
		Unit          Unit
	}
	if err := json.Unmarshal(data, &alias); err != nil {
		return fmt.Errorf("malformed %T: %q: %w", cd, s, err)
	}

	w, err := strconv.ParseFloat(alias.Width, 64)
	if err != nil {
		return fmt.Errorf("malformed %T.Width: %q: %w", cd, s, err)
	}
	h, err := strconv.ParseFloat(alias.Height, 64)
	if err != nil {
		return fmt.Errorf("malformed %T.Height: %q: %w", cd, s, err)
	}

	cd.Width = w
	cd.Height = h
	cd.Unit = alias.Unit
	return nil
}

func (cd CoverDimensions) String() string {
	return fmt.Sprintf("%0.3f x %0.3f %s", cd.Width, cd.Height, cd.Unit)
}

// CoverValidation contains the validation status of a cover file.
type CoverValidation struct {
	Id     uint     `json:"id"`
	SrcUrl string   `json:"source_url"`
	NPages uint     `json:"page_count"`
	Errors []string `json:"errors"`
	Status CoverValidationStatus
}