Skip to content

Commit c2c1f35

Browse files
committed
unicode refactoring wip
1 parent bf217ba commit c2c1f35

File tree

16 files changed

+317
-163
lines changed

16 files changed

+317
-163
lines changed

ghcjs/lightning-verifier/src/App/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ stQuery st = do
224224
encodeText :: (MonadThrow m) => BL.ByteString -> m Prelude.Text
225225
encodeText =
226226
either throw pure
227-
. decodeUtf8'
227+
. decodeUtf8Strict
228228
. B64URL.encode
229229
. from @BL.ByteString @ByteString
230230

ghcjs/miso-widgets/src/Functora/Miso/Prelude.hs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ module Functora.Miso.Prelude
88
)
99
where
1010

11-
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
12-
import qualified Data.Binary as Binary (get, put)
13-
#endif
1411
import Functora.Cfg as X
1512
import Functora.Miso.Orphan as X ()
1613
import Functora.Prelude as X hiding
@@ -40,23 +37,6 @@ import Miso.String as X
4037
)
4138
import Type.Reflection
4239

43-
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
44-
instance Binary MisoString where
45-
put = Binary.put . fromMisoString @Prelude.Text
46-
get = fmap (toMisoString @Prelude.Text) Binary.get
47-
48-
instance ConvertUtf8 MisoString ByteString where
49-
encodeUtf8 = encodeUtf8 . fromMisoString @Prelude.String
50-
decodeUtf8 = toMisoString @Prelude.String . decodeUtf8
51-
decodeUtf8Strict = fmap (toMisoString @Prelude.String) . decodeUtf8Strict
52-
53-
instance ToJSONKey MisoString where
54-
toJSONKey = contramap (fromMisoString @Prelude.Text) $ toJSONKey
55-
56-
instance FromJSONKey MisoString where
57-
fromJSONKey = fmap toMisoString $ fromJSONKey @Prelude.Text
58-
#endif
59-
6040
instance (ToMisoString a) => ToMisoString (Tagged "UTF-8" a) where
6141
toMisoString = toMisoString . unTagged
6242

ghcjs/miso-widgets/src/Functora/Miso/Types.hs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,13 @@ newPaymentMethod ::
485485
m (PaymentMethod Unique)
486486
newPaymentMethod cur addr0 = do
487487
lbl <-
488-
newTextField
489-
. toMisoString @Prelude.Text
490-
$ inspectCurrencyInfo cur
491-
<> " total"
488+
newTextField $ inspectCurrencyInfo cur <> " total"
492489
addr1 <-
493490
maybe
494491
( pure Nothing
495492
)
496493
( fmap (Just . (& #fieldPairValue . #fieldType .~ FieldTypeQrCode))
497-
. newFieldPair
498-
( toMisoString @Prelude.Text
499-
$ inspectCurrencyInfo cur
500-
<> " address"
501-
)
494+
. newFieldPair (inspectCurrencyInfo cur <> " address")
502495
. DynamicFieldText
503496
)
504497
addr0

ghcjs/miso-widgets/src/Functora/Miso/Widgets/Currency.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import Functora.Miso.Prelude
1212
import Functora.Miso.Types
1313
import qualified Functora.Miso.Widgets.Field as Field
1414
import Functora.Money hiding (Currency, Money, Text)
15-
import qualified Functora.Prelude as Prelude
1615
import qualified Material.Button as Button
1716
import qualified Material.Dialog as Dialog
1817
import qualified Material.LayoutGrid as LayoutGrid
@@ -58,7 +57,6 @@ selectCurrency
5857
]
5958
$ Button.config
6059
)
61-
. toMisoString @Prelude.Text
6260
. inspectCurrencyInfo
6361
$ fromMaybe
6462
(CurrencyInfo (CurrencyCode "XXX") mempty)
@@ -153,9 +151,7 @@ currencyListWidget
153151
newFuzz cur =
154152
Fuzzy.Fuzzy
155153
{ Fuzzy.original = cur,
156-
Fuzzy.rendered =
157-
toMisoString @Prelude.Text
158-
$ inspectCurrencyInfo cur,
154+
Fuzzy.rendered = inspectCurrencyInfo cur,
159155
Fuzzy.score = 0
160156
}
161157
search =
@@ -182,8 +178,7 @@ currencyListWidget
182178
)
183179
"<b>"
184180
"</b>"
185-
( toMisoString @Prelude.Text . inspectCurrencyInfo
186-
)
181+
inspectCurrencyInfo
187182
False
188183

189184
currencyListItemWidget ::

ghcjs/miso-widgets/src/Functora/Miso/Widgets/Money.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,8 @@ moneyViewer Args {argsModel = st, argsOptic = optic, argsAction = action} opts =
6666
)
6767
( Field.defOpts
6868
& #optsPlaceholder
69-
.~ toMisoString @Prelude.Text
70-
( inspectCurrencyInfo
71-
$ money
69+
.~ inspectCurrencyInfo
70+
( money
7271
^. #moneyCurrency
7372
. #currencyOutput
7473
)

prv

Submodule prv updated from 46a0347 to a3e0782

pub/functora/functora.cabal

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ common pkg-prelude
123123
, with-utf8
124124

125125
if ((impl(ghcjs) || arch(javascript)) || os(wasi))
126-
build-depends: ghcjs-base
126+
build-depends:
127+
, aeson
128+
, jsaddle
129+
, miso
127130

128131
common pkg-qr
129132
import: pkg
@@ -206,7 +209,6 @@ common pkg-money
206209
, cfg
207210
, tags
208211
, template-haskell
209-
, text
210212

211213
common pkg-rates
212214
import: pkg
@@ -258,7 +260,6 @@ common pkg-bolt11
258260
, binary
259261
, bitcoin-address
260262
, bytestring
261-
, text
262263

263264
library
264265
import: pkg-prelude

pub/functora/src/bolt11/Functora/Bolt11.hs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Functora.Bolt11
2020
Bolt11Hrp (..),
2121
Bolt11Sig (..),
2222
Bolt11 (..),
23-
inspectW5,
2423
decodeBolt11,
2524
)
2625
where
@@ -40,7 +39,6 @@ import qualified Data.ByteString as BS
4039
import qualified Data.ByteString.Base16 as B16
4140
import qualified Data.ByteString.Builder as BS
4241
import qualified Data.ByteString.Lazy.Char8 as BL
43-
import qualified Data.Text.Lazy as TL
4442
import Functora.Prelude hiding (error)
4543
import Prelude (Show (..), error)
4644

@@ -50,11 +48,9 @@ newtype Hex = Hex
5048
deriving stock (Eq, Ord, Data, Generic)
5149

5250
instance Show Hex where
53-
show =
54-
from @TL.Text @String
55-
. inspectHex
51+
show = inspectHex @String
5652

57-
inspectHex :: Hex -> TL.Text
53+
inspectHex :: (Textual a) => Hex -> a
5854
inspectHex =
5955
decodeUtf8
6056
. BS.toLazyByteString
@@ -82,7 +78,7 @@ instance IsString Hex where
8278
data Tag
8379
= PaymentHash Hex
8480
| PaymentSecret Hex
85-
| Description Text
81+
| Description Unicode
8682
| AdditionalMetadata [Word5]
8783
| PayeePubkey Hex
8884
| DescriptionHash Hex
@@ -208,18 +204,18 @@ data Route = Route
208204
instance A.ToJSON Route where
209205
toJSON x =
210206
A.object
211-
[ "pubkey" A..= inspectHex (routePubKey x),
212-
"short_channel_id" A..= inspectHex (routeShortChanId x),
207+
[ "pubkey" A..= inspectHex @String (routePubKey x),
208+
"short_channel_id" A..= inspectHex @String (routeShortChanId x),
213209
"fee_base_msat" A..= routeFeeBaseMsat x,
214210
"fee_proportional_millionths" A..= routeFeePropMillionth x,
215211
"cltv_expiry_delta" A..= routeCltvExpiryDelta x
216212
]
217213
toEncoding x =
218214
A.pairs
219215
$ "pubkey"
220-
A..= inspectHex (routePubKey x)
216+
A..= inspectHex @String (routePubKey x)
221217
<> "short_channel_id"
222-
A..= inspectHex (routeShortChanId x)
218+
A..= inspectHex @String (routeShortChanId x)
223219
<> "fee_base_msat"
224220
A..= routeFeeBaseMsat x
225221
<> "fee_proportional_millionths"
@@ -284,7 +280,7 @@ data Bolt11HrpAmt = Bolt11HrpAmt
284280
}
285281
deriving stock (Eq, Ord, Show, Data, Generic)
286282

287-
inspectBolt11HrpAmt :: (IsString a, Semigroup a) => Bolt11HrpAmt -> a
283+
inspectBolt11HrpAmt :: forall a. (Textual a) => Bolt11HrpAmt -> a
288284
inspectBolt11HrpAmt (Bolt11HrpAmt amt mul) =
289285
if sat > 1_000_000
290286
then inspectBolt11HrpAmt' btc <> " BTC"
@@ -302,9 +298,9 @@ inspectBolt11HrpAmt (Bolt11HrpAmt amt mul) =
302298

303299
inspectBolt11HrpAmt' ::
304300
forall a b.
305-
( IsString a,
306-
From b Integer,
307-
Integral b
301+
( Textual a,
302+
Integral b,
303+
From b Integer
308304
) =>
309305
Ratio b ->
310306
a
@@ -390,9 +386,9 @@ w5hex :: [Word5] -> Either String Hex
390386
w5hex =
391387
second Hex . w5bs
392388

393-
w5txt :: [Word5] -> Either String Text
389+
w5txt :: [Word5] -> Either String Unicode
394390
w5txt =
395-
first displayException . decodeUtf8' <=< w5bs
391+
first displayException . decodeUtf8Strict <=< w5bs
396392

397393
w5int :: [Word5] -> Int
398394
w5int bytes = foldl' decodeInt 0 (zip [0 ..] (take 7 (reverse bytes)))
@@ -430,13 +426,6 @@ w5addr net (v0 : rest) =
430426
where
431427
cfg = networkSettings net
432428

433-
inspectW5 :: forall a. (From Text a) => [Word5] -> a
434-
inspectW5 ws =
435-
from @Text @a
436-
. either (const . inspect $ fmap fromEnum ws) id
437-
$ w5txt ws
438-
<|> fmap inspect (w5bs ws)
439-
440429
parseTag :: Network -> [Word5] -> (Maybe Tag, [Word5])
441430
parseTag _ [] = (Nothing, [])
442431
parseTag _ ws@[_] = (Nothing, ws)

pub/functora/src/functora-ghcjs.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ common pkg
133133
, with-utf8
134134

135135
if (impl(ghcjs) || arch(javascript))
136-
build-depends: ghcjs-base
136+
build-depends:
137+
, ghcjs-base
138+
, miso
137139

138140
if os(wasi)
139141
build-depends:

pub/functora/src/money/Functora/Money.hs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ module Functora.Money
3434
where
3535

3636
import qualified Data.Aeson.Combinators.Decode as A
37-
import qualified Data.Text as T
3837
import Functora.Cfg
3938
import Functora.MoneySing as X
4039
import Functora.Prelude
4140
import Functora.Tags as X
42-
import qualified Language.Haskell.TH.Syntax as TH
4341

4442
type IntRep tags = IntRepFamily (GetTag SignedOrUnsigned tags)
4543

@@ -305,32 +303,30 @@ unJsonUnsignedMoneyGOL =
305303
newUnsignedMoneyGOL @tags <$> unJsonRational
306304

307305
newtype CurrencyCode = CurrencyCode
308-
{ unCurrencyCode :: Text
306+
{ unCurrencyCode :: Unicode
309307
}
310-
deriving stock (Eq, Ord, Show, Read, Data, Generic, TH.Lift)
308+
deriving stock (Eq, Ord, Show, Read, Data, Generic)
311309
deriving newtype (Binary, FromJSON, FromJSONKey, ToJSON, ToJSONKey)
312310

313-
inspectCurrencyCode :: forall a. (From Text a) => CurrencyCode -> a
311+
inspectCurrencyCode :: CurrencyCode -> Unicode
314312
inspectCurrencyCode =
315-
from @Text @a
316-
. T.strip
313+
strip
317314
. unCurrencyCode
318315

319316
data CurrencyInfo = CurrencyInfo
320317
{ currencyInfoCode :: CurrencyCode,
321-
currencyInfoText :: Text
318+
currencyInfoText :: Unicode
322319
}
323-
deriving stock (Eq, Ord, Show, Read, Data, Generic, TH.Lift)
320+
deriving stock (Eq, Ord, Show, Read, Data, Generic)
324321
deriving (Binary, FromJSON, ToJSON) via GenericType CurrencyInfo
325322

326-
inspectCurrencyInfo :: forall a. (From Text a) => CurrencyInfo -> a
323+
inspectCurrencyInfo :: CurrencyInfo -> Unicode
327324
inspectCurrencyInfo input =
328-
from @Text @a
329-
$ if null info
330-
then code
331-
else code <> " - " <> info
325+
if null info
326+
then code
327+
else code <> " - " <> info
332328
where
333-
info = T.strip $ currencyInfoText input
329+
info = strip $ currencyInfoText input
334330
code = inspectCurrencyCode $ currencyInfoCode input
335331

336332
--

0 commit comments

Comments
 (0)