68 lines
2.1 KiB
Haskell
Raw Normal View History

module Main (main) where
import Interop.Shared (Summary (..))
import RustClient (
callRustByteChecksum,
callRustBytePattern,
callRustMessage,
callRustSequence,
callRustSliceSum,
callRustSummary,
)
import System.Environment (getArgs)
import Text.Read (readMaybe)
main :: IO ()
main = do
args <- getArgs
let (name, left, right) = parseArgs args
summary <- callRustSummary left right
message <- callRustMessage name left right
let sliceValues = [2, 4, 6, 8]
sliceSum <- callRustSliceSum sliceValues
sequenceValues <- callRustSequence left 5
let byteValues = [72, 0, 105, 255]
byteChecksum <- callRustByteChecksum byteValues
bytePattern <- callRustBytePattern left 6
putStrLn "Haskell -> Rust demo"
putStrLn $ "Inputs: name=" ++ name ++ ", left=" ++ show left ++ ", right=" ++ show right
putStrLn $ "Stats from Rust: " ++ renderSummary summary
putStrLn $ "Message from Rust: " ++ message
putStrLn $ "Slice sent to Rust: " ++ show sliceValues
putStrLn $ "Rust summed slice to: " ++ show sliceSum
putStrLn $ "Vector returned from Rust: " ++ show sequenceValues
putStrLn $ "Byte slice sent to Rust: " ++ show byteValues
putStrLn $ "Rust checksummed bytes to: " ++ show byteChecksum
putStrLn $ "Byte buffer returned from Rust: " ++ show bytePattern
parseArgs :: [String] -> (String, Int, Int)
parseArgs args =
let name = case args of
value : _ -> value
[] -> "Ada"
left = maybe 7 id (pickNumber 1 args)
right = maybe 5 id (pickNumber 2 args)
in (name, left, right)
pickNumber :: Int -> [String] -> Maybe Int
pickNumber index values = do
value <- safeIndex index values
readMaybe value
safeIndex :: Int -> [a] -> Maybe a
safeIndex index values
| index < 0 = Nothing
| otherwise = case drop index values of
value : _ -> Just value
[] -> Nothing
renderSummary :: Summary -> String
renderSummary summary =
"total="
++ show (total summary)
++ ", product="
++ show (combinedProduct summary)
++ ", gap="
++ show (gap summary)