packages feed

ipldm-1.0.0.0: ipld/IPLD/DM/Decoder.hs

-- | Module      : IPLD.DM.Decoder
--   Description : Type-class of IPLD data model decoders
--   Copyright   : Zoey McBride (c) 2026
--   License     : AGPL-3.0-or-later
--   Maintainer  : zoeymcbride@mailbox.org
--   Stability   : experimental
module IPLD.DM.Decoder
  ( -- * Class of data model decoders.
    Decoder (..),

    -- * Method signature for decoder implementers.
    DecoderMethod,
  )
where

import Control.Applicative (Alternative (..))
import IPLD.DM qualified as DM

-- | Method signature for DM decoders. Decodes some `item` from `tokens` into
-- the `ok` functor and the `result` bifunctor, storing the decoded item with
-- the remaining tokens.
type DecoderMethod item tokens ok result = tokens -> ok (result item tokens)

-- | Abstract class for developing DM decoders within some `codec`. Expects
-- implementations for each IPLD Kind, and provides a concrete method for
-- parsing a whole data model with the `decoder` method into a Node.
class (Alternative codec) => Decoder codec where
  -- | Decodes a object into the IPLD data model.
  mapKind :: codec DM.Node

  -- | Decodes a list object into the IPLD data model.
  listKind :: codec DM.Node

  -- | Decodes a ResourceKind aka link kind in IPLD.
  resourceKind :: codec DM.Node

  -- | Decodes a UTF-8 Text into IPLD data model.
  stringKind :: codec DM.Node

  -- | Decodes a ByteString into IPLD data model.
  bytesKind :: codec DM.Node

  -- | Decodes Integer values into IPLD whole-numbers.
  intKind :: codec DM.Node

  -- | Decodes a IEEE754 value into a valid IPLD floating point number.
  floatKind :: codec DM.Node

  -- | Decodes IPLD booleans.
  boolKind :: codec DM.Node

  -- | Decodes IPLD null values.
  nullKind :: codec DM.Node

  -- | Decodes DAG from the format.
  decoder :: codec DM.Node
  decoder =
    mapKind
      <|> listKind
      <|> resourceKind
      <|> bytesKind
      <|> stringKind
      <|> floatKind
      <|> intKind
      <|> boolKind
      <|> nullKind