From 855f5011c3c4ee090aa1978730b3145b1b34c4f6 Mon Sep 17 00:00:00 2001 From: startling Date: Wed, 5 Dec 2012 04:36:53 -0500 Subject: [PATCH] Added doc comments everywhere. --- Partly/Make.hs | 41 +++++++++++++++++++++++------------------ Partly/View.hs | 7 +++++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Partly/Make.hs b/Partly/Make.hs index 6d10dd9..2419df6 100644 --- a/Partly/Make.hs +++ b/Partly/Make.hs @@ -23,17 +23,29 @@ import System.Disk.Partitions.MBR import Partly.Json import Partly.Common +-- | A change we can apply to a boot record. +data Delta = Delta + { btl :: Maybe FilePath + , sig :: Maybe Word16 } + deriving (Eq, Show) + +-- | Apply some changes to a boot record. +applyDelta :: Delta -> BootRecord -> IO BootRecord +applyDelta d b = do + b <- apply b (btl d) $ \p -> (`fmap` B.readFile p) + $ \x -> b { bootloader = x} + b <- apply b (sig d) $ \x -> return b { bootSig = x } + return b + where apply b v fn = maybe (return b) fn v + +-- | The kinds of options the main program will hand us. data MakeOptions = MakeOptions { from :: Maybe FilePath , change :: Delta , displayOpts :: Display } deriving (Eq, Show) -data Delta = Delta - { btl :: Maybe FilePath - , sig :: Maybe Word16 } - deriving (Eq, Show) - +-- | A parser for those options. makeOptions :: Parser MakeOptions makeOptions = MakeOptions <$> maybeOption @@ -60,22 +72,15 @@ makeOptions = MakeOptions "f" -> Just 0x0000; "false" -> Just 0x0000; _ -> case reads s of [(v, "")] -> Just v; _ -> Nothing; +-- | A description of the parsing options. makeParser :: ParserInfo MakeOptions makeParser = info makeOptions ( progDesc "Create an MBR, potentially based on some existing one." & fullDesc) -applyDelta :: Delta -> BootRecord -> IO BootRecord -applyDelta d b = do - b <- apply b (btl d) $ \p -> (`fmap` B.readFile p) - $ \x -> b { bootloader = x} - b <- apply b (sig d) $ \x -> return b { bootSig = x } - return b - where apply b v fn = maybe (return b) fn v - +-- | Run the "make" command. make :: MakeOptions -> IO () -make m = do - base <- maybe (return nullBootRecord) - (input . flip Input Nothing) $ from m - new <- applyDelta (change m) base - display (displayOpts m) new +make m = maybe (return nullBootRecord) + (input . flip Input Nothing) (from m) + >>= applyDelta (change m) + >>= display (displayOpts m) diff --git a/Partly/View.hs b/Partly/View.hs index 93cee57..b23f7f1 100644 --- a/Partly/View.hs +++ b/Partly/View.hs @@ -2,8 +2,6 @@ module Partly.View where -- base: import Control.Applicative import Text.Printf --- bytestring: -import qualified Data.ByteString as B -- optparse-applicative: import Options.Applicative -- partly: @@ -11,11 +9,13 @@ import System.Disk.Partitions.MBR import Partly.Json import Partly.Common +-- | Parse the options we can use for "partly view json". viewJsonOptions :: Parser (Input, Display) viewJsonOptions = (,) <$> parseInput "The file to parse and inspect." <*> parseDisplay +-- | Turn those options into an IO action. viewJson :: (Input, Display) -> IO () viewJson (i, d) = input i >>= display d @@ -27,10 +27,12 @@ fieldCommand s fn m = command s . flip info m $ <$> parseInput "The file to parse and inspect." <*> ((. fn) <$> output <$> parseOutput) +-- | The type that the main program will hand to us. data ViewCommand = ViewJson (Input, Display) | ViewField Input (BootRecord -> IO ()) +-- | Combine all the commands together. viewParser :: ParserInfo ViewCommand viewParser = info ( subparser @@ -45,6 +47,7 @@ viewParser = info ( progDesc "Inspect a boot record." & fullDesc ) +-- | Execute the "view" command. view :: ViewCommand -> IO () view c = case c of ViewJson vj -> viewJson vj