added errorhandling for base62 encoding

This commit is contained in:
Stefan Friese 2024-05-12 23:16:21 +02:00
parent 6972d08ff7
commit fd6b5dfd16
1 changed files with 12 additions and 3 deletions

15
Main.hs
View File

@ -213,14 +213,23 @@ dec62 input =
-- maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input)))
stringToInt :: String -> Maybe Integer
stringToInt str = readMaybe str
stringToInt = readMaybe
enc62 :: String -> String
-- enc62 :: String -> String
-- enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt)))
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . (B62.encode128 . (fromIntegral . ( BSU.fromString . fromJust StringToInt))))
enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . Bytes.fromByteArray . B62.encode64 . fromIntegral . fromJust . stringToInt
-- working correct:
-- enc62 :: String -> String
-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . Bytes.fromByteArray . B62.encode64 . fromIntegral . fromJust . stringToInt
enc62 :: String -> String
enc62 input =
let intValue = fromMaybe (error "Error: Unable to convert input string to integer") (stringToInt input)
encoded = B62.encode64 (fromIntegral intValue)
encodedText = T.decodeUtf8 (BSU.fromString (Latin1.toString (Bytes.fromByteArray encoded)))
in T.unpack encodedText
-- enc62 :: Text -> Text
-- enc62 text = B62.text128 (fromIntegral (T.length text))