A tool for archiving & converting scans of postcards, and information about them.
1package types
2
3import (
4 "fmt"
5 "slices"
6)
7
8type Flip string
9
10const (
11 FlipBook Flip = "book"
12 FlipLeftHand Flip = "left-hand"
13 FlipCalendar Flip = "calendar"
14 FlipRightHand Flip = "right-hand"
15 FlipNone Flip = "none"
16)
17
18var ValidFlips = []Flip{FlipBook, FlipCalendar, FlipLeftHand, FlipRightHand, FlipNone}
19
20// Heteroriented will be true if the card need to pivot about a diagonal axis for the front and back to remain upright.
21// the negation of this method is always whether the card is homoriented or not.
22func (flip Flip) IsHeteroriented() bool {
23 return flip == FlipLeftHand || flip == FlipRightHand
24}
25
26// IsValid will return false if the flip string isn't a known one
27func (flip Flip) IsValid() bool {
28 return slices.Contains(ValidFlips, flip)
29}
30
31func (flip Flip) Description() string {
32 switch flip {
33 case FlipBook:
34 return "vertical axis (like a book)"
35 case FlipLeftHand:
36 return "diagonal (up-right) axis (flipping with your left hand)"
37 case FlipCalendar:
38 return "horizontal axis (like a calendar)"
39 case FlipRightHand:
40 return "diagonal (down-right) axis (flipping with your right hand)"
41 case FlipNone:
42 return "one sided"
43 default:
44 panic("unknown flip axis")
45 }
46}
47
48// Checks whether the given Flip is appropriate for sides with the provided dimensions
49func CheckFlip(front, back Size, flip Flip) error {
50 fo := front.Orientation()
51 bo := back.Orientation()
52 switch {
53 case fo == OrientationSquare:
54 // Any flip is permissable
55 return nil
56 case fo == bo:
57 if !flip.IsHeteroriented() {
58 return nil
59 }
60 return fmt.Errorf("the front and back images are both %s, but '%s' flip only works with sides of different orientations. Try '%s' or '%s'.", fo, flip, FlipBook, FlipCalendar)
61 case fo != bo:
62 if flip.IsHeteroriented() {
63 return nil
64 }
65 return fmt.Errorf("the front (%s) and back (%s) images aren't the same orientation, but '%s' flip only works with sides of the same orientation. Try '%s' or '%s'.", fo, bo, flip, FlipLeftHand, FlipRightHand)
66 }
67
68 return nil
69}