<commit_before>module Node.FS.Perms
  ( Perm()
  , none
  , read
  , write
  , execute
  , all
  , Perms()
  , mkPerms
  , permsFromString
  , permsToString
  , permsToInt
  ) where

import Global (readInt)
import Data.Array () -- for Semigroup and Functor instances
import Data.Maybe (Maybe(..))
import Data.Char (Char(), charString, fromCharCode)
import Data.String (toCharArray, joinWith, drop, charAt)
import Data.Int (Int(), fromNumber, toNumber)

-- | A `Perm` value specifies what is allowed to be done with a particular
-- | file by a particular class of user &mdash; that is, whether it is
-- | readable, writable, and/or executable. It has a semigroup instance, which
-- | allows you to combine permissions; for example, `read <> write` means
-- | "readable and writable".
newtype Perm = Perm { r :: Boolean, w :: Boolean, x :: Boolean }

instance eqPerm :: Eq Perm where
  (==) (Perm { r = r1, w = w1, x = x1 }) (Perm { r = r2, w = w2, x = x2 }) =
    r1 == r2  && w1 == w2 && x1 == x2
  (/=) x y = not (x == y)

instance ordPerm :: Ord Perm where
  compare (Perm { r = r1, w = w1, x = x1 }) (Perm { r = r2, w = w2, x = x2 }) =
    compare [r1, w1, x1] [r2, w2, x2]

instance showPerm :: Show Perm where
  show p | p == none = "none"
  show p | p == all  = "all"
  show (Perm { r = r, w = w, x = x }) =
    joinWith " <> " ps
    where
    ps =
      (if r then ["read"] else []) <>
      (if w then ["write"] else []) <>
      (if x then ["execute"] else [])

instance semigroupPerm :: Semigroup Perm where
  (<>) (Perm { r = r0, w = w0, x = x0 }) (Perm { r = r1, w = w1, x = x1 }) =
    Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1  }

-- | Create a `Perm` value. The arguments represent the readable, writable, and
-- | executable permissions, in that order.
mkPerm :: Boolean -> Boolean -> Boolean -> Perm
mkPerm r w x = Perm { r: r, w: w, x: x }

-- | No permissions. This is the identity of the `Semigroup` operation `(<>)`
-- | for `Perm`.
none :: Perm
none = Perm { r: false, w: false, x: false }

-- | The "readable" permission.
read :: Perm
read = Perm { r: true, w: false, x: false }

-- | The "writable" permission.
write :: Perm
write = Perm { r: false, w: true, x: false }

-- | The "executable" permission.
execute :: Perm
execute = Perm { r: false, w: false, x: true }

-- | All permissions: readable, writable, and executable.
all :: Perm
all = read <> write <> execute

-- | A `Perms` value includes all the permissions information about a
-- | particular file or directory, by storing a `Perm` value for each of the
-- | file owner, the group, and any other users.
newtype Perms = Perms { u :: Perm, g :: Perm, o :: Perm }

instance eqPerms :: Eq Perms where
  (==) (Perms { u = u1, g = g1, o = o1 }) (Perms { u = u2, g = g2, o = o2 }) =
    u1 == u2  && g1 == g2 && o1 == o2
  (/=) x y = not (x == y)

instance ordPerms :: Ord Perms where
  compare (Perms { u = u1, g = g1, o = o1 }) (Perms { u = u2, g = g2, o = o2 }) =
    compare [u1, g1, o1] [u2, g2, o2]

instance showPerms :: Show Perms where
  show (Perms { u = u, g = g, o = o }) =
    "mkPerms " <> joinWith " " (f <$> [u, g, o])
    where
    f perm = "(" <> show perm <> ")"

-- | Attempt to parse a `Perms` value from a `String` containing an octal
-- | integer. For example,
-- | `permsFromString "0644" == Just (mkPerms (read <> write) read read)`.
permsFromString :: String -> Maybe Perms
permsFromString = _perms <<< toCharArray <<< dropPrefix zeroChar
  where
    zeroChar = fromCharCode 48

    dropPrefix x xs
      | charAt 0 xs == Just x = drop 1 xs
      | otherwise             = xs

    _perms [u, g, o] =<commit_msg>Only bracket if necessary in showPerms<commit_after>module Node.FS.Perms
  ( Perm()
  , none
  , read
  , write
  , execute
  , all
  , Perms()
  , mkPerms
  , permsFromString
  , permsToString
  , permsToInt
  ) where

import Global (readInt)
import Data.Array () -- for Semigroup and Functor instances
import Data.Maybe (Maybe(..))
import Data.Char (Char(), charString, fromCharCode)
import Data.String (toCharArray, joinWith, drop, charAt, indexOf)
import Data.Int (Int(), fromNumber, toNumber)

-- | A `Perm` value specifies what is allowed to be done with a particular
-- | file by a particular class of user &mdash; that is, whether it is
-- | readable, writable, and/or executable. It has a semigroup instance, which
-- | allows you to combine permissions; for example, `read <> write` means
-- | "readable and writable".
newtype Perm = Perm { r :: Boolean, w :: Boolean, x :: Boolean }

instance eqPerm :: Eq Perm where
  (==) (Perm { r = r1, w = w1, x = x1 }) (Perm { r = r2, w = w2, x = x2 }) =
    r1 == r2  && w1 == w2 && x1 == x2
  (/=) x y = not (x == y)

instance ordPerm :: Ord Perm where
  compare (Perm { r = r1, w = w1, x = x1 }) (Perm { r = r2, w = w2, x = x2 }) =
    compare [r1, w1, x1] [r2, w2, x2]

instance showPerm :: Show Perm where
  show p | p == none = "none"
  show p | p == all  = "all"
  show (Perm { r = r, w = w, x = x }) =
    joinWith " <> " ps
    where
    ps =
      (if r then ["read"] else []) <>
      (if w then ["write"] else []) <>
      (if x then ["execute"] else [])

instance semigroupPerm :: Semigroup Perm where
  (<>) (Perm { r = r0, w = w0, x = x0 }) (Perm { r = r1, w = w1, x = x1 }) =
    Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1  }

-- | Create a `Perm` value. The arguments represent the readable, writable, and
-- | executable permissions, in that order.
mkPerm :: Boolean -> Boolean -> Boolean -> Perm
mkPerm r w x = Perm { r: r, w: w, x: x }

-- | No permissions. This is the identity of the `Semigroup` operation `(<>)`
-- | for `Perm`.
none :: Perm
none = Perm { r: false, w: false, x: false }

-- | The "readable" permission.
read :: Perm
read = Perm { r: true, w: false, x: false }

-- | The "writable" permission.
write :: Perm
write = Perm { r: false, w: true, x: false }

-- | The "executable" permission.
execute :: Perm
execute = Perm { r: false, w: false, x: true }

-- | All permissions: readable, writable, and executable.
all :: Perm
all = read <> write <> execute

-- | A `Perms` value includes all the permissions information about a
-- | particular file or directory, by storing a `Perm` value for each of the
-- | file owner, the group, and any other users.
newtype Perms = Perms { u :: Perm, g :: Perm, o :: Perm }

instance eqPerms :: Eq Perms where
  (==) (Perms { u = u1, g = g1, o = o1 }) (Perms { u = u2, g = g2, o = o2 }) =
    u1 == u2  && g1 == g2 && o1 == o2
  (/=) x y = not (x == y)

instance ordPerms :: Ord Perms where
  compare (Perms { u = u1, g = g1, o = o1 }) (Perms { u = u2, g = g2, o = o2 }) =
    compare [u1, g1, o1] [u2, g2, o2]

instance showPerms :: Show Perms where
  show (Perms { u = u, g = g, o = o }) =
    "mkPerms " <> joinWith " " (f <$> [u, g, o])
    where
      f perm = let str = show perm
               in  if indexOf " " str == -1
                     then str
                     else "(" <> str <> ")"

-- | Attempt to parse a `Perms` value from a `String` containing an octal
-- | integer. For example,
-- | `permsFromString "0644" == Just (mkPerms (read <> write) read read)`.
permsFromString :: String -> Maybe Perms
permsFromString = _perms <<< toCharArray <<< dropPrefix zeroChar
  where
    zeroChar = fromCharCode 48

    dropPrefix x xs
      | charAt 0 xs == Just x = drop 1 xs
      | otherwise             = xs

    _perms [u, g, o] =
{'fraction_non_alphanumeric': 0.1570192047606167, 'fraction_numerical': 0.018528536651338922, 'mean_word_length': 2.6374815543531724, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2449602', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2600, 'n_words': 1033}