Something went wrong. Try again.
A simple convention app I decided to make for myself and share dhershman.tngl.io/con-goer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421module Codec exposing (Stored, decode, decodeTransfer, encode, transferText)
{-| Turning the saved half of the model into JSON and back-}
import Data exposing ( ConStatus(..) , Condition(..) , Convention , ConventionId(..) , Estimate , Item , ItemId(..) , ItemStatus(..) , Priority(..) , Purchase , Section )import Date exposing (Date)import Json.Decode as Decode exposing (Decoder)import Json.Encode as Encode
{-| The saved shape. Bumping version in encode is the hook for migrationsif the format ever has to change-}type alias Stored = { conventions : List Convention , active : Maybe ConventionId , nextConventionId : Int }
currentVersion : IntcurrentVersion = 1
-- ENCODE
encode : Stored -> Encode.Valueencode stored = Encode.object [ ( "version", Encode.int currentVersion ) , ( "conventions", Encode.list encodeConvention stored.conventions ) , ( "active", encodeMaybe encodeConventionId stored.active ) , ( "nextConventionId", Encode.int stored.nextConventionId ) ]
{-| The text that travels between devices, as a file or as a paste-}transferText : List Convention -> StringtransferText conventions = Encode.encode 0 <| Encode.object [ ( "version", Encode.int currentVersion ) , ( "conventions", Encode.list encodeConvention conventions ) ]
encodeConvention : Convention -> Encode.ValueencodeConvention convention = Encode.object [ ( "id", encodeConventionId convention.id ) , ( "name", Encode.string convention.name ) , ( "date", Encode.string (Date.toIsoString convention.date) ) , ( "venue", encodeMaybe Encode.string convention.venue ) , ( "budgetCents", Encode.int convention.budgetCents ) , ( "sections", Encode.list encodeSection convention.sections ) , ( "status", encodeConStatus convention.status ) , ( "nextItemId", Encode.int convention.nextItemId ) ]
encodeSection : Section -> Encode.ValueencodeSection section = Encode.object [ ( "name", Encode.string section.name ) , ( "items", Encode.list encodeItem section.items ) ]
encodeItem : Item -> Encode.ValueencodeItem item = Encode.object [ ( "id", encodeItemId item.id ) , ( "name", Encode.string item.name ) , ( "priority", Encode.string (priorityTag item.priority) ) , ( "estimate", encodeEstimate item.estimate ) , ( "status", encodeItemStatus item.status ) ]
encodeEstimate : Estimate -> Encode.ValueencodeEstimate estimate = Encode.object [ ( "lowCents", Encode.int estimate.lowCents ) , ( "highCents", Encode.int estimate.highCents ) ]
encodeItemStatus : ItemStatus -> Encode.ValueencodeItemStatus status = case status of Hunting -> Encode.object [ ( "tag", Encode.string "hunting" ) ]
Passed -> Encode.object [ ( "tag", Encode.string "passed" ) ]
Acquired purchase -> Encode.object [ ( "tag", Encode.string "acquired" ) , ( "purchase", encodePurchase purchase ) ]
encodePurchase : Purchase -> Encode.ValueencodePurchase purchase = Encode.object [ ( "paidCents", Encode.int purchase.paidCents ) , ( "quantity", Encode.int purchase.quantity ) , ( "condition", Encode.string (conditionTag purchase.condition) ) , ( "booth", encodeMaybe Encode.string purchase.booth ) , ( "note", Encode.string purchase.note ) ]
encodeConventionId : ConventionId -> Encode.ValueencodeConventionId (ConventionId raw) = Encode.int raw
encodeItemId : ItemId -> Encode.ValueencodeItemId (ItemId raw) = Encode.int raw
encodeMaybe : (a -> Encode.Value) -> Maybe a -> Encode.ValueencodeMaybe encoder maybe = case maybe of Just value -> encoder value
Nothing -> Encode.null
priorityTag : Priority -> StringpriorityTag priority = case priority of MustFind -> "mustFind"
Wanted -> "wanted"
IfCheap -> "ifCheap"
conditionTag : Condition -> StringconditionTag condition = case condition of Untested -> "untested"
Tested -> "tested"
Boxed -> "boxed"
encodeConStatus : ConStatus -> Encode.ValueencodeConStatus status = Encode.string <| case status of Upcoming -> "upcoming"
OnTheFloor -> "onTheFloor"
Closed -> "closed"
-- DECODE
decode : Decoder Storeddecode = Decode.map3 Stored (Decode.field "conventions" (Decode.list conventionDecoder)) (Decode.field "active" (Decode.nullable conventionIdDecoder)) (Decode.field "nextConventionId" Decode.int)
{-| Conventions out of a file, or a plain-English reason why not
This takes a String and returns a String error rather than handing back aDecoder, because some problems are not decoder errors
-}decodeTransfer : String -> Result String (List Convention)decodeTransfer raw = let versionDecoder = Decode.oneOf [ Decode.field "version" Decode.int , Decode.succeed currentVersion ] in case Decode.decodeString versionDecoder raw of Err _ -> Err "That does not look like con-goer data. Check that the whole thing was copied."
Ok version -> if version > currentVersion then Err ("That file came from a newer version of con-goer (format " ++ String.fromInt version ++ "; this app reads " ++ String.fromInt currentVersion ++ ")." )
else Decode.decodeString (Decode.field "conventions" (Decode.list conventionDecoder)) raw |> Result.mapError Decode.errorToString
conventionDecoder : Decoder ConventionconventionDecoder = Decode.map8 Convention (Decode.field "id" conventionIdDecoder) (Decode.field "name" Decode.string) (Decode.field "date" dateDecoder) (Decode.field "venue" (Decode.nullable Decode.string)) (Decode.field "budgetCents" Decode.int) (Decode.field "sections" (Decode.list sectionDecoder)) (Decode.field "status" conStatusDecoder) (Decode.field "nextItemId" Decode.int) |> Decode.map repairNextItemId
{-| Where an ISO string on disk becomes a real Date, or the whole loadfails
We fail loudly because Main already treats a failed decaodeas a "say so and do not overwrite" situation
-}dateDecoder : Decoder DatedateDecoder = Decode.string |> Decode.andThen (\raw -> case Date.fromIsoString raw of Ok date -> Decode.succeed date
Err reason -> Decode.fail ("Bad date " ++ raw ++ ": " ++ reason) )
{-| Guard against a stale or hand-edited id counter
If nextItemId is not actually past every id in the file, the next item addedwould collide with an existing one and the two would update as a pair.
Saves the headache of debugging later tbh
-}repairNextItemId : Convention -> ConventionrepairNextItemId convention = let highestUsed = Data.allItems convention |> List.map (\item -> rawItemId item.id) |> List.maximum |> Maybe.withDefault 0 in { convention | nextItemId = max convention.nextItemId (highestUsed + 1) }
rawItemId : ItemId -> IntrawItemId (ItemId raw) = raw
sectionDecoder : Decoder SectionsectionDecoder = Decode.map2 Section (Decode.field "name" Decode.string) (Decode.field "items" (Decode.list itemDecoder))
itemDecoder : Decoder ItemitemDecoder = Decode.map5 Item (Decode.field "id" itemIdDecoder) (Decode.field "name" Decode.string) (Decode.field "priority" priorityDecoder) (Decode.field "estimate" estimateDecoder) (Decode.field "status" itemStatusDecoder)
estimateDecoder : Decoder EstimateestimateDecoder = Decode.map2 Estimate (Decode.field "lowCents" Decode.int) (Decode.field "highCents" Decode.int)
itemStatusDecoder : Decoder ItemStatusitemStatusDecoder = Decode.field "tag" Decode.string |> Decode.andThen (\tag -> case tag of "hunting" -> Decode.succeed Hunting
"passed" -> Decode.succeed Passed
"acquired" -> Decode.map Acquired (Decode.field "purchase" purchaseDecoder)
other -> Decode.fail ("Unknown item status: " ++ other) )
purchaseDecoder : Decoder PurchasepurchaseDecoder = Decode.map5 Purchase (Decode.field "paidCents" Decode.int) (Decode.field "quantity" Decode.int) (Decode.field "condition" conditionDecoder) (Decode.field "booth" (Decode.nullable Decode.string)) (Decode.field "note" Decode.string)
priorityDecoder : Decoder PrioritypriorityDecoder = Decode.string |> Decode.andThen (\tag -> case tag of "mustFind" -> Decode.succeed MustFind
"wanted" -> Decode.succeed Wanted
"ifCheap" -> Decode.succeed IfCheap
other -> Decode.fail ("Unknown priority: " ++ other) )
conditionDecoder : Decoder ConditionconditionDecoder = Decode.string |> Decode.andThen (\tag -> case tag of "untested" -> Decode.succeed Untested
"tested" -> Decode.succeed Tested
"boxed" -> Decode.succeed Boxed
other -> Decode.fail ("Unknown condition: " ++ other) )
conStatusDecoder : Decoder ConStatusconStatusDecoder = Decode.string |> Decode.andThen (\tag -> case tag of "upcoming" -> Decode.succeed Upcoming
"onTheFloor" -> Decode.succeed OnTheFloor
"closed" -> Decode.succeed Closed
other -> Decode.fail ("Unknown convention status: " ++ other) )
conventionIdDecoder : Decoder ConventionIdconventionIdDecoder = Decode.map ConventionId Decode.int
itemIdDecoder : Decoder ItemIditemIdDecoder = Decode.map ItemId Decode.int