@@ -26,11 +26,12 @@ import Data.Int (Int(), fromNumber, toNumber)
2626newtype Perm = Perm { r :: Boolean , w :: Boolean , x :: Boolean }
2727
2828-- | A `Perms` value includes all the permissions information about a
29- -- | particular file or directory,
29+ -- | particular file or directory, by storing a `Perm` value for each of the
30+ -- | file owner, the group, and others.
3031newtype Perms = Perms { u :: Perm , g :: Perm , o :: Perm }
3132
32- -- | No permissions. This is the identity of the Semigroup (<>) operation for
33- -- | Perm.
33+ -- | No permissions. This is the identity of the ` Semigroup` operation ` (<>)`
34+ -- | for ` Perm` .
3435none :: Perm
3536none = Perm { r: false , w: false , x: false }
3637
@@ -54,10 +55,13 @@ instance semigroupPerm :: Semigroup Perm where
5455 (<>) (Perm { r = r0, w = w0, x = x0 }) (Perm { r = r1, w = w1, x = x1 }) =
5556 Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1 }
5657
58+ -- | Attempt to parse a `Perms` value from a `String` containing an octal
59+ -- | integer. For example,
60+ -- | `permsFromString "644" == Just (mkPerms (r <> w) r r)`.
5761permsFromString :: String -> Maybe Perms
5862permsFromString = _perms <<< toCharArray
5963 where
60- _perms (u : g : o : [] ) =
64+ _perms [u, g, o] =
6165 mkPerms <$> permFromChar u
6266 <*> permFromChar g
6367 <*> permFromChar o
@@ -77,7 +81,7 @@ permFromChar = _perm <<< charString
7781 _perm _ = Nothing
7882
7983-- | Create a `Perm` value. The arguments represent the readable, writable, and
80- -- | executable permissions respectively .
84+ -- | executable permissions, in that order .
8185mkPerm :: Boolean -> Boolean -> Boolean -> Perm
8286mkPerm r w x = Perm { r: r, w: w, x: x }
8387
@@ -86,15 +90,25 @@ mkPerm r w x = Perm { r: r, w: w, x: x }
8690mkPerms :: Perm -> Perm -> Perm -> Perms
8791mkPerms u g o = Perms { u: u, g: g, o: o }
8892
93+ -- | Convert a `Perm` to an octal digit. For example:
94+ -- |
95+ -- | * `permToInt r == 4`
96+ -- | * `permToInt w == 2`
97+ -- | * `permToInt (r <> w) == 6`
8998permToInt :: Perm -> Int
9099permToInt (Perm { r = r, w = w, x = x }) = fromNumber $
91100 (if r then 4 else 0 )
92101 + (if w then 2 else 0 )
93102 + (if x then 1 else 0 )
94103
104+ -- | Convert a `Perm` to an octal string, via `permToInt`.
95105permToString :: Perm -> String
96106permToString = show <<< toNumber <<< permToInt
97107
108+ -- | Convert a `Perms` value to an octal string, in a format similar to that
109+ -- | accepted by `chmod`. For example:
110+ -- |
111+ -- | * `permsToString (mkPerms (r <> w) r r) == "0644"`
98112permsToString :: Perms -> String
99113permsToString (Perms { u = u, g = g, o = o }) =
100114 " 0"
0 commit comments