{-# OPTIONS_HADDOCK not-home #-}
module FlatBuffers.Internal.Read where
import Control.Monad (join, (>=>))
import Data.Binary.Get (Get)
import Data.Binary.Get qualified as G
import Data.ByteString qualified as BS
import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Lazy qualified as BSL
import Data.ByteString.Lazy.Internal qualified as BSL
import Data.ByteString.Unsafe qualified as BSU
import Data.Coerce (coerce)
import Data.Functor ((<&>))
import Data.Int
import Data.List qualified as L
import Data.Text (Text)
import Data.Text.Encoding qualified as T
import Data.Text.Encoding.Error qualified as T
import Data.Word
import FlatBuffers.Internal.Constants
import FlatBuffers.Internal.FileIdentifier (FileIdentifier(..), HasFileIdentifier(..))
import FlatBuffers.Internal.Types
import Prelude hiding (drop, length, take)
type ReadError = String
newtype TableIndex = TableIndex { TableIndex -> Word16
unTableIndex :: Word16 }
deriving newtype (Int -> TableIndex -> ShowS
[TableIndex] -> ShowS
TableIndex -> ReadError
(Int -> TableIndex -> ShowS)
-> (TableIndex -> ReadError)
-> ([TableIndex] -> ShowS)
-> Show TableIndex
forall a.
(Int -> a -> ShowS) -> (a -> ReadError) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TableIndex -> ShowS
showsPrec :: Int -> TableIndex -> ShowS
$cshow :: TableIndex -> ReadError
show :: TableIndex -> ReadError
$cshowList :: [TableIndex] -> ShowS
showList :: [TableIndex] -> ShowS
Show, Integer -> TableIndex
TableIndex -> TableIndex
TableIndex -> TableIndex -> TableIndex
(TableIndex -> TableIndex -> TableIndex)
-> (TableIndex -> TableIndex -> TableIndex)
-> (TableIndex -> TableIndex -> TableIndex)
-> (TableIndex -> TableIndex)
-> (TableIndex -> TableIndex)
-> (TableIndex -> TableIndex)
-> (Integer -> TableIndex)
-> Num TableIndex
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: TableIndex -> TableIndex -> TableIndex
+ :: TableIndex -> TableIndex -> TableIndex
$c- :: TableIndex -> TableIndex -> TableIndex
- :: TableIndex -> TableIndex -> TableIndex
$c* :: TableIndex -> TableIndex -> TableIndex
* :: TableIndex -> TableIndex -> TableIndex
$cnegate :: TableIndex -> TableIndex
negate :: TableIndex -> TableIndex
$cabs :: TableIndex -> TableIndex
abs :: TableIndex -> TableIndex
$csignum :: TableIndex -> TableIndex
signum :: TableIndex -> TableIndex
$cfromInteger :: Integer -> TableIndex
fromInteger :: Integer -> TableIndex
Num)
newtype VOffset = VOffset { VOffset -> Word16
unVOffset :: Word16 }
deriving newtype (Int -> VOffset -> ShowS
[VOffset] -> ShowS
VOffset -> ReadError
(Int -> VOffset -> ShowS)
-> (VOffset -> ReadError) -> ([VOffset] -> ShowS) -> Show VOffset
forall a.
(Int -> a -> ShowS) -> (a -> ReadError) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VOffset -> ShowS
showsPrec :: Int -> VOffset -> ShowS
$cshow :: VOffset -> ReadError
show :: VOffset -> ReadError
$cshowList :: [VOffset] -> ShowS
showList :: [VOffset] -> ShowS
Show, Integer -> VOffset
VOffset -> VOffset
VOffset -> VOffset -> VOffset
(VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset)
-> (VOffset -> VOffset)
-> (VOffset -> VOffset)
-> (Integer -> VOffset)
-> Num VOffset
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: VOffset -> VOffset -> VOffset
+ :: VOffset -> VOffset -> VOffset
$c- :: VOffset -> VOffset -> VOffset
- :: VOffset -> VOffset -> VOffset
$c* :: VOffset -> VOffset -> VOffset
* :: VOffset -> VOffset -> VOffset
$cnegate :: VOffset -> VOffset
negate :: VOffset -> VOffset
$cabs :: VOffset -> VOffset
abs :: VOffset -> VOffset
$csignum :: VOffset -> VOffset
signum :: VOffset -> VOffset
$cfromInteger :: Integer -> VOffset
fromInteger :: Integer -> VOffset
Num, Num VOffset
Ord VOffset
(Num VOffset, Ord VOffset) => (VOffset -> Rational) -> Real VOffset
VOffset -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: VOffset -> Rational
toRational :: VOffset -> Rational
Real, Eq VOffset
Eq VOffset =>
(VOffset -> VOffset -> Ordering)
-> (VOffset -> VOffset -> Bool)
-> (VOffset -> VOffset -> Bool)
-> (VOffset -> VOffset -> Bool)
-> (VOffset -> VOffset -> Bool)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> Ord VOffset
VOffset -> VOffset -> Bool
VOffset -> VOffset -> Ordering
VOffset -> VOffset -> VOffset
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: VOffset -> VOffset -> Ordering
compare :: VOffset -> VOffset -> Ordering
$c< :: VOffset -> VOffset -> Bool
< :: VOffset -> VOffset -> Bool
$c<= :: VOffset -> VOffset -> Bool
<= :: VOffset -> VOffset -> Bool
$c> :: VOffset -> VOffset -> Bool
> :: VOffset -> VOffset -> Bool
$c>= :: VOffset -> VOffset -> Bool
>= :: VOffset -> VOffset -> Bool
$cmax :: VOffset -> VOffset -> VOffset
max :: VOffset -> VOffset -> VOffset
$cmin :: VOffset -> VOffset -> VOffset
min :: VOffset -> VOffset -> VOffset
Ord, Int -> VOffset
VOffset -> Int
VOffset -> [VOffset]
VOffset -> VOffset
VOffset -> VOffset -> [VOffset]
VOffset -> VOffset -> VOffset -> [VOffset]
(VOffset -> VOffset)
-> (VOffset -> VOffset)
-> (Int -> VOffset)
-> (VOffset -> Int)
-> (VOffset -> [VOffset])
-> (VOffset -> VOffset -> [VOffset])
-> (VOffset -> VOffset -> [VOffset])
-> (VOffset -> VOffset -> VOffset -> [VOffset])
-> Enum VOffset
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: VOffset -> VOffset
succ :: VOffset -> VOffset
$cpred :: VOffset -> VOffset
pred :: VOffset -> VOffset
$ctoEnum :: Int -> VOffset
toEnum :: Int -> VOffset
$cfromEnum :: VOffset -> Int
fromEnum :: VOffset -> Int
$cenumFrom :: VOffset -> [VOffset]
enumFrom :: VOffset -> [VOffset]
$cenumFromThen :: VOffset -> VOffset -> [VOffset]
enumFromThen :: VOffset -> VOffset -> [VOffset]
$cenumFromTo :: VOffset -> VOffset -> [VOffset]
enumFromTo :: VOffset -> VOffset -> [VOffset]
$cenumFromThenTo :: VOffset -> VOffset -> VOffset -> [VOffset]
enumFromThenTo :: VOffset -> VOffset -> VOffset -> [VOffset]
Enum, Enum VOffset
Real VOffset
(Real VOffset, Enum VOffset) =>
(VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> VOffset)
-> (VOffset -> VOffset -> (VOffset, VOffset))
-> (VOffset -> VOffset -> (VOffset, VOffset))
-> (VOffset -> Integer)
-> Integral VOffset
VOffset -> Integer
VOffset -> VOffset -> (VOffset, VOffset)
VOffset -> VOffset -> VOffset
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: VOffset -> VOffset -> VOffset
quot :: VOffset -> VOffset -> VOffset
$crem :: VOffset -> VOffset -> VOffset
rem :: VOffset -> VOffset -> VOffset
$cdiv :: VOffset -> VOffset -> VOffset
div :: VOffset -> VOffset -> VOffset
$cmod :: VOffset -> VOffset -> VOffset
mod :: VOffset -> VOffset -> VOffset
$cquotRem :: VOffset -> VOffset -> (VOffset, VOffset)
quotRem :: VOffset -> VOffset -> (VOffset, VOffset)
$cdivMod :: VOffset -> VOffset -> (VOffset, VOffset)
divMod :: VOffset -> VOffset -> (VOffset, VOffset)
$ctoInteger :: VOffset -> Integer
toInteger :: VOffset -> Integer
Integral, VOffset -> VOffset -> Bool
(VOffset -> VOffset -> Bool)
-> (VOffset -> VOffset -> Bool) -> Eq VOffset
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: VOffset -> VOffset -> Bool
== :: VOffset -> VOffset -> Bool
$c/= :: VOffset -> VOffset -> Bool
/= :: VOffset -> VOffset -> Bool
Eq)
newtype OffsetFromRoot = OffsetFromRoot Int32
deriving newtype (Int -> OffsetFromRoot -> ShowS
[OffsetFromRoot] -> ShowS
OffsetFromRoot -> ReadError
(Int -> OffsetFromRoot -> ShowS)
-> (OffsetFromRoot -> ReadError)
-> ([OffsetFromRoot] -> ShowS)
-> Show OffsetFromRoot
forall a.
(Int -> a -> ShowS) -> (a -> ReadError) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OffsetFromRoot -> ShowS
showsPrec :: Int -> OffsetFromRoot -> ShowS
$cshow :: OffsetFromRoot -> ReadError
show :: OffsetFromRoot -> ReadError
$cshowList :: [OffsetFromRoot] -> ShowS
showList :: [OffsetFromRoot] -> ShowS
Show, Integer -> OffsetFromRoot
OffsetFromRoot -> OffsetFromRoot
OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
(OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot)
-> (Integer -> OffsetFromRoot)
-> Num OffsetFromRoot
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
+ :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$c- :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
- :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$c* :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
* :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$cnegate :: OffsetFromRoot -> OffsetFromRoot
negate :: OffsetFromRoot -> OffsetFromRoot
$cabs :: OffsetFromRoot -> OffsetFromRoot
abs :: OffsetFromRoot -> OffsetFromRoot
$csignum :: OffsetFromRoot -> OffsetFromRoot
signum :: OffsetFromRoot -> OffsetFromRoot
$cfromInteger :: Integer -> OffsetFromRoot
fromInteger :: Integer -> OffsetFromRoot
Num, Num OffsetFromRoot
Ord OffsetFromRoot
(Num OffsetFromRoot, Ord OffsetFromRoot) =>
(OffsetFromRoot -> Rational) -> Real OffsetFromRoot
OffsetFromRoot -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: OffsetFromRoot -> Rational
toRational :: OffsetFromRoot -> Rational
Real, Eq OffsetFromRoot
Eq OffsetFromRoot =>
(OffsetFromRoot -> OffsetFromRoot -> Ordering)
-> (OffsetFromRoot -> OffsetFromRoot -> Bool)
-> (OffsetFromRoot -> OffsetFromRoot -> Bool)
-> (OffsetFromRoot -> OffsetFromRoot -> Bool)
-> (OffsetFromRoot -> OffsetFromRoot -> Bool)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> Ord OffsetFromRoot
OffsetFromRoot -> OffsetFromRoot -> Bool
OffsetFromRoot -> OffsetFromRoot -> Ordering
OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: OffsetFromRoot -> OffsetFromRoot -> Ordering
compare :: OffsetFromRoot -> OffsetFromRoot -> Ordering
$c< :: OffsetFromRoot -> OffsetFromRoot -> Bool
< :: OffsetFromRoot -> OffsetFromRoot -> Bool
$c<= :: OffsetFromRoot -> OffsetFromRoot -> Bool
<= :: OffsetFromRoot -> OffsetFromRoot -> Bool
$c> :: OffsetFromRoot -> OffsetFromRoot -> Bool
> :: OffsetFromRoot -> OffsetFromRoot -> Bool
$c>= :: OffsetFromRoot -> OffsetFromRoot -> Bool
>= :: OffsetFromRoot -> OffsetFromRoot -> Bool
$cmax :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
max :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$cmin :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
min :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
Ord, Int -> OffsetFromRoot
OffsetFromRoot -> Int
OffsetFromRoot -> [OffsetFromRoot]
OffsetFromRoot -> OffsetFromRoot
OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
OffsetFromRoot
-> OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
(OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot)
-> (Int -> OffsetFromRoot)
-> (OffsetFromRoot -> Int)
-> (OffsetFromRoot -> [OffsetFromRoot])
-> (OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot])
-> (OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot])
-> (OffsetFromRoot
-> OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot])
-> Enum OffsetFromRoot
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: OffsetFromRoot -> OffsetFromRoot
succ :: OffsetFromRoot -> OffsetFromRoot
$cpred :: OffsetFromRoot -> OffsetFromRoot
pred :: OffsetFromRoot -> OffsetFromRoot
$ctoEnum :: Int -> OffsetFromRoot
toEnum :: Int -> OffsetFromRoot
$cfromEnum :: OffsetFromRoot -> Int
fromEnum :: OffsetFromRoot -> Int
$cenumFrom :: OffsetFromRoot -> [OffsetFromRoot]
enumFrom :: OffsetFromRoot -> [OffsetFromRoot]
$cenumFromThen :: OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
enumFromThen :: OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
$cenumFromTo :: OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
enumFromTo :: OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
$cenumFromThenTo :: OffsetFromRoot
-> OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
enumFromThenTo :: OffsetFromRoot
-> OffsetFromRoot -> OffsetFromRoot -> [OffsetFromRoot]
Enum, Enum OffsetFromRoot
Real OffsetFromRoot
(Real OffsetFromRoot, Enum OffsetFromRoot) =>
(OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot)
-> (OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot))
-> (OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot))
-> (OffsetFromRoot -> Integer)
-> Integral OffsetFromRoot
OffsetFromRoot -> Integer
OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot)
OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
quot :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$crem :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
rem :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$cdiv :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
div :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$cmod :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
mod :: OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
$cquotRem :: OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot)
quotRem :: OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot)
$cdivMod :: OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot)
divMod :: OffsetFromRoot
-> OffsetFromRoot -> (OffsetFromRoot, OffsetFromRoot)
$ctoInteger :: OffsetFromRoot -> Integer
toInteger :: OffsetFromRoot -> Integer
Integral, OffsetFromRoot -> OffsetFromRoot -> Bool
(OffsetFromRoot -> OffsetFromRoot -> Bool)
-> (OffsetFromRoot -> OffsetFromRoot -> Bool) -> Eq OffsetFromRoot
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OffsetFromRoot -> OffsetFromRoot -> Bool
== :: OffsetFromRoot -> OffsetFromRoot -> Bool
$c/= :: OffsetFromRoot -> OffsetFromRoot -> Bool
/= :: OffsetFromRoot -> OffsetFromRoot -> Bool
Eq)
data Table a = Table
{ forall a. Table a -> ByteString
vtable :: !Position
, forall a. Table a -> PositionInfo
tablePos :: !PositionInfo
}
newtype Struct a = Struct
{ forall a. Struct a -> ByteString
structPos :: Position
}
data Union a
= Union !a
| UnionUnknown !Word8
type Position = ByteString
data PositionInfo = PositionInfo
{ PositionInfo -> ByteString
posRoot :: !Position
, PositionInfo -> ByteString
posCurrent :: !Position
, PositionInfo -> OffsetFromRoot
posOffsetFromRoot :: !OffsetFromRoot
}
class HasPosition a where
getPosition :: a -> Position
move :: Integral i => a -> i -> a
instance HasPosition ByteString where
getPosition :: ByteString -> ByteString
getPosition = ByteString -> ByteString
forall a. a -> a
id
move :: forall i. Integral i => ByteString -> i -> ByteString
move ByteString
bs i
offset = Int64 -> ByteString -> ByteString
BSL.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral @_ @Int64 i
offset) ByteString
bs
instance HasPosition PositionInfo where
getPosition :: PositionInfo -> ByteString
getPosition = PositionInfo -> ByteString
posCurrent
move :: forall i. Integral i => PositionInfo -> i -> PositionInfo
move PositionInfo{ByteString
OffsetFromRoot
posRoot :: PositionInfo -> ByteString
posCurrent :: PositionInfo -> ByteString
posOffsetFromRoot :: PositionInfo -> OffsetFromRoot
posRoot :: ByteString
posCurrent :: ByteString
posOffsetFromRoot :: OffsetFromRoot
..} i
offset =
PositionInfo
{ posRoot :: ByteString
posRoot = ByteString
posRoot
, posCurrent :: ByteString
posCurrent = ByteString -> i -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move ByteString
posCurrent i
offset
, posOffsetFromRoot :: OffsetFromRoot
posOffsetFromRoot = OffsetFromRoot
posOffsetFromRoot OffsetFromRoot -> OffsetFromRoot -> OffsetFromRoot
forall a. Num a => a -> a -> a
+ Int32 -> OffsetFromRoot
OffsetFromRoot (forall a b. (Integral a, Num b) => a -> b
fromIntegral @_ @Int32 i
offset)
}
decode :: ByteString -> Either ReadError (Table a)
decode :: forall a. ByteString -> Either ReadError (Table a)
decode ByteString
root = PositionInfo -> Either ReadError (Table a)
forall t. PositionInfo -> Either ReadError (Table t)
readTable PositionInfo
initialPos
where
initialPos :: PositionInfo
initialPos = ByteString -> ByteString -> OffsetFromRoot -> PositionInfo
PositionInfo ByteString
root ByteString
root OffsetFromRoot
0
checkFileIdentifier :: forall a. HasFileIdentifier a => ByteString -> Bool
checkFileIdentifier :: forall a. HasFileIdentifier a => ByteString -> Bool
checkFileIdentifier = FileIdentifier -> ByteString -> Bool
checkFileIdentifier' (forall a. HasFileIdentifier a => FileIdentifier
getFileIdentifier @a)
checkFileIdentifier' :: FileIdentifier -> ByteString -> Bool
checkFileIdentifier' :: FileIdentifier -> ByteString -> Bool
checkFileIdentifier' (FileIdentifier -> ByteString
unFileIdentifier -> ByteString
fileIdent) ByteString
bs =
ByteString
actualFileIdent ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> ByteString
BSL.fromStrict ByteString
fileIdent
where
actualFileIdent :: ByteString
actualFileIdent =
Int64 -> ByteString -> ByteString
BSL.take Int64
forall a. Num a => a
fileIdentifierSize (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Int64 -> ByteString -> ByteString
BSL.drop Int64
forall a. Num a => a
uoffsetSize (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$
ByteString
bs
newtype Positive a = Positive { forall a. Positive a -> a
getPositive :: a }
deriving newtype (Positive a -> Positive a -> Bool
(Positive a -> Positive a -> Bool)
-> (Positive a -> Positive a -> Bool) -> Eq (Positive a)
forall a. Eq a => Positive a -> Positive a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Positive a -> Positive a -> Bool
== :: Positive a -> Positive a -> Bool
$c/= :: forall a. Eq a => Positive a -> Positive a -> Bool
/= :: Positive a -> Positive a -> Bool
Eq, Int -> Positive a -> ShowS
[Positive a] -> ShowS
Positive a -> ReadError
(Int -> Positive a -> ShowS)
-> (Positive a -> ReadError)
-> ([Positive a] -> ShowS)
-> Show (Positive a)
forall a. Show a => Int -> Positive a -> ShowS
forall a. Show a => [Positive a] -> ShowS
forall a. Show a => Positive a -> ReadError
forall a.
(Int -> a -> ShowS) -> (a -> ReadError) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Positive a -> ShowS
showsPrec :: Int -> Positive a -> ShowS
$cshow :: forall a. Show a => Positive a -> ReadError
show :: Positive a -> ReadError
$cshowList :: forall a. Show a => [Positive a] -> ShowS
showList :: [Positive a] -> ShowS
Show)
{-# INLINE positive #-}
positive :: (Num a, Ord a) => a -> Maybe (Positive a)
positive :: forall a. (Num a, Ord a) => a -> Maybe (Positive a)
positive a
n = if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
0 then Positive a -> Maybe (Positive a)
forall a. a -> Maybe a
Just (a -> Positive a
forall a. a -> Positive a
Positive a
n) else Maybe (Positive a)
forall a. Maybe a
Nothing
{-# INLINE moveToElem #-}
moveToElem :: HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem :: forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem pos
pos Int32
elemSize Int32
ix =
pos -> Int32 -> pos
forall i. Integral i => pos -> i -> pos
forall a i. (HasPosition a, Integral i) => a -> i -> a
move pos
pos (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
elemSize)
{-# INLINE checkIndexBounds #-}
checkIndexBounds :: Int32 -> Int32 -> Int32
checkIndexBounds :: Int32 -> Int32 -> Int32
checkIndexBounds Int32
ix Int32
length
| Int32
ix Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
0 = ReadError -> Int32
forall a. HasCallStack => ReadError -> a
error (ReadError
"FlatBuffers.Internal.Read.index: negative index: " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int32 -> ReadError
forall a. Show a => a -> ReadError
show Int32
ix)
| Int32
ix Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int32
length = ReadError -> Int32
forall a. HasCallStack => ReadError -> a
error (ReadError
"FlatBuffers.Internal.Read.index: index too large: " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int32 -> ReadError
forall a. Show a => a -> ReadError
show Int32
ix)
| Bool
otherwise = Int32
ix
{-# INLINE inlineVectorToList #-}
inlineVectorToList :: Get a -> Int32 -> Position -> Either ReadError [a]
inlineVectorToList :: forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get a
get Int32
len ByteString
pos =
ByteString -> Get [a] -> Either ReadError [a]
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos (Get [a] -> Either ReadError [a])
-> Get [a] -> Either ReadError [a]
forall a b. (a -> b) -> a -> b
$
[Get a] -> Get [a]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence ([Get a] -> Get [a]) -> [Get a] -> Get [a]
forall a b. (a -> b) -> a -> b
$ Int -> Get a -> [Get a]
forall a. Int -> a -> [a]
L.replicate (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int Int32
len) Get a
get
clamp :: Int32 -> Int32 -> Int32
clamp :: Int32 -> Int32 -> Int32
clamp Int32
n Int32
upperBound = Int32
n Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
`min` Int32
upperBound Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
`max` Int32
0
class VectorElement a where
data Vector a
length :: Vector a -> Int32
unsafeIndex :: Vector a -> Int32 -> Either ReadError a
toList :: Vector a -> Either ReadError [a]
take :: Int32 -> Vector a -> Vector a
drop :: Int32 -> Vector a -> Vector a
index :: VectorElement a => Vector a -> Int32 -> Either ReadError a
index :: forall a.
VectorElement a =>
Vector a -> Int32 -> Either ReadError a
index Vector a
vec Int32
ix = Vector a -> Int32 -> Either ReadError a
forall a.
VectorElement a =>
Vector a -> Int32 -> Either ReadError a
unsafeIndex Vector a
vec (Int32 -> Either ReadError a)
-> (Int32 -> Int32) -> Int32 -> Either ReadError a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int32 -> Int32
checkIndexBounds Int32
ix (Int32 -> Either ReadError a) -> Int32 -> Either ReadError a
forall a b. (a -> b) -> a -> b
$ Vector a -> Int32
forall a. VectorElement a => Vector a -> Int32
length Vector a
vec
toLazyByteString :: Vector Word8 -> ByteString
toLazyByteString :: Vector Word8 -> ByteString
toLazyByteString (VectorWord8 Int32
len ByteString
pos) =
Int64 -> ByteString -> ByteString
BSL.take (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int64 Int32
len) ByteString
pos
instance VectorElement Word8 where
data Vector Word8 = VectorWord8 !Int32 !Position
length :: Vector Word8 -> Int32
length (VectorWord8 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Word8 -> Int32 -> Either ReadError Word8
unsafeIndex (VectorWord8 Int32
_ ByteString
pos) = ByteString -> Int32 -> Either ReadError Word8
byteStringSafeIndex ByteString
pos
take :: Int32 -> Vector Word8 -> Vector Word8
take Int32
n (VectorWord8 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word8
VectorWord8 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Word8 -> Vector Word8
drop Int32
n (VectorWord8 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word8
VectorWord8 (Int32 -> Int32 -> Int32
clamp (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n) Int32
len) (Int64 -> ByteString -> ByteString
BSL.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int64 Int32
n) ByteString
pos)
toList :: Vector Word8 -> Either ReadError [Word8]
toList = [Word8] -> Either ReadError [Word8]
forall a b. b -> Either a b
Right ([Word8] -> Either ReadError [Word8])
-> (Vector Word8 -> [Word8])
-> Vector Word8
-> Either ReadError [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BSL.unpack (ByteString -> [Word8])
-> (Vector Word8 -> ByteString) -> Vector Word8 -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Word8 -> ByteString
toLazyByteString
instance VectorElement Word16 where
data Vector Word16 = VectorWord16 !Int32 !Position
length :: Vector Word16 -> Int32
length (VectorWord16 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Word16 -> Int32 -> Either ReadError Word16
unsafeIndex (VectorWord16 Int32
_ ByteString
pos) = ByteString -> Either ReadError Word16
forall a. HasPosition a => a -> Either ReadError Word16
readWord16 (ByteString -> Either ReadError Word16)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word16Size
take :: Int32 -> Vector Word16 -> Vector Word16
take Int32
n (VectorWord16 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word16
VectorWord16 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Word16 -> Vector Word16
drop Int32
n (VectorWord16 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word16
VectorWord16 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word16Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Word16 -> Either ReadError [Word16]
toList (VectorWord16 Int32
len ByteString
pos) = Get Word16 -> Int32 -> ByteString -> Either ReadError [Word16]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Word16
G.getWord16le Int32
len ByteString
pos
instance VectorElement Word32 where
data Vector Word32 = VectorWord32 !Int32 !Position
length :: Vector Word32 -> Int32
length (VectorWord32 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Word32 -> Int32 -> Either ReadError Word32
unsafeIndex (VectorWord32 Int32
_ ByteString
pos) = ByteString -> Either ReadError Word32
forall a. HasPosition a => a -> Either ReadError Word32
readWord32 (ByteString -> Either ReadError Word32)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word32Size
take :: Int32 -> Vector Word32 -> Vector Word32
take Int32
n (VectorWord32 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word32
VectorWord32 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Word32 -> Vector Word32
drop Int32
n (VectorWord32 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word32
VectorWord32 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word32Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Word32 -> Either ReadError [Word32]
toList (VectorWord32 Int32
len ByteString
pos) = Get Word32 -> Int32 -> ByteString -> Either ReadError [Word32]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Word32
G.getWord32le Int32
len ByteString
pos
instance VectorElement Word64 where
data Vector Word64 = VectorWord64 !Int32 !Position
length :: Vector Word64 -> Int32
length (VectorWord64 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Word64 -> Int32 -> Either ReadError Word64
unsafeIndex (VectorWord64 Int32
_ ByteString
pos) = ByteString -> Either ReadError Word64
forall a. HasPosition a => a -> Either ReadError Word64
readWord64 (ByteString -> Either ReadError Word64)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word64Size
take :: Int32 -> Vector Word64 -> Vector Word64
take Int32
n (VectorWord64 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word64
VectorWord64 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Word64 -> Vector Word64
drop Int32
n (VectorWord64 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Word64
VectorWord64 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
word64Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Word64 -> Either ReadError [Word64]
toList (VectorWord64 Int32
len ByteString
pos) = Get Word64 -> Int32 -> ByteString -> Either ReadError [Word64]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Word64
G.getWord64le Int32
len ByteString
pos
instance VectorElement Int8 where
data Vector Int8 = VectorInt8 !Int32 !Position
length :: Vector Int8 -> Int32
length (VectorInt8 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Int8 -> Int32 -> Either ReadError Int8
unsafeIndex (VectorInt8 Int32
_ ByteString
pos) = ByteString -> Either ReadError Int8
forall a. HasPosition a => a -> Either ReadError Int8
readInt8 (ByteString -> Either ReadError Int8)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Int8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int8Size
take :: Int32 -> Vector Int8 -> Vector Int8
take Int32
n (VectorInt8 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int8
VectorInt8 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Int8 -> Vector Int8
drop Int32
n (VectorInt8 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int8
VectorInt8 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int8Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Int8 -> Either ReadError [Int8]
toList (VectorInt8 Int32
len ByteString
pos) = Get Int8 -> Int32 -> ByteString -> Either ReadError [Int8]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int8
G.getInt8 Int32
len ByteString
pos
instance VectorElement Int16 where
data Vector Int16 = VectorInt16 !Int32 !Position
length :: Vector Int16 -> Int32
length (VectorInt16 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Int16 -> Int32 -> Either ReadError Int16
unsafeIndex (VectorInt16 Int32
_ ByteString
pos) = ByteString -> Either ReadError Int16
forall a. HasPosition a => a -> Either ReadError Int16
readInt16 (ByteString -> Either ReadError Int16)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Int16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int16Size
take :: Int32 -> Vector Int16 -> Vector Int16
take Int32
n (VectorInt16 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int16
VectorInt16 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Int16 -> Vector Int16
drop Int32
n (VectorInt16 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int16
VectorInt16 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int16Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Int16 -> Either ReadError [Int16]
toList (VectorInt16 Int32
len ByteString
pos) = Get Int16 -> Int32 -> ByteString -> Either ReadError [Int16]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int16
G.getInt16le Int32
len ByteString
pos
instance VectorElement Int32 where
data Vector Int32 = VectorInt32 !Int32 !Position
length :: Vector Int32 -> Int32
length (VectorInt32 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Int32 -> Int32 -> Either ReadError Int32
unsafeIndex (VectorInt32 Int32
_ ByteString
pos) = ByteString -> Either ReadError Int32
forall a. HasPosition a => a -> Either ReadError Int32
readInt32 (ByteString -> Either ReadError Int32)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int32Size
take :: Int32 -> Vector Int32 -> Vector Int32
take Int32
n (VectorInt32 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int32
VectorInt32 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Int32 -> Vector Int32
drop Int32
n (VectorInt32 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int32
VectorInt32 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int32Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Int32 -> Either ReadError [Int32]
toList (VectorInt32 Int32
len ByteString
pos) = Get Int32 -> Int32 -> ByteString -> Either ReadError [Int32]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int32
G.getInt32le Int32
len ByteString
pos
instance VectorElement Int64 where
data Vector Int64 = VectorInt64 !Int32 !Position
length :: Vector Int64 -> Int32
length (VectorInt64 Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Int64 -> Int32 -> Either ReadError Int64
unsafeIndex (VectorInt64 Int32
_ ByteString
pos) = ByteString -> Either ReadError Int64
forall a. HasPosition a => a -> Either ReadError Int64
readInt64 (ByteString -> Either ReadError Int64)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Int64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int64Size
take :: Int32 -> Vector Int64 -> Vector Int64
take Int32
n (VectorInt64 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int64
VectorInt64 (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Int64 -> Vector Int64
drop Int32
n (VectorInt64 Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Int64
VectorInt64 (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
int64Size Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Int64 -> Either ReadError [Int64]
toList (VectorInt64 Int32
len ByteString
pos) = Get Int64 -> Int32 -> ByteString -> Either ReadError [Int64]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int64
G.getInt64le Int32
len ByteString
pos
instance VectorElement Float where
data Vector Float = VectorFloat !Int32 !Position
length :: Vector Float -> Int32
length (VectorFloat Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Float -> Int32 -> Either ReadError Float
unsafeIndex (VectorFloat Int32
_ ByteString
pos) = ByteString -> Either ReadError Float
forall a. HasPosition a => a -> Either ReadError Float
readFloat (ByteString -> Either ReadError Float)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
floatSize
take :: Int32 -> Vector Float -> Vector Float
take Int32
n (VectorFloat Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Float
VectorFloat (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Float -> Vector Float
drop Int32
n (VectorFloat Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Float
VectorFloat (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
floatSize Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Float -> Either ReadError [Float]
toList (VectorFloat Int32
len ByteString
pos) = Get Float -> Int32 -> ByteString -> Either ReadError [Float]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Float
G.getFloatle Int32
len ByteString
pos
instance VectorElement Double where
data Vector Double = VectorDouble !Int32 !Position
length :: Vector Double -> Int32
length (VectorDouble Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Double -> Int32 -> Either ReadError Double
unsafeIndex (VectorDouble Int32
_ ByteString
pos) = ByteString -> Either ReadError Double
forall a. HasPosition a => a -> Either ReadError Double
readDouble (ByteString -> Either ReadError Double)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
doubleSize
take :: Int32 -> Vector Double -> Vector Double
take Int32
n (VectorDouble Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Double
VectorDouble (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Double -> Vector Double
drop Int32
n (VectorDouble Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Double
VectorDouble (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
doubleSize Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Double -> Either ReadError [Double]
toList (VectorDouble Int32
len ByteString
pos) = Get Double -> Int32 -> ByteString -> Either ReadError [Double]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Double
G.getDoublele Int32
len ByteString
pos
instance VectorElement Bool where
data Vector Bool = VectorBool !Int32 !Position
length :: Vector Bool -> Int32
length (VectorBool Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Bool -> Int32 -> Either ReadError Bool
unsafeIndex (VectorBool Int32
_ ByteString
pos) = ByteString -> Either ReadError Bool
forall a. HasPosition a => a -> Either ReadError Bool
readBool (ByteString -> Either ReadError Bool)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
boolSize
take :: Int32 -> Vector Bool -> Vector Bool
take Int32
n (VectorBool Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Bool
VectorBool (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Bool -> Vector Bool
drop Int32
n (VectorBool Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Bool
VectorBool (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
boolSize Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Bool -> Either ReadError [Bool]
toList (VectorBool Int32
len ByteString
pos) = (Word8 -> Bool) -> [Word8] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word8 -> Bool
word8ToBool ([Word8] -> [Bool])
-> Either ReadError [Word8] -> Either ReadError [Bool]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Vector Word8 -> Either ReadError [Word8]
forall a. VectorElement a => Vector a -> Either ReadError [a]
toList (Int32 -> ByteString -> Vector Word8
VectorWord8 Int32
len ByteString
pos)
instance VectorElement Text where
data Vector Text = VectorText !Int32 !Position
length :: Vector Text -> Int32
length (VectorText Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector Text -> Int32 -> Either ReadError Text
unsafeIndex (VectorText Int32
_ ByteString
pos) = ByteString -> Either ReadError Text
forall a. HasPosition a => a -> Either ReadError Text
readText (ByteString -> Either ReadError Text)
-> (Int32 -> ByteString) -> Int32 -> Either ReadError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
textRefSize
take :: Int32 -> Vector Text -> Vector Text
take Int32
n (VectorText Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Text
VectorText (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector Text -> Vector Text
drop Int32
n (VectorText Int32
len ByteString
pos) = Int32 -> ByteString -> Vector Text
VectorText (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos Int32
forall a. Num a => a
textRefSize Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector Text -> Either ReadError [Text]
toList :: Vector Text -> Either ReadError [Text]
toList (VectorText Int32
len ByteString
pos) = do
[Int32]
offsets <- Get Int32 -> Int32 -> ByteString -> Either ReadError [Int32]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int32
G.getInt32le Int32
len ByteString
pos
[Text] -> [Text]
forall a. [a] -> [a]
L.reverse ([Text] -> [Text])
-> Either ReadError [Text] -> Either ReadError [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Int32] -> Int32 -> [Text] -> Either ReadError [Text]
go [Int32]
offsets Int32
0 []
where
go :: [Int32] -> Int32 -> [Text] -> Either ReadError [Text]
go :: [Int32] -> Int32 -> [Text] -> Either ReadError [Text]
go [] Int32
_ [Text]
acc = [Text] -> Either ReadError [Text]
forall a b. b -> Either a b
Right [Text]
acc
go (Int32
offset : [Int32]
xs) Int32
ix [Text]
acc = do
let textPos :: ByteString
textPos = ByteString -> Int32 -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move ByteString
pos (Int32
offset Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
4))
Text
text <- Either ReadError (Either ReadError Text) -> Either ReadError Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Either ReadError (Either ReadError Text) -> Either ReadError Text)
-> Either ReadError (Either ReadError Text)
-> Either ReadError Text
forall a b. (a -> b) -> a -> b
$ ByteString
-> Get (Either ReadError Text)
-> Either ReadError (Either ReadError Text)
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
textPos Get (Either ReadError Text)
readText'
[Int32] -> Int32 -> [Text] -> Either ReadError [Text]
go [Int32]
xs (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
1) (Text
text Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
acc)
instance IsStruct a => VectorElement (Struct a) where
data Vector (Struct a) = VectorStruct !Int32 !Position
length :: Vector (Struct a) -> Int32
length (VectorStruct Int32
len ByteString
_) = Int32
len
unsafeIndex :: Vector (Struct a) -> Int32 -> Either ReadError (Struct a)
unsafeIndex (VectorStruct Int32
_ ByteString
pos) = Struct a -> Either ReadError (Struct a)
forall a b. b -> Either a b
Right (Struct a -> Either ReadError (Struct a))
-> (Int32 -> Struct a) -> Int32 -> Either ReadError (Struct a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Struct a
forall a s. HasPosition a => a -> Struct s
readStruct (ByteString -> Struct a)
-> (Int32 -> ByteString) -> Int32 -> Struct a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos (InlineSize -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. IsStruct a => InlineSize
structSizeOf @a))
take :: Int32 -> Vector (Struct a) -> Vector (Struct a)
take Int32
n (VectorStruct Int32
len ByteString
pos) = Int32 -> ByteString -> Vector (Struct a)
forall a. Int32 -> ByteString -> Vector (Struct a)
VectorStruct (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) ByteString
pos
drop :: Int32 -> Vector (Struct a) -> Vector (Struct a)
drop Int32
n (VectorStruct Int32
len ByteString
pos) = Int32 -> ByteString -> Vector (Struct a)
forall a. Int32 -> ByteString -> Vector (Struct a)
VectorStruct (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (ByteString -> Int32 -> Int32 -> ByteString
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem ByteString
pos (InlineSize -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. IsStruct a => InlineSize
structSizeOf @a)) Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector (Struct a) -> Either ReadError [Struct a]
toList (VectorStruct Int32
len ByteString
pos) =
[Struct a] -> Either ReadError [Struct a]
forall a b. b -> Either a b
Right (Int32 -> ByteString -> [Struct a]
go Int32
len ByteString
pos)
where
go :: Int32 -> Position -> [Struct a]
go :: Int32 -> ByteString -> [Struct a]
go Int32
0 ByteString
_ = []
go !Int32
len ByteString
pos =
let head :: Struct a
head = ByteString -> Struct a
forall a s. HasPosition a => a -> Struct s
readStruct ByteString
pos
tail :: [Struct a]
tail = Int32 -> ByteString -> [Struct a]
go (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
1) (ByteString -> InlineSize -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move ByteString
pos (forall a. IsStruct a => InlineSize
structSizeOf @a))
in Struct a
head Struct a -> [Struct a] -> [Struct a]
forall a. a -> [a] -> [a]
: [Struct a]
tail
instance VectorElement (Table a) where
data Vector (Table a) = VectorTable !Int32 !PositionInfo
length :: Vector (Table a) -> Int32
length (VectorTable Int32
len PositionInfo
_) = Int32
len
unsafeIndex :: Vector (Table a) -> Int32 -> Either ReadError (Table a)
unsafeIndex (VectorTable Int32
_ PositionInfo
pos) = PositionInfo -> Either ReadError (Table a)
forall t. PositionInfo -> Either ReadError (Table t)
readTable (PositionInfo -> Either ReadError (Table a))
-> (Int32 -> PositionInfo) -> Int32 -> Either ReadError (Table a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PositionInfo -> Int32 -> Int32 -> PositionInfo
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem PositionInfo
pos Int32
forall a. Num a => a
tableRefSize
take :: Int32 -> Vector (Table a) -> Vector (Table a)
take Int32
n (VectorTable Int32
len PositionInfo
pos) = Int32 -> PositionInfo -> Vector (Table a)
forall a. Int32 -> PositionInfo -> Vector (Table a)
VectorTable (Int32 -> Int32 -> Int32
clamp Int32
n Int32
len) PositionInfo
pos
drop :: Int32 -> Vector (Table a) -> Vector (Table a)
drop Int32
n (VectorTable Int32
len PositionInfo
pos) = Int32 -> PositionInfo -> Vector (Table a)
forall a. Int32 -> PositionInfo -> Vector (Table a)
VectorTable (Int32
len Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
n') (PositionInfo -> Int32 -> Int32 -> PositionInfo
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem PositionInfo
pos Int32
forall a. Num a => a
tableRefSize Int32
n')
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n Int32
len
toList :: Vector (Table a) -> Either ReadError [Table a]
toList (VectorTable Int32
len PositionInfo
vectorPos) = do
[Int32]
offsets <- Get Int32 -> Int32 -> ByteString -> Either ReadError [Int32]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int32
G.getInt32le Int32
len (PositionInfo -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition PositionInfo
vectorPos)
[Int32] -> Int32 -> Either ReadError [Table a]
go [Int32]
offsets Int32
0
where
go :: [Int32] -> Int32 -> Either ReadError [Table a]
go :: [Int32] -> Int32 -> Either ReadError [Table a]
go [] Int32
_ = [Table a] -> Either ReadError [Table a]
forall a b. b -> Either a b
Right []
go (Int32
offset : [Int32]
offsets) !Int32
ix = do
let tablePos :: PositionInfo
tablePos = PositionInfo -> Int32 -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move PositionInfo
vectorPos (Int32
offset Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
4))
Table a
table <- PositionInfo -> Either ReadError (Table a)
forall t. PositionInfo -> Either ReadError (Table t)
readTable' PositionInfo
tablePos
[Table a]
tables <- [Int32] -> Int32 -> Either ReadError [Table a]
go [Int32]
offsets (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
1)
[Table a] -> Either ReadError [Table a]
forall a. a -> Either ReadError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Table a
table Table a -> [Table a] -> [Table a]
forall a. a -> [a] -> [a]
: [Table a]
tables)
instance VectorElement (Union a) where
data Vector (Union a) = VectorUnion
{ forall a. Vector (Union a) -> Vector Word8
vectorUnionTypesPos :: !(Vector Word8)
, forall a. Vector (Union a) -> PositionInfo
vectorUnionValuesPos :: !PositionInfo
, forall a.
Vector (Union a)
-> Positive Word8 -> PositionInfo -> Either ReadError (Union a)
vectorUnionReadElem :: !(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
}
length :: Vector (Union a) -> Int32
length = Vector Word8 -> Int32
forall a. VectorElement a => Vector a -> Int32
length (Vector Word8 -> Int32)
-> (Vector (Union a) -> Vector Word8) -> Vector (Union a) -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Union a) -> Vector Word8
forall a. Vector (Union a) -> Vector Word8
vectorUnionTypesPos
unsafeIndex :: Vector (Union a) -> Int32 -> Either ReadError (Union a)
unsafeIndex (VectorUnion Vector Word8
typesPos PositionInfo
valuesPos Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem) Int32
ix = do
Word8
unionType <- Vector Word8 -> Int32 -> Either ReadError Word8
forall a.
VectorElement a =>
Vector a -> Int32 -> Either ReadError a
unsafeIndex Vector Word8
typesPos Int32
ix
case Word8 -> Maybe (Positive Word8)
forall a. (Num a, Ord a) => a -> Maybe (Positive a)
positive Word8
unionType of
Maybe (Positive Word8)
Nothing -> ReadError -> Either ReadError (Union a)
forall a b. a -> Either a b
Left ReadError
"Union vector: 'None' is not allowed in vectors."
Just Positive Word8
unionType' -> do
PositionInfo
tablePos <- PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip (PositionInfo -> Either ReadError PositionInfo)
-> PositionInfo -> Either ReadError PositionInfo
forall a b. (a -> b) -> a -> b
$ PositionInfo -> Int32 -> Int32 -> PositionInfo
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem PositionInfo
valuesPos Int32
forall a. Num a => a
tableRefSize Int32
ix
Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem Positive Word8
unionType' PositionInfo
tablePos
take :: Int32 -> Vector (Union a) -> Vector (Union a)
take Int32
n (VectorUnion Vector Word8
typesPos PositionInfo
valuesPos Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem) = Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
forall a.
Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
VectorUnion (Int32 -> Vector Word8 -> Vector Word8
forall a. VectorElement a => Int32 -> Vector a -> Vector a
take Int32
n Vector Word8
typesPos) PositionInfo
valuesPos Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem
drop :: Int32 -> Vector (Union a) -> Vector (Union a)
drop Int32
n vec :: Vector (Union a)
vec@(VectorUnion Vector Word8
typesPos PositionInfo
valuesPos Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem) = Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
forall a.
Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
VectorUnion (Int32 -> Vector Word8 -> Vector Word8
forall a. VectorElement a => Int32 -> Vector a -> Vector a
drop Int32
n Vector Word8
typesPos) (PositionInfo -> Int32 -> Int32 -> PositionInfo
forall pos. HasPosition pos => pos -> Int32 -> Int32 -> pos
moveToElem PositionInfo
valuesPos Int32
forall a. Num a => a
tableRefSize Int32
n') Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem
where n' :: Int32
n' = Int32 -> Int32 -> Int32
clamp Int32
n (Vector (Union a) -> Int32
forall a. VectorElement a => Vector a -> Int32
length Vector (Union a)
vec)
toList :: Vector (Union a) -> Either ReadError [Union a]
toList vec :: Vector (Union a)
vec@(VectorUnion Vector Word8
typesPos PositionInfo
valuesPos Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem) = do
[Word8]
unionTypes <- Vector Word8 -> Either ReadError [Word8]
forall a. VectorElement a => Vector a -> Either ReadError [a]
toList Vector Word8
typesPos
[Int32]
offsets <- Get Int32 -> Int32 -> ByteString -> Either ReadError [Int32]
forall a. Get a -> Int32 -> ByteString -> Either ReadError [a]
inlineVectorToList Get Int32
G.getInt32le (Vector (Union a) -> Int32
forall a. VectorElement a => Vector a -> Int32
length Vector (Union a)
vec) (PositionInfo -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition PositionInfo
valuesPos)
[Word8] -> [Int32] -> Int32 -> Either ReadError [Union a]
go [Word8]
unionTypes [Int32]
offsets Int32
0
where
go :: [Word8] -> [Int32] -> Int32 -> Either ReadError [Union a]
go :: [Word8] -> [Int32] -> Int32 -> Either ReadError [Union a]
go [] [] Int32
_ = [Union a] -> Either ReadError [Union a]
forall a b. b -> Either a b
Right []
go (Word8
unionType : [Word8]
unionTypes) (Int32
offset : [Int32]
offsets) !Int32
ix = do
Union a
union <-
case Word8 -> Maybe (Positive Word8)
forall a. (Num a, Ord a) => a -> Maybe (Positive a)
positive Word8
unionType of
Maybe (Positive Word8)
Nothing -> ReadError -> Either ReadError (Union a)
forall a b. a -> Either a b
Left ReadError
"Union vector: 'None' is not allowed in vectors."
Just Positive Word8
unionType' ->
let tablePos :: PositionInfo
tablePos = PositionInfo -> Int32 -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move PositionInfo
valuesPos (Int32
offset Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
4))
in Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readElem Positive Word8
unionType' PositionInfo
tablePos
[Union a]
unions <- [Word8] -> [Int32] -> Int32 -> Either ReadError [Union a]
go [Word8]
unionTypes [Int32]
offsets (Int32
ix Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
1)
[Union a] -> Either ReadError [Union a]
forall a. a -> Either ReadError a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Union a
union Union a -> [Union a] -> [Union a]
forall a. a -> [a] -> [a]
: [Union a]
unions)
go [Word8]
_ [Int32]
_ Int32
_ = ReadError -> Either ReadError [Union a]
forall a b. a -> Either a b
Left ReadError
"Union vector: 'type vector' and 'value vector' do not have the same length."
{-# INLINE readStructField #-}
readStructField :: (Position -> a) -> VOffset -> Struct s -> a
readStructField :: forall a s. (ByteString -> a) -> VOffset -> Struct s -> a
readStructField ByteString -> a
read VOffset
voffset (Struct ByteString
bs) =
ByteString -> a
read (ByteString -> VOffset -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move ByteString
bs VOffset
voffset)
{-# INLINE readTableFieldOpt #-}
readTableFieldOpt :: (PositionInfo -> Either ReadError a) -> TableIndex -> Table t -> Either ReadError (Maybe a)
readTableFieldOpt :: forall a t.
(PositionInfo -> Either ReadError a)
-> TableIndex -> Table t -> Either ReadError (Maybe a)
readTableFieldOpt PositionInfo -> Either ReadError a
read TableIndex
ix Table t
t = do
Maybe VOffset
mbOffset <- Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix
(VOffset -> Either ReadError a)
-> Maybe VOffset -> Either ReadError (Maybe a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse (\VOffset
offset -> PositionInfo -> Either ReadError a
read (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
offset)) Maybe VOffset
mbOffset
{-# INLINE readTableFieldReq #-}
readTableFieldReq :: (PositionInfo -> Either ReadError a) -> TableIndex -> String -> Table t -> Either ReadError a
readTableFieldReq :: forall a t.
(PositionInfo -> Either ReadError a)
-> TableIndex -> ReadError -> Table t -> Either ReadError a
readTableFieldReq PositionInfo -> Either ReadError a
read TableIndex
ix ReadError
name Table t
t = do
Maybe VOffset
mbOffset <- Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix
case Maybe VOffset
mbOffset of
Maybe VOffset
Nothing -> ReadError -> Either ReadError a
forall a. ReadError -> Either ReadError a
missingField ReadError
name
Just VOffset
offset -> PositionInfo -> Either ReadError a
read (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
offset)
{-# INLINE readTableFieldWithDef #-}
readTableFieldWithDef :: (PositionInfo -> Either ReadError a) -> TableIndex -> a -> Table t -> Either ReadError a
readTableFieldWithDef :: forall a t.
(PositionInfo -> Either ReadError a)
-> TableIndex -> a -> Table t -> Either ReadError a
readTableFieldWithDef PositionInfo -> Either ReadError a
read TableIndex
ix a
dflt Table t
t =
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError a) -> Either ReadError a
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> a -> Either ReadError a
forall a b. b -> Either a b
Right a
dflt
Just VOffset
offset -> PositionInfo -> Either ReadError a
read (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
offset)
{-# INLINE readTableFieldUnionOpt #-}
readTableFieldUnionOpt ::
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> Table t
-> Either ReadError (Maybe (Union a))
readTableFieldUnionOpt :: forall a t.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex -> Table t -> Either ReadError (Maybe (Union a))
readTableFieldUnionOpt Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read TableIndex
ix Table t
t =
(PositionInfo -> Either ReadError Word8)
-> TableIndex -> Word8 -> Table t -> Either ReadError Word8
forall a t.
(PositionInfo -> Either ReadError a)
-> TableIndex -> a -> Table t -> Either ReadError a
readTableFieldWithDef PositionInfo -> Either ReadError Word8
forall a. HasPosition a => a -> Either ReadError Word8
readWord8 (TableIndex
ix TableIndex -> TableIndex -> TableIndex
forall a. Num a => a -> a -> a
- TableIndex
1) Word8
0 Table t
t Either ReadError Word8
-> (Word8 -> Either ReadError (Maybe (Union a)))
-> Either ReadError (Maybe (Union a))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
unionType ->
case Word8 -> Maybe (Positive Word8)
forall a. (Num a, Ord a) => a -> Maybe (Positive a)
positive Word8
unionType of
Maybe (Positive Word8)
Nothing -> Maybe (Union a) -> Either ReadError (Maybe (Union a))
forall a b. b -> Either a b
Right Maybe (Union a)
forall a. Maybe a
Nothing
Just Positive Word8
unionType' ->
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Maybe (Union a)))
-> Either ReadError (Maybe (Union a))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> ReadError -> Either ReadError (Maybe (Union a))
forall a b. a -> Either a b
Left ReadError
"Union: 'union type' found but 'union value' is missing."
Just VOffset
offset ->
PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
offset)
Either ReadError PositionInfo
-> (PositionInfo -> Either ReadError (Union a))
-> Either ReadError (Union a)
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read Positive Word8
unionType'
Either ReadError (Union a)
-> (Union a -> Maybe (Union a))
-> Either ReadError (Maybe (Union a))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> Union a -> Maybe (Union a)
forall a. a -> Maybe a
Just
{-# INLINE readTableFieldUnionReq #-}
readTableFieldUnionReq ::
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> String
-> Table t
-> Either ReadError (Union a)
readTableFieldUnionReq :: forall a t.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex -> ReadError -> Table t -> Either ReadError (Union a)
readTableFieldUnionReq Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read TableIndex
ix ReadError
name Table t
t =
(PositionInfo -> Either ReadError Word8)
-> TableIndex -> Word8 -> Table t -> Either ReadError Word8
forall a t.
(PositionInfo -> Either ReadError a)
-> TableIndex -> a -> Table t -> Either ReadError a
readTableFieldWithDef PositionInfo -> Either ReadError Word8
forall a. HasPosition a => a -> Either ReadError Word8
readWord8 (TableIndex
ix TableIndex -> TableIndex -> TableIndex
forall a. Num a => a -> a -> a
- TableIndex
1) Word8
0 Table t
t Either ReadError Word8
-> (Word8 -> Either ReadError (Union a))
-> Either ReadError (Union a)
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word8
unionType ->
case Word8 -> Maybe (Positive Word8)
forall a. (Num a, Ord a) => a -> Maybe (Positive a)
positive Word8
unionType of
Maybe (Positive Word8)
Nothing -> ReadError -> Either ReadError (Union a)
forall a. ReadError -> Either ReadError a
missingField ReadError
name
Just Positive Word8
unionType' ->
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Union a))
-> Either ReadError (Union a)
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> ReadError -> Either ReadError (Union a)
forall a b. a -> Either a b
Left ReadError
"Union: 'union type' found but 'union value' is missing."
Just VOffset
offset -> PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
offset) Either ReadError PositionInfo
-> (PositionInfo -> Either ReadError (Union a))
-> Either ReadError (Union a)
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read Positive Word8
unionType'
readTableFieldUnionVectorOpt ::
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> Table t
-> Either ReadError (Maybe (Vector (Union a)))
readTableFieldUnionVectorOpt :: forall a t.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> Table t
-> Either ReadError (Maybe (Vector (Union a)))
readTableFieldUnionVectorOpt Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read TableIndex
ix Table t
t =
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t (TableIndex
ix TableIndex -> TableIndex -> TableIndex
forall a. Num a => a -> a -> a
- TableIndex
1) Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Maybe (Vector (Union a))))
-> Either ReadError (Maybe (Vector (Union a)))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> Maybe (Vector (Union a))
-> Either ReadError (Maybe (Vector (Union a)))
forall a b. b -> Either a b
Right Maybe (Vector (Union a))
forall a. Maybe a
Nothing
Just VOffset
typesOffset ->
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Maybe (Vector (Union a))))
-> Either ReadError (Maybe (Vector (Union a)))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> ReadError -> Either ReadError (Maybe (Vector (Union a)))
forall a b. a -> Either a b
Left ReadError
"Union vector: 'type vector' found but 'value vector' is missing."
Just VOffset
valuesOffset ->
Vector (Union a) -> Maybe (Vector (Union a))
forall a. a -> Maybe a
Just (Vector (Union a) -> Maybe (Vector (Union a)))
-> Either ReadError (Vector (Union a))
-> Either ReadError (Maybe (Vector (Union a)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
forall a.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
readUnionVector Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
typesOffset) (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
valuesOffset)
readTableFieldUnionVectorReq ::
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> String
-> Table t
-> Either ReadError (Vector (Union a))
readTableFieldUnionVectorReq :: forall a t.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> TableIndex
-> ReadError
-> Table t
-> Either ReadError (Vector (Union a))
readTableFieldUnionVectorReq Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read TableIndex
ix ReadError
name Table t
t =
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t (TableIndex
ix TableIndex -> TableIndex -> TableIndex
forall a. Num a => a -> a -> a
- TableIndex
1) Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Vector (Union a)))
-> Either ReadError (Vector (Union a))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> ReadError -> Either ReadError (Vector (Union a))
forall a. ReadError -> Either ReadError a
missingField ReadError
name
Just VOffset
typesOffset ->
Table t -> TableIndex -> Either ReadError (Maybe VOffset)
forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table t
t TableIndex
ix Either ReadError (Maybe VOffset)
-> (Maybe VOffset -> Either ReadError (Vector (Union a)))
-> Either ReadError (Vector (Union a))
forall a b.
Either ReadError a
-> (a -> Either ReadError b) -> Either ReadError b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe VOffset
Nothing -> ReadError -> Either ReadError (Vector (Union a))
forall a b. a -> Either a b
Left ReadError
"Union vector: 'type vector' found but 'value vector' is missing."
Just VOffset
valuesOffset ->
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
forall a.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
readUnionVector Positive Word8 -> PositionInfo -> Either ReadError (Union a)
read (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
typesOffset) (PositionInfo -> VOffset -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (Table t -> PositionInfo
forall a. Table a -> PositionInfo
tablePos Table t
t) VOffset
valuesOffset)
{-# INLINE readInt8 #-}
readInt8 :: HasPosition a => a -> Either ReadError Int8
readInt8 :: forall a. HasPosition a => a -> Either ReadError Int8
readInt8 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Int8 -> Either ReadError Int8
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Int8
G.getInt8
{-# INLINE readInt16 #-}
readInt16 :: HasPosition a => a -> Either ReadError Int16
readInt16 :: forall a. HasPosition a => a -> Either ReadError Int16
readInt16 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Int16 -> Either ReadError Int16
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Int16
G.getInt16le
{-# INLINE readInt32 #-}
readInt32 :: HasPosition a => a -> Either ReadError Int32
readInt32 :: forall a. HasPosition a => a -> Either ReadError Int32
readInt32 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Int32 -> Either ReadError Int32
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Int32
G.getInt32le
{-# INLINE readInt64 #-}
readInt64 :: HasPosition a => a -> Either ReadError Int64
readInt64 :: forall a. HasPosition a => a -> Either ReadError Int64
readInt64 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Int64 -> Either ReadError Int64
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Int64
G.getInt64le
{-# INLINE readWord8 #-}
readWord8 :: HasPosition a => a -> Either ReadError Word8
readWord8 :: forall a. HasPosition a => a -> Either ReadError Word8
readWord8 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Word8 -> Either ReadError Word8
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Word8
G.getWord8
{-# INLINE readWord16 #-}
readWord16 :: HasPosition a => a -> Either ReadError Word16
readWord16 :: forall a. HasPosition a => a -> Either ReadError Word16
readWord16 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Word16 -> Either ReadError Word16
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Word16
G.getWord16le
{-# INLINE readWord32 #-}
readWord32 :: HasPosition a => a -> Either ReadError Word32
readWord32 :: forall a. HasPosition a => a -> Either ReadError Word32
readWord32 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Word32 -> Either ReadError Word32
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Word32
G.getWord32le
{-# INLINE readWord64 #-}
readWord64 :: HasPosition a => a -> Either ReadError Word64
readWord64 :: forall a. HasPosition a => a -> Either ReadError Word64
readWord64 (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Word64 -> Either ReadError Word64
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Word64
G.getWord64le
{-# INLINE readFloat #-}
readFloat :: HasPosition a => a -> Either ReadError Float
readFloat :: forall a. HasPosition a => a -> Either ReadError Float
readFloat (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Float -> Either ReadError Float
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Float
G.getFloatle
{-# INLINE readDouble #-}
readDouble :: HasPosition a => a -> Either ReadError Double
readDouble :: forall a. HasPosition a => a -> Either ReadError Double
readDouble (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) = ByteString -> Get Double -> Either ReadError Double
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos Get Double
G.getDoublele
{-# INLINE readBool #-}
readBool :: HasPosition a => a -> Either ReadError Bool
readBool :: forall a. HasPosition a => a -> Either ReadError Bool
readBool a
p = Word8 -> Bool
word8ToBool (Word8 -> Bool) -> Either ReadError Word8 -> Either ReadError Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Either ReadError Word8
forall a. HasPosition a => a -> Either ReadError Word8
readWord8 a
p
{-# INLINE word8ToBool #-}
word8ToBool :: Word8 -> Bool
word8ToBool :: Word8 -> Bool
word8ToBool Word8
0 = Bool
False
word8ToBool Word8
_ = Bool
True
readPrimVector ::
(Int32 -> Position -> Vector a)
-> PositionInfo
-> Either ReadError (Vector a)
readPrimVector :: forall a.
(Int32 -> ByteString -> Vector a)
-> PositionInfo -> Either ReadError (Vector a)
readPrimVector Int32 -> ByteString -> Vector a
vecConstructor (PositionInfo -> ByteString
posCurrent -> ByteString
pos) = do
ByteString
vecPos <- ByteString -> Either ReadError ByteString
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip ByteString
pos
Int32
vecLength <- ByteString -> Either ReadError Int32
forall a. HasPosition a => a -> Either ReadError Int32
readInt32 ByteString
vecPos
Vector a -> Either ReadError (Vector a)
forall a b. b -> Either a b
Right (Vector a -> Either ReadError (Vector a))
-> Vector a -> Either ReadError (Vector a)
forall a b. (a -> b) -> a -> b
$! Int32 -> ByteString -> Vector a
vecConstructor Int32
vecLength (ByteString -> Int64 -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move ByteString
vecPos (Int64
forall a. Num a => a
int32Size :: Int64))
readTableVector :: PositionInfo -> Either ReadError (Vector (Table a))
readTableVector :: forall a. PositionInfo -> Either ReadError (Vector (Table a))
readTableVector PositionInfo
pos = do
PositionInfo
vecPos <- PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip PositionInfo
pos
Int32
vecLength <- PositionInfo -> Either ReadError Int32
forall a. HasPosition a => a -> Either ReadError Int32
readInt32 PositionInfo
vecPos
Vector (Table a) -> Either ReadError (Vector (Table a))
forall a b. b -> Either a b
Right (Vector (Table a) -> Either ReadError (Vector (Table a)))
-> Vector (Table a) -> Either ReadError (Vector (Table a))
forall a b. (a -> b) -> a -> b
$! Int32 -> PositionInfo -> Vector (Table a)
forall a. Int32 -> PositionInfo -> Vector (Table a)
VectorTable Int32
vecLength (PositionInfo -> Int64 -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move PositionInfo
vecPos (Int64
forall a. Num a => a
int32Size :: Int64))
readUnionVector ::
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
readUnionVector :: forall a.
(Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> PositionInfo
-> PositionInfo
-> Either ReadError (Vector (Union a))
readUnionVector Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readUnion PositionInfo
typesPos PositionInfo
valuesPos =
do
Vector Word8
typesVec <- (Int32 -> ByteString -> Vector Word8)
-> PositionInfo -> Either ReadError (Vector Word8)
forall a.
(Int32 -> ByteString -> Vector a)
-> PositionInfo -> Either ReadError (Vector a)
readPrimVector Int32 -> ByteString -> Vector Word8
VectorWord8 PositionInfo
typesPos
PositionInfo
valuesVec <- PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip PositionInfo
valuesPos
Vector (Union a) -> Either ReadError (Vector (Union a))
forall a b. b -> Either a b
Right (Vector (Union a) -> Either ReadError (Vector (Union a)))
-> Vector (Union a) -> Either ReadError (Vector (Union a))
forall a b. (a -> b) -> a -> b
$! Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
forall a.
Vector Word8
-> PositionInfo
-> (Positive Word8 -> PositionInfo -> Either ReadError (Union a))
-> Vector (Union a)
VectorUnion
Vector Word8
typesVec
(PositionInfo -> Int64 -> PositionInfo
forall i. Integral i => PositionInfo -> i -> PositionInfo
forall a i. (HasPosition a, Integral i) => a -> i -> a
move PositionInfo
valuesVec (Int64
forall a. Num a => a
int32Size :: Int64))
Positive Word8 -> PositionInfo -> Either ReadError (Union a)
readUnion
{-# INLINE readText #-}
readText :: HasPosition a => a -> Either ReadError Text
readText :: forall a. HasPosition a => a -> Either ReadError Text
readText (a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition -> ByteString
pos) =
Either ReadError (Either ReadError Text) -> Either ReadError Text
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Either ReadError (Either ReadError Text) -> Either ReadError Text)
-> Either ReadError (Either ReadError Text)
-> Either ReadError Text
forall a b. (a -> b) -> a -> b
$ ByteString
-> Get (Either ReadError Text)
-> Either ReadError (Either ReadError Text)
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
pos (Get (Either ReadError Text)
-> Either ReadError (Either ReadError Text))
-> Get (Either ReadError Text)
-> Either ReadError (Either ReadError Text)
forall a b. (a -> b) -> a -> b
$ do
Int32
uoffset <- Get Int32
G.getInt32le
Int -> Get ()
G.skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int (Int32
uoffset Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
forall a. Num a => a
uoffsetSize))
Get (Either ReadError Text)
readText'
{-# INLINE readText' #-}
readText' :: Get (Either ReadError Text)
readText' :: Get (Either ReadError Text)
readText' = do
Int32
strLength <- Get Int32
G.getInt32le
ByteString
bs <- Int -> Get ByteString
G.getByteString (Int -> Get ByteString) -> Int -> Get ByteString
forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int Int32
strLength
Either ReadError Text -> Get (Either ReadError Text)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ReadError Text -> Get (Either ReadError Text))
-> Either ReadError Text -> Get (Either ReadError Text)
forall a b. (a -> b) -> a -> b
$! case ByteString -> Either UnicodeException Text
T.decodeUtf8' ByteString
bs of
Right Text
t -> Text -> Either ReadError Text
forall a b. b -> Either a b
Right Text
t
Left (T.DecodeError ReadError
msg Maybe Word8
byteMaybe) ->
case Maybe Word8
byteMaybe of
Just Word8
byte -> ReadError -> Either ReadError Text
forall a b. a -> Either a b
Left (ReadError -> Either ReadError Text)
-> ReadError -> Either ReadError Text
forall a b. (a -> b) -> a -> b
$ ReadError
"UTF8 decoding error (byte " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> Word8 -> ReadError
forall a. Show a => a -> ReadError
show Word8
byte ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> ReadError
"): " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> ReadError
msg
Maybe Word8
Nothing -> ReadError -> Either ReadError Text
forall a b. a -> Either a b
Left (ReadError -> Either ReadError Text)
-> ReadError -> Either ReadError Text
forall a b. (a -> b) -> a -> b
$ ReadError
"UTF8 decoding error: " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> ReadError
msg
Left UnicodeException
_ -> ReadError -> Either ReadError Text
forall a. HasCallStack => ReadError -> a
error ReadError
"the impossible happened"
{-# INLINE readTable #-}
readTable :: PositionInfo -> Either ReadError (Table t)
readTable :: forall t. PositionInfo -> Either ReadError (Table t)
readTable = PositionInfo -> Either ReadError PositionInfo
forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip (PositionInfo -> Either ReadError PositionInfo)
-> (PositionInfo -> Either ReadError (Table t))
-> PositionInfo
-> Either ReadError (Table t)
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> PositionInfo -> Either ReadError (Table t)
forall t. PositionInfo -> Either ReadError (Table t)
readTable'
{-# INLINE readTable' #-}
readTable' :: PositionInfo -> Either ReadError (Table t)
readTable' :: forall t. PositionInfo -> Either ReadError (Table t)
readTable' PositionInfo
tablePos =
PositionInfo -> Either ReadError Int32
forall a. HasPosition a => a -> Either ReadError Int32
readInt32 PositionInfo
tablePos Either ReadError Int32
-> (Int32 -> Table t) -> Either ReadError (Table t)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \Int32
soffset ->
let vtableOffsetFromRoot :: Int32
vtableOffsetFromRoot = OffsetFromRoot -> Int32
forall a b. Coercible a b => a -> b
coerce (PositionInfo -> OffsetFromRoot
posOffsetFromRoot PositionInfo
tablePos) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
soffset
vtable :: ByteString
vtable = ByteString -> Int32 -> ByteString
forall i. Integral i => ByteString -> i -> ByteString
forall a i. (HasPosition a, Integral i) => a -> i -> a
move (PositionInfo -> ByteString
posRoot PositionInfo
tablePos) Int32
vtableOffsetFromRoot
in ByteString -> PositionInfo -> Table t
forall a. ByteString -> PositionInfo -> Table a
Table ByteString
vtable PositionInfo
tablePos
{-# INLINE readStruct #-}
readStruct :: HasPosition a => a -> Struct s
readStruct :: forall a s. HasPosition a => a -> Struct s
readStruct = ByteString -> Struct s
forall a. ByteString -> Struct a
Struct (ByteString -> Struct s) -> (a -> ByteString) -> a -> Struct s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. HasPosition a => a -> ByteString
getPosition
{-# INLINE tableIndexToVOffset #-}
tableIndexToVOffset :: Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset :: forall t. Table t -> TableIndex -> Either ReadError (Maybe VOffset)
tableIndexToVOffset Table{ByteString
PositionInfo
vtable :: forall a. Table a -> ByteString
tablePos :: forall a. Table a -> PositionInfo
vtable :: ByteString
tablePos :: PositionInfo
..} TableIndex
ix =
ByteString
-> Get (Maybe VOffset) -> Either ReadError (Maybe VOffset)
forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
vtable (Get (Maybe VOffset) -> Either ReadError (Maybe VOffset))
-> Get (Maybe VOffset) -> Either ReadError (Maybe VOffset)
forall a b. (a -> b) -> a -> b
$ do
Word16
vtableSize <- Get Word16
G.getWord16le
let vtableIndex :: Word16
vtableIndex = Word16
4 Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ (TableIndex -> Word16
unTableIndex TableIndex
ix Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
* Word16
2)
if Word16
vtableIndex Word16 -> Word16 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word16
vtableSize
then Maybe VOffset -> Get (Maybe VOffset)
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe VOffset
forall a. Maybe a
Nothing
else do
Int -> Get ()
G.skip (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word16 @Int Word16
vtableIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
Get Word16
G.getWord16le Get Word16 -> (Word16 -> Maybe VOffset) -> Get (Maybe VOffset)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Word16
0 -> Maybe VOffset
forall a. Maybe a
Nothing
Word16
word16 -> VOffset -> Maybe VOffset
forall a. a -> Maybe a
Just (Word16 -> VOffset
VOffset Word16
word16)
{-# INLINE readUOffsetAndSkip #-}
readUOffsetAndSkip :: HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip :: forall pos. HasPosition pos => pos -> Either ReadError pos
readUOffsetAndSkip pos
pos =
pos -> Int32 -> pos
forall i. Integral i => pos -> i -> pos
forall a i. (HasPosition a, Integral i) => a -> i -> a
move pos
pos (Int32 -> pos) -> Either ReadError Int32 -> Either ReadError pos
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> pos -> Either ReadError Int32
forall a. HasPosition a => a -> Either ReadError Int32
readInt32 pos
pos
{-# INLINE runGet #-}
runGet :: ByteString -> Get a -> Either ReadError a
runGet :: forall a. ByteString -> Get a -> Either ReadError a
runGet ByteString
bs Get a
get =
case Get a
-> ByteString
-> Either (ByteString, Int64, ReadError) (ByteString, Int64, a)
forall a.
Get a
-> ByteString
-> Either (ByteString, Int64, ReadError) (ByteString, Int64, a)
G.runGetOrFail Get a
get ByteString
bs of
Right (ByteString
_, Int64
_, a
a) -> a -> Either ReadError a
forall a b. b -> Either a b
Right a
a
Left (ByteString
_, Int64
_, ReadError
msg) -> ReadError -> Either ReadError a
forall a b. a -> Either a b
Left ReadError
msg
{-# NOINLINE missingField #-}
missingField :: String -> Either ReadError a
missingField :: forall a. ReadError -> Either ReadError a
missingField ReadError
fieldName =
ReadError -> Either ReadError a
forall a b. a -> Either a b
Left (ReadError -> Either ReadError a)
-> ReadError -> Either ReadError a
forall a b. (a -> b) -> a -> b
$ ReadError
"Missing required table field: " ReadError -> ShowS
forall a. Semigroup a => a -> a -> a
<> ReadError
fieldName
{-# INLINE byteStringSafeIndex #-}
byteStringSafeIndex :: ByteString -> Int32 -> Either ReadError Word8
byteStringSafeIndex :: ByteString -> Int32 -> Either ReadError Word8
byteStringSafeIndex !ByteString
cs0 !Int32
i =
ByteString -> Int32 -> Either ReadError Word8
forall {a}. IsString a => ByteString -> Int32 -> Either a Word8
index' ByteString
cs0 Int32
i
where index' :: ByteString -> Int32 -> Either a Word8
index' ByteString
BSL.Empty Int32
_ = a -> Either a Word8
forall a b. a -> Either a b
Left a
"not enough bytes"
index' (BSL.Chunk ByteString
c ByteString
cs) Int32
n
| forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int Int32
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
BS.length ByteString
c =
ByteString -> Int32 -> Either a Word8
index' ByteString
cs (Int32
n Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int @Int32 (ByteString -> Int
BS.length ByteString
c))
| Bool
otherwise = Word8 -> Either a Word8
forall a b. b -> Either a b
Right (Word8 -> Either a Word8) -> Word8 -> Either a Word8
forall a b. (a -> b) -> a -> b
$! ByteString -> Int -> Word8
BSU.unsafeIndex ByteString
c (forall a b. (Integral a, Num b) => a -> b
fromIntegral @Int32 @Int Int32
n)