11{-# LANGUAGE TypeApplications #-}
2+ {-# LANGUAGE TypeFamilies #-}
3+
4+ -- The DefaultIn instances for the generated enums below are orphans: the enum types
5+ -- and the class are both defined elsewhere. The natural home is the generated module
6+ -- (ideally hs-bindgen would emit them), so silence the expected warning here.
7+ {-# OPTIONS_GHC -Wno-orphans #-}
28
39-- | High-level wrappers around the generated qrcodegen bindings.
410--
@@ -11,21 +17,33 @@ module HighLevel (
1117import Data.Word (Word8 )
1218import Foreign.Ptr (Ptr )
1319
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 )
20+ import HsBindgen.Runtime.HighLevel (output , scratchArray , toHighLevel )
21+ import HsBindgen.Runtime.HighLevel.Defaults (DefaultIn (.. ), auto )
22+ import HsBindgen.Runtime.HighLevel.Marshaller (Unmarshaller , scalar )
23+ import HsBindgen.Runtime.HighLevel.Marshaller.Utils (peekIncompleteArrayOut )
2124import HsBindgen.Runtime.IncompleteArray (IncompleteArray )
2225
2326import QRCodeGenerator.Generated qualified as QR
2427import QRCodeGenerator.Generated.Safe qualified as QR
2528
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.
29+ -- | Passthrough defaults for the generated enums: each enum is its own C argument
30+ -- type, so the default marshaller is @scalar id@. With these in scope, 'auto' can
31+ -- fill the @ecc@ and @mask@ arguments of 'encodeText'. These are orphan instances;
32+ -- the natural home is alongside the enum in the generated module, so ideally
33+ -- hs-bindgen would emit them.
34+ instance DefaultIn QR. Qrcodegen_Ecc where
35+ type DefInArrow QR. Qrcodegen_Ecc lo = QR. Qrcodegen_Ecc -> lo
36+ defaultIn = scalar id
37+
38+ instance DefaultIn QR. Qrcodegen_Mask where
39+ type DefInArrow QR. Qrcodegen_Mask lo = QR. Qrcodegen_Mask -> lo
40+ defaultIn = scalar id
41+
42+ -- | Lift @qrcodegen_encodeText@. Only the two positions that need a human decision
43+ -- are explicit: @tempBuffer@ is a 'scratchArray' (the callee writes it, the caller
44+ -- never sees it) and @qrcode@ is the 'output' we keep. 'auto' fills the rest from
45+ -- the signature: the leading 'String', then the two enums, the version 'Int's, the
46+ -- boost 'Bool', and the result.
2947encodeText
3048 :: String
3149 -> QR. Qrcodegen_Ecc
@@ -35,33 +53,22 @@ encodeText
3553 -> Bool
3654 -> IO (IncompleteArray Word8 , Bool )
3755encodeText = toHighLevel
38- ( input withCStringIn
56+ ( auto -- text (String)
3957 $ 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
58+ $ output qrCodeOut -- qrcode: the out-parameter we keep
59+ $ auto -- ecc, minVersion, maxVersion, mask, boostEcl, result
4760 ) QR. qrcodegen_encodeText
4861 where
4962 maxLen :: Int
5063 maxLen = fromIntegral QR. qrcodegen_BUFFER_LEN_MAX
5164
52- qrCodeOut :: OutMarshaller (Ptr Word8 ) (IncompleteArray Word8 )
65+ qrCodeOut :: Unmarshaller (Ptr Word8 ) (IncompleteArray Word8 )
5366 qrCodeOut = peekIncompleteArrayOut maxLen
5467
68+ -- | Every position is a default ('IncompleteArray' marshals as a @const@ pointer,
69+ -- 'CInt' \/ 'CBool' scalars), so the whole wrapper is 'auto'.
5570getSize :: IncompleteArray Word8 -> IO Int
56- getSize = toHighLevel
57- ( input withConstIncompleteArrayIn
58- $ defaultRes
59- ) QR. qrcodegen_getSize
71+ getSize = toHighLevel auto QR. qrcodegen_getSize
6072
6173getModule :: IncompleteArray Word8 -> Int -> Int -> IO Bool
62- getModule = toHighLevel
63- ( input withConstIncompleteArrayIn
64- $ input defaultIn -- x (Int -> CInt)
65- $ input defaultIn -- y (Int -> CInt)
66- $ defaultRes -- CBool -> Bool
67- ) QR. qrcodegen_getModule
74+ getModule = toHighLevel auto QR. qrcodegen_getModule
0 commit comments