From 50f03abc7b4e05f38d64851ca8c6724b4fb0c4d1 Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Fri, 1 Nov 2013 13:12:05 +0400 Subject: Document Tree module --- bittorrent.cabal | 2 +- src/Data/Torrent/InfoHash.hs | 9 +++++++++ src/Data/Torrent/Layout.hs | 2 +- src/Data/Torrent/Tree.hs | 30 ++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/bittorrent.cabal b/bittorrent.cabal index 49f444e2..65e8208d 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal @@ -41,7 +41,7 @@ library , RecordWildCards hs-source-dirs: src exposed-modules: Data.Torrent - , Data.Torrent.Bitfield +-- , Data.Torrent.Bitfield , Data.Torrent.Block , Data.Torrent.InfoHash , Data.Torrent.Layout diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 5ec429b5..ac13aa6c 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs @@ -1,3 +1,12 @@ +-- | +-- Copyright : (c) Sam Truzjan 2013 +-- License : BSD3 +-- Maintainer : pxqr.sta@gmail.com +-- Stability : experimental +-- Portability : portable +-- +-- Infohash is a unique identifier of torrent. +-- {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Data.Torrent.InfoHash diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index 6f0668f2..ea8fa894 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs @@ -5,7 +5,7 @@ -- Stability : experimental -- Portability : portable -- --- +-- Layout of files in torrent. -- {-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} diff --git a/src/Data/Torrent/Tree.hs b/src/Data/Torrent/Tree.hs index 8c18041a..cf71c2ec 100644 --- a/src/Data/Torrent/Tree.hs +++ b/src/Data/Torrent/Tree.hs @@ -5,21 +5,25 @@ -- Stability : experimental -- Portability : portable -- +-- Directory tree can be used to easily manipulate file layout info. +-- {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE DeriveDataTypeable #-} module Data.Torrent.Tree - ( DirTree (..) + ( -- * Directory tree + DirTree (..) + + -- * Construction , build + -- * Query , Data.Torrent.Tree.lookup , lookupDir - , fileNumber , dirNumber ) where -import Control.Arrow import Data.ByteString as BS import Data.ByteString.Char8 as BC import Data.Foldable @@ -30,10 +34,12 @@ import Data.Monoid import Data.Torrent.Layout +-- | 'DirTree' is more convenient form of 'LayoutInfo'. data DirTree a = Dir { children :: Map ByteString (DirTree a) } | File { node :: FileInfo a } deriving Show +-- | Build directory tree from a list of files. build :: LayoutInfo -> DirTree () build SingleFile {liFile = FileInfo {..}} = Dir { children = M.singleton fiName (File fi) } @@ -46,26 +52,30 @@ build MultiFile {..} = Dir $ M.singleton liDirName files where ent = File $ FileInfo fiLength fiMD5Sum () -decompress :: DirTree () -> [FileInfo ()] -decompress = undefined +--decompress :: DirTree () -> [FileInfo ()] +--decompress = undefined +-- | Lookup file by path. lookup :: [FilePath] -> DirTree a -> Maybe (DirTree a) lookup [] t = Just t lookup (p : ps) (Dir m) | Just subTree <- M.lookup (BC.pack p) m = Data.Torrent.Tree.lookup ps subTree lookup _ _ = Nothing +-- | Lookup directory by path. lookupDir :: [FilePath] -> DirTree a -> Maybe [(ByteString, DirTree a)] -lookupDir ps d - | Just subTree <- Data.Torrent.Tree.lookup ps d = - case subTree of - File _ -> Nothing - Dir es -> Just $ M.toList es +lookupDir ps d = do + subTree <- Data.Torrent.Tree.lookup ps d + case subTree of + File _ -> Nothing + Dir es -> Just $ M.toList es +-- | Get total count of files in directory and subdirectories. fileNumber :: DirTree a -> Sum Int fileNumber File {..} = Sum 1 fileNumber Dir {..} = foldMap fileNumber children +-- | Get total count of directories in the directory and subdirectories. dirNumber :: DirTree a -> Sum Int dirNumber File {..} = Sum 0 dirNumber Dir {..} = Sum 1 <> foldMap dirNumber children -- cgit v1.2.3