module Rattletrap.Type.CompressedWordVector where

import qualified Rattletrap.BitGet as BitGet
import qualified Rattletrap.BitPut as BitPut
import qualified Rattletrap.Schema as Schema
import qualified Rattletrap.Type.CompressedWord as CompressedWord
import qualified Rattletrap.Utility.Json as Json

data CompressedWordVector = CompressedWordVector
  { CompressedWordVector -> CompressedWord
x :: CompressedWord.CompressedWord
  , CompressedWordVector -> CompressedWord
y :: CompressedWord.CompressedWord
  , CompressedWordVector -> CompressedWord
z :: CompressedWord.CompressedWord
  }
  deriving (CompressedWordVector -> CompressedWordVector -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CompressedWordVector -> CompressedWordVector -> Bool
$c/= :: CompressedWordVector -> CompressedWordVector -> Bool
== :: CompressedWordVector -> CompressedWordVector -> Bool
$c== :: CompressedWordVector -> CompressedWordVector -> Bool
Eq, Int -> CompressedWordVector -> ShowS
[CompressedWordVector] -> ShowS
CompressedWordVector -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CompressedWordVector] -> ShowS
$cshowList :: [CompressedWordVector] -> ShowS
show :: CompressedWordVector -> String
$cshow :: CompressedWordVector -> String
showsPrec :: Int -> CompressedWordVector -> ShowS
$cshowsPrec :: Int -> CompressedWordVector -> ShowS
Show)

instance Json.FromJSON CompressedWordVector where
  parseJSON :: Value -> Parser CompressedWordVector
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
Json.withObject String
"CompressedWordVector" forall a b. (a -> b) -> a -> b
$ \Object
object -> do
    CompressedWord
x <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"x"
    CompressedWord
y <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"y"
    CompressedWord
z <- forall value. FromJSON value => Object -> String -> Parser value
Json.required Object
object String
"z"
    forall (f :: * -> *) a. Applicative f => a -> f a
pure CompressedWordVector { CompressedWord
x :: CompressedWord
x :: CompressedWord
x, CompressedWord
y :: CompressedWord
y :: CompressedWord
y, CompressedWord
z :: CompressedWord
z :: CompressedWord
z }

instance Json.ToJSON CompressedWordVector where
  toJSON :: CompressedWordVector -> Value
toJSON CompressedWordVector
a =
    [(Key, Value)] -> Value
Json.object [forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"x" forall a b. (a -> b) -> a -> b
$ CompressedWordVector -> CompressedWord
x CompressedWordVector
a, forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"y" forall a b. (a -> b) -> a -> b
$ CompressedWordVector -> CompressedWord
y CompressedWordVector
a, forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"z" forall a b. (a -> b) -> a -> b
$ CompressedWordVector -> CompressedWord
z CompressedWordVector
a]

schema :: Schema.Schema
schema :: Schema
schema = String -> Value -> Schema
Schema.named String
"compressed-word-vector" forall a b. (a -> b) -> a -> b
$ [((Key, Value), Bool)] -> Value
Schema.object
  [ (forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"x" forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
CompressedWord.schema, Bool
True)
  , (forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"y" forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
CompressedWord.schema, Bool
True)
  , (forall value pair.
(ToJSON value, KeyValue pair) =>
String -> value -> pair
Json.pair String
"z" forall a b. (a -> b) -> a -> b
$ Schema -> Value
Schema.ref Schema
CompressedWord.schema, Bool
True)
  ]

bitPut :: CompressedWordVector -> BitPut.BitPut
bitPut :: CompressedWordVector -> BitPut
bitPut CompressedWordVector
compressedWordVector =
  CompressedWord -> BitPut
CompressedWord.bitPut (CompressedWordVector -> CompressedWord
x CompressedWordVector
compressedWordVector)
    forall a. Semigroup a => a -> a -> a
<> CompressedWord -> BitPut
CompressedWord.bitPut (CompressedWordVector -> CompressedWord
y CompressedWordVector
compressedWordVector)
    forall a. Semigroup a => a -> a -> a
<> CompressedWord -> BitPut
CompressedWord.bitPut (CompressedWordVector -> CompressedWord
z CompressedWordVector
compressedWordVector)

bitGet :: BitGet.BitGet CompressedWordVector
bitGet :: BitGet CompressedWordVector
bitGet = do
  CompressedWord
x <- Word -> BitGet CompressedWord
CompressedWord.bitGet Word
limit
  CompressedWord
y <- Word -> BitGet CompressedWord
CompressedWord.bitGet Word
limit
  CompressedWord
z <- Word -> BitGet CompressedWord
CompressedWord.bitGet Word
limit
  forall (f :: * -> *) a. Applicative f => a -> f a
pure CompressedWordVector { CompressedWord
x :: CompressedWord
x :: CompressedWord
x, CompressedWord
y :: CompressedWord
y :: CompressedWord
y, CompressedWord
z :: CompressedWord
z :: CompressedWord
z }

limit :: Word
limit :: Word
limit = Word
65536