diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2026-05-07 11:53:51 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2026-05-07 11:53:51 -0400 |
| commit | f2ee817ba1423b10b382f6b759a19184db8eeacc (patch) | |
| tree | 57d7660024b9b21d2a9ac80c351b2911b5c573e7 /cover.go | |
| parent | 09c91d21b083abc976a4cc6439899fb00438a630 (diff) | |
| download | lulu-f2ee817ba1423b10b382f6b759a19184db8eeacc.zip | |
refactor
Diffstat (limited to 'cover.go')
| -rw-r--r-- | cover.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/cover.go b/cover.go new file mode 100644 index 0000000..30be2d6 --- /dev/null +++ b/cover.go @@ -0,0 +1,55 @@ +package lulu + +import ( + "encoding/json" + "fmt" + "strconv" +) + +//go:generate go run github.com/yawnak/string-enumer -t Unit --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" +) + +// coverDimensionsReq is the json body of a /cover-dimensions/ request. +type coverDimensionsReq struct { + PkgId PkgId `json:"pod_package_id"` + NPages uint `json:"interior_page_count"` + Unit Unit `json:"unit"` +} + +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 +} |