Skip to content

Commit

Permalink
Added doc comments everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
startling committed Dec 5, 2012
1 parent f0e5fe7 commit 855f501
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
41 changes: 23 additions & 18 deletions Partly/Make.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
7 changes: 5 additions & 2 deletions Partly/View.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ module Partly.View where
-- base:
import Control.Applicative
import Text.Printf
-- bytestring:
import qualified Data.ByteString as B
-- optparse-applicative:
import Options.Applicative
-- partly:
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

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 855f501

Please sign in to comment.