module TD.Query.ReadFilePart
  (ReadFilePart(..)
  , defaultReadFilePart
  ) where

import qualified Data.Aeson as A
import qualified Data.Aeson.Types as AT
import qualified TD.Lib.Internal as I

-- | Reads a part of a file from the TDLib file cache and returns read bytes. This method is intended to be used only if the application has no direct access to TDLib's file system, because it is usually slower than a direct read from the file. Returns 'TD.Data.FilePart.FilePart'
data ReadFilePart
  = ReadFilePart
    { ReadFilePart -> Maybe Int
file_id :: Maybe Int -- ^ Identifier of the file. The file must be located in the TDLib file cache
    , ReadFilePart -> Maybe Int
offset  :: Maybe Int -- ^ The offset from which to read the file
    , ReadFilePart -> Maybe Int
count   :: Maybe Int -- ^ Number of bytes to read. An error will be returned if there are not enough bytes available in the file from the specified position. Pass 0 to read all available data from the specified position
    }
  deriving (ReadFilePart -> ReadFilePart -> Bool
(ReadFilePart -> ReadFilePart -> Bool)
-> (ReadFilePart -> ReadFilePart -> Bool) -> Eq ReadFilePart
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ReadFilePart -> ReadFilePart -> Bool
== :: ReadFilePart -> ReadFilePart -> Bool
$c/= :: ReadFilePart -> ReadFilePart -> Bool
/= :: ReadFilePart -> ReadFilePart -> Bool
Eq, Int -> ReadFilePart -> ShowS
[ReadFilePart] -> ShowS
ReadFilePart -> String
(Int -> ReadFilePart -> ShowS)
-> (ReadFilePart -> String)
-> ([ReadFilePart] -> ShowS)
-> Show ReadFilePart
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReadFilePart -> ShowS
showsPrec :: Int -> ReadFilePart -> ShowS
$cshow :: ReadFilePart -> String
show :: ReadFilePart -> String
$cshowList :: [ReadFilePart] -> ShowS
showList :: [ReadFilePart] -> ShowS
Show)

instance I.ShortShow ReadFilePart where
  shortShow :: ReadFilePart -> String
shortShow
    ReadFilePart
      { file_id :: ReadFilePart -> Maybe Int
file_id = Maybe Int
file_id_
      , offset :: ReadFilePart -> Maybe Int
offset  = Maybe Int
offset_
      , count :: ReadFilePart -> Maybe Int
count   = Maybe Int
count_
      }
        = String
"ReadFilePart"
          String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
I.cc
          [ String
"file_id" String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
file_id_
          , String
"offset"  String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
offset_
          , String
"count"   String -> Maybe Int -> String
forall a. ShortShow a => String -> Maybe a -> String
`I.p` Maybe Int
count_
          ]

instance AT.ToJSON ReadFilePart where
  toJSON :: ReadFilePart -> Value
toJSON
    ReadFilePart
      { file_id :: ReadFilePart -> Maybe Int
file_id = Maybe Int
file_id_
      , offset :: ReadFilePart -> Maybe Int
offset  = Maybe Int
offset_
      , count :: ReadFilePart -> Maybe Int
count   = Maybe Int
count_
      }
        = [Pair] -> Value
A.object
          [ Key
"@type"   Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Text -> Value
AT.String Text
"readFilePart"
          , Key
"file_id" Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
file_id_
          , Key
"offset"  Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
offset_
          , Key
"count"   Key -> Maybe Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
A..= Maybe Int
count_
          ]

defaultReadFilePart :: ReadFilePart
defaultReadFilePart :: ReadFilePart
defaultReadFilePart =
  ReadFilePart
    { file_id :: Maybe Int
file_id = Maybe Int
forall a. Maybe a
Nothing
    , offset :: Maybe Int
offset  = Maybe Int
forall a. Maybe a
Nothing
    , count :: Maybe Int
count   = Maybe Int
forall a. Maybe a
Nothing
    }