module Ribosome.App.ProjectNames where

import Path (parseRelDir)

import Ribosome.App.Data (ModuleName (ModuleName), ProjectName (ProjectName), ProjectNames (..))
import Ribosome.Host.Text (pascalCase)

parse ::
  IsString err =>
  String ->
  Either err ProjectNames
parse :: forall err. IsString err => String -> Either err ProjectNames
parse String
raw = do
  let
    name :: ProjectName
name = Text -> ProjectName
ProjectName (String -> Text
forall a. ToText a => a -> Text
toText String
raw)
    modRaw :: String
modRaw = String -> String
forall a b. (ToString a, IsString b) => a -> b
pascalCase String
raw
    moduleName :: ModuleName
moduleName = Text -> ModuleName
ModuleName (String -> Text
forall a. ToText a => a -> Text
toText String
modRaw)
  Path Rel Dir
nameDir <- Either err (Path Rel Dir)
-> (Path Rel Dir -> Either err (Path Rel Dir))
-> Maybe (Path Rel Dir)
-> Either err (Path Rel Dir)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either err (Path Rel Dir)
forall {b}. Either err b
err Path Rel Dir -> Either err (Path Rel Dir)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Maybe (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => String -> m (Path Rel Dir)
parseRelDir String
raw)
  Path Rel Dir
moduleNameDir <- Either err (Path Rel Dir)
-> (Path Rel Dir -> Either err (Path Rel Dir))
-> Maybe (Path Rel Dir)
-> Either err (Path Rel Dir)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Either err (Path Rel Dir)
forall {b}. Either err b
err Path Rel Dir -> Either err (Path Rel Dir)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Maybe (Path Rel Dir)
forall (m :: * -> *). MonadThrow m => String -> m (Path Rel Dir)
parseRelDir String
modRaw)
  pure ProjectNames :: ProjectName
-> Path Rel Dir -> ModuleName -> Path Rel Dir -> ProjectNames
ProjectNames {Path Rel Dir
ModuleName
ProjectName
$sel:moduleNameDir:ProjectNames :: Path Rel Dir
$sel:moduleName:ProjectNames :: ModuleName
$sel:nameDir:ProjectNames :: Path Rel Dir
$sel:name:ProjectNames :: ProjectName
moduleNameDir :: Path Rel Dir
nameDir :: Path Rel Dir
moduleName :: ModuleName
name :: ProjectName
..}
  where
    err :: Either err b
err =
      err -> Either err b
forall a b. a -> Either a b
Left err
"The project name must be usable as a directory name"