/Main.hs Secret
Created
January 24, 2021 23:34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Char ( isLower, isUpper ) | |
import Control.Monad.Trans.Maybe | |
import Control.Monad.Trans.Reader | |
import Control.Monad.Trans.Class | |
type Env = (Maybe String, Maybe String, Maybe String) | |
main :: IO () | |
main = do | |
maybeCreds <- runMaybeT $ do | |
usr <- runReaderT readUserName (Nothing, Nothing, Nothing) | |
email <- readEmail | |
pass <- readPassword | |
return (usr, email, pass) | |
case maybeCreds of | |
Nothing -> print "Could not log in." | |
Just (u, e, p) -> print $ "Logging in: "++u++e++p | |
readUserName :: MaybeT (ReaderT Env IO) String | |
readUserName = MaybeT $ do | |
(maybeOldUser, _, _ ) <- ask | |
case maybeOldUser of | |
Just str -> return $ Just str | |
Nothing -> do | |
lift $ putStrLn "Please enter your username: " | |
input <- lift getLine | |
if length input > 5 | |
then return $ Just input | |
else return Nothing | |
readEmail :: MaybeT IO String | |
readEmail = MaybeT $ do | |
putStrLn "Please enter your email: " | |
str <- getLine | |
if '@' `elem` str && '.' `elem` str | |
then return $ Just str | |
else return Nothing | |
readPassword :: MaybeT IO String | |
readPassword = MaybeT $ do | |
putStrLn "Please enter your password: " | |
str <- getLine | |
if length str < 8 || not (any isUpper str) || not (any isLower str) | |
then return Nothing | |
else return $ Just str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment