module 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 migrations if the format ever has to change -} type alias Stored = { conventions : List Convention , active : Maybe ConventionId , nextConventionId : Int } currentVersion : Int currentVersion = 1 -- ENCODE encode : Stored -> Encode.Value encode 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 -> String transferText conventions = Encode.encode 0 <| Encode.object [ ( "version", Encode.int currentVersion ) , ( "conventions", Encode.list encodeConvention conventions ) ] encodeConvention : Convention -> Encode.Value encodeConvention 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.Value encodeSection section = Encode.object [ ( "name", Encode.string section.name ) , ( "items", Encode.list encodeItem section.items ) ] encodeItem : Item -> Encode.Value encodeItem 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.Value encodeEstimate estimate = Encode.object [ ( "lowCents", Encode.int estimate.lowCents ) , ( "highCents", Encode.int estimate.highCents ) ] encodeItemStatus : ItemStatus -> Encode.Value encodeItemStatus 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.Value encodePurchase 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.Value encodeConventionId (ConventionId raw) = Encode.int raw encodeItemId : ItemId -> Encode.Value encodeItemId (ItemId raw) = Encode.int raw encodeMaybe : (a -> Encode.Value) -> Maybe a -> Encode.Value encodeMaybe encoder maybe = case maybe of Just value -> encoder value Nothing -> Encode.null priorityTag : Priority -> String priorityTag priority = case priority of MustFind -> "mustFind" Wanted -> "wanted" IfCheap -> "ifCheap" conditionTag : Condition -> String conditionTag condition = case condition of Untested -> "untested" Tested -> "tested" Boxed -> "boxed" encodeConStatus : ConStatus -> Encode.Value encodeConStatus status = Encode.string <| case status of Upcoming -> "upcoming" OnTheFloor -> "onTheFloor" Closed -> "closed" -- DECODE decode : Decoder Stored decode = 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 a Decoder, 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 Convention conventionDecoder = 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 load fails We fail loudly because Main already treats a failed decaode as a "say so and do not overwrite" situation -} dateDecoder : Decoder Date dateDecoder = 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 added would collide with an existing one and the two would update as a pair. Saves the headache of debugging later tbh -} repairNextItemId : Convention -> Convention repairNextItemId 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 -> Int rawItemId (ItemId raw) = raw sectionDecoder : Decoder Section sectionDecoder = Decode.map2 Section (Decode.field "name" Decode.string) (Decode.field "items" (Decode.list itemDecoder)) itemDecoder : Decoder Item itemDecoder = 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 Estimate estimateDecoder = Decode.map2 Estimate (Decode.field "lowCents" Decode.int) (Decode.field "highCents" Decode.int) itemStatusDecoder : Decoder ItemStatus itemStatusDecoder = 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 Purchase purchaseDecoder = 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 Priority priorityDecoder = 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 Condition conditionDecoder = 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 ConStatus conStatusDecoder = 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 ConventionId conventionIdDecoder = Decode.map ConventionId Decode.int itemIdDecoder : Decoder ItemId itemIdDecoder = Decode.map ItemId Decode.int