2026-03-24 14:34:09 +01:00
|
|
|
module Main (main) where
|
|
|
|
|
|
|
|
|
|
import Interop.Shared (Summary (..))
|
2026-03-27 09:20:31 +01:00
|
|
|
import RustClient (
|
|
|
|
|
callRustByteChecksum,
|
|
|
|
|
callRustBytePattern,
|
|
|
|
|
callRustMessage,
|
|
|
|
|
callRustSequence,
|
|
|
|
|
callRustSliceSum,
|
|
|
|
|
callRustSummary,
|
|
|
|
|
)
|
2026-03-24 14:34:09 +01:00
|
|
|
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
|
2026-03-27 09:20:31 +01:00
|
|
|
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
|
2026-03-24 14:34:09 +01:00
|
|
|
|
|
|
|
|
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
|
2026-03-27 09:20:31 +01:00
|
|
|
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
|
2026-03-24 14:34:09 +01:00
|
|
|
|
|
|
|
|
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)
|