|
| 1 | +{-# LANGUAGE TypeApplications #-} |
| 2 | + |
1 | 3 | -- | High-level wrappers around the generated qrcodegen bindings. |
| 4 | +-- |
2 | 5 | module HighLevel ( |
3 | 6 | encodeText |
4 | 7 | , getSize |
5 | 8 | , getModule |
6 | 9 | ) where |
7 | 10 |
|
8 | 11 | import Data.Word (Word8) |
9 | | -import Foreign.Marshal.Utils qualified as Marshal |
10 | 12 | import Foreign.Ptr (Ptr) |
11 | 13 |
|
12 | | -import HsBindgen.Runtime.HighLevel.Refine (OutMarshaller, input, output, |
13 | | - peekIncompleteArrayOut, pureIn, |
14 | | - pureRes, result, scratchOut, |
15 | | - toHighLevel, withCStringIn, |
16 | | - withConstIncompleteArrayIn) |
| 14 | +import HsBindgen.Runtime.HighLevel.ToHighLevel (OutMarshaller, input, output, |
| 15 | + pureIn, scratchArray, |
| 16 | + toHighLevel) |
| 17 | +import HsBindgen.Runtime.HighLevel.ToHighLevel.Defaults (defaultIn, defaultRes) |
| 18 | +import HsBindgen.Runtime.HighLevel.ToHighLevel.Marshallers (peekIncompleteArrayOut, |
| 19 | + withCStringIn, |
| 20 | + withConstIncompleteArrayIn) |
17 | 21 | import HsBindgen.Runtime.IncompleteArray (IncompleteArray) |
18 | 22 |
|
19 | 23 | import QRCodeGenerator.Generated qualified as QR |
20 | 24 | import QRCodeGenerator.Generated.Safe qualified as QR |
21 | 25 |
|
22 | | --- | Lift @qrcodegen_encodeText@. Destructure the leading scratch @()@ at |
23 | | --- the call site. |
| 26 | +-- | Lift @qrcodegen_encodeText@. @tempBuffer@ is a 'scratchArray', so it adds |
| 27 | +-- nothing to the result: only the QR code and the success flag come back. The |
| 28 | +-- scalar inputs and the result use default marshallers. |
24 | 29 | encodeText |
25 | 30 | :: String |
26 | 31 | -> QR.Qrcodegen_Ecc |
27 | 32 | -> Int |
28 | 33 | -> Int |
29 | 34 | -> QR.Qrcodegen_Mask |
30 | 35 | -> Bool |
31 | | - -> IO ((), (IncompleteArray Word8, Bool)) |
| 36 | + -> IO (IncompleteArray Word8, Bool) |
32 | 37 | encodeText = toHighLevel |
33 | | - ( input withCStringIn |
34 | | - $ output tempScratch |
35 | | - $ output qrCodeOut |
36 | | - $ input (pureIn id) |
37 | | - $ input (pureIn fromIntegral) |
38 | | - $ input (pureIn fromIntegral) |
39 | | - $ input (pureIn id) |
40 | | - $ input (pureIn Marshal.fromBool) |
41 | | - $ result (pureRes Marshal.toBool) |
| 38 | + ( input withCStringIn |
| 39 | + $ scratchArray @Word8 maxLen -- tempBuffer: written, never read |
| 40 | + $ output qrCodeOut |
| 41 | + $ input (pureIn id) -- ecc level (enum, passthrough) |
| 42 | + $ input defaultIn -- minVersion (Int -> CInt) |
| 43 | + $ input defaultIn -- maxVersion (Int -> CInt) |
| 44 | + $ input (pureIn id) -- mask (enum, passthrough) |
| 45 | + $ input defaultIn -- boostEcl (Bool -> CBool) |
| 46 | + $ defaultRes -- CBool -> Bool |
42 | 47 | ) QR.qrcodegen_encodeText |
43 | 48 | where |
44 | 49 | maxLen :: Int |
45 | 50 | maxLen = fromIntegral QR.qrcodegen_BUFFER_LEN_MAX |
46 | 51 |
|
47 | | - tempScratch :: OutMarshaller (Ptr Word8) () |
48 | | - tempScratch = scratchOut maxLen |
49 | | - |
50 | 52 | qrCodeOut :: OutMarshaller (Ptr Word8) (IncompleteArray Word8) |
51 | 53 | qrCodeOut = peekIncompleteArrayOut maxLen |
52 | 54 |
|
53 | 55 | getSize :: IncompleteArray Word8 -> IO Int |
54 | 56 | getSize = toHighLevel |
55 | 57 | ( input withConstIncompleteArrayIn |
56 | | - $ result (pureRes fromIntegral) |
| 58 | + $ defaultRes |
57 | 59 | ) QR.qrcodegen_getSize |
58 | 60 |
|
59 | 61 | getModule :: IncompleteArray Word8 -> Int -> Int -> IO Bool |
60 | 62 | getModule = toHighLevel |
61 | 63 | ( input withConstIncompleteArrayIn |
62 | | - $ input (pureIn fromIntegral) |
63 | | - $ input (pureIn fromIntegral) |
64 | | - $ result (pureRes Marshal.toBool) |
| 64 | + $ input defaultIn -- x (Int -> CInt) |
| 65 | + $ input defaultIn -- y (Int -> CInt) |
| 66 | + $ defaultRes -- CBool -> Bool |
65 | 67 | ) QR.qrcodegen_getModule |
0 commit comments