{- |
Module      : App.URLDecoder
Copyright   : (c) Leon Vatthauer, 2026
License     : GPL-3
Maintainer  : Leon Vatthauer <leon.vatthauer@fau.de>
Stability   : experimental
Portability : non-portable (ghc-wasm-meta)

This module defines function for encoding a t'Proof' as URL-safe Base64
and also decoding back to a t'Proof'.
-}
module App.URLDecoder where

import Data.Base64.Types (extractBase64)
import Data.Text.Encoding.Base64.URL qualified as Base64URL
import Fitch.Proof (Proof)
import Parser.IncompleteProof (parseIncompleteProof, safeParsePrint)

-- | Encodes t'Proof' to a URL-safe 'Text'
encodeForUrl :: Proof -> Text
encodeForUrl :: Proof -> Text
encodeForUrl = Base64 'UrlPadded Text -> Text
forall (k :: Alphabet) a. Base64 k a -> a
extractBase64 (Base64 'UrlPadded Text -> Text)
-> (Proof -> Base64 'UrlPadded Text) -> Proof -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Base64 'UrlPadded Text
Base64URL.encodeBase64 (Text -> Base64 'UrlPadded Text)
-> (Proof -> Text) -> Proof -> Base64 'UrlPadded Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Proof -> Text
safeParsePrint

-- | Decodes a URL-safe 'Text' back to t'Proof'
decodeFromUrl :: Text -> Maybe Proof
decodeFromUrl :: Text -> Maybe Proof
decodeFromUrl = Text -> Maybe Proof
parseIncompleteProof (Text -> Maybe Proof) -> (Text -> Text) -> Text -> Maybe Proof
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
Base64URL.decodeBase64Lenient