Skip to content

Commit e3e20c4

Browse files
author
Aleksandr Penskoi
committed
Remove plutus-config dependency. Reason:
plutus-config right now is a closed source project, so this commit remove this dependency.
1 parent 9a54e6d commit e3e20c4

File tree

8 files changed

+803
-11
lines changed

8 files changed

+803
-11
lines changed

bot-plutus-interface.cabal

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ library
9191
BotPlutusInterface.TimeSlot
9292
BotPlutusInterface.Types
9393
BotPlutusInterface.UtxoParser
94+
PlutusConfig.Base
95+
PlutusConfig.Cardano.Api
96+
PlutusConfig.Cardano.Api.Shelley
97+
PlutusConfig.Ledger
98+
PlutusConfig.Misc
99+
PlutusConfig.Types
94100

95101
build-depends:
96102
, aeson ^>=1.5.0.0
@@ -122,7 +128,6 @@ library
122128
, playground-common
123129
, plutus-chain-index
124130
, plutus-chain-index-core
125-
, plutus-config
126131
, plutus-contract
127132
, plutus-core
128133
, plutus-ledger
@@ -131,17 +136,21 @@ library
131136
, plutus-pab
132137
, plutus-tx
133138
, plutus-tx-plugin
139+
, pretty
134140
, prettyprinter
135141
, process
136142
, QuickCheck
143+
, regex-compat
137144
, row-types
138145
, serialise
139146
, servant
140147
, servant-client
148+
, servant-client-core
141149
, servant-server
142150
, servant-websockets
143151
, split
144152
, stm
153+
, temporary
145154
, text ^>=1.2.4.0
146155
, time
147156
, tostring
@@ -197,7 +206,6 @@ test-suite bot-plutus-interface-test
197206
, playground-common
198207
, plutus-chain-index
199208
, plutus-chain-index-core
200-
, plutus-config
201209
, plutus-contract
202210
, plutus-core
203211
, plutus-ledger

flake.nix

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@
109109
"github:input-output-hk/Win32-network/3825d3abf75f83f406c1f7161883c438dac7277d";
110110
flake = false;
111111
};
112-
plutus-config = {
113-
url = "git+ssh://git@github.com/mlabs-haskell/plutus-config.git?rev=599266c16d5575e640721c542b256902b2d11aae";
114-
flake = false;
115-
};
116112
};
117113

118114
outputs = { self, nixpkgs, haskell-nix, iohk-nix, ... }@inputs:
@@ -318,10 +314,6 @@
318314
src = inputs.Win32-network;
319315
subdirs = [ "." ];
320316
}
321-
{
322-
src = inputs.plutus-config;
323-
subdirs = [ "." ];
324-
}
325317
];
326318

327319
projectFor = system:
@@ -337,7 +329,6 @@
337329
shell = {
338330
additional = ps: [
339331
ps.plutus-pab
340-
ps.plutus-config
341332
];
342333
withHoogle = true;
343334
tools.haskell-language-server = { };

src/PlutusConfig/Base.hs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{-# OPTIONS -fno-warn-orphans #-}
2+
3+
module PlutusConfig.Base (
4+
-- *Serialization
5+
maybeSpec,
6+
caseAgnosticAtomSpec,
7+
customRationalSpec,
8+
portSpec,
9+
pathSpec,
10+
filepathSpec,
11+
textSpecViaJSON,
12+
13+
-- *Deserialization
14+
toValueTextViaJSON,
15+
enumToAtom,
16+
) where
17+
18+
import Config (
19+
Atom (MkAtom),
20+
Value (Atom, Number, Text),
21+
integerToNumber,
22+
)
23+
import Config.Schema (
24+
HasSpec (anySpec),
25+
ValueSpec,
26+
anyAtomSpec,
27+
customSpec,
28+
naturalSpec,
29+
stringSpec,
30+
textSpec,
31+
(<!>),
32+
)
33+
import Data.Aeson (FromJSON, ToJSON)
34+
import Data.Aeson qualified as JSON
35+
import Data.Bifunctor (first)
36+
import Data.Ratio ((%))
37+
import Data.String (fromString)
38+
import Data.String.ToString (toString)
39+
import Data.Text (Text)
40+
import Data.Text qualified as Text
41+
import Network.Wai.Handler.Warp (Port)
42+
import Numeric.Natural (Natural)
43+
import PlutusConfig.Types (ToValue (toValue), withNamePrefixSpec)
44+
import Servant.Client.Core (BaseUrl (..), parseBaseUrl, showBaseUrl)
45+
import Text.Regex (matchRegex, mkRegex)
46+
import Prelude
47+
48+
instance ToValue Bool where
49+
toValue = Atom () . MkAtom . Text.toLower . Text.pack . show
50+
51+
instance ToValue Natural where
52+
toValue x = Number () $ integerToNumber $ toInteger x
53+
54+
instance ToValue Integer where
55+
toValue x = Number () $ integerToNumber x
56+
57+
instance ToValue Int where
58+
toValue = toValue . toInteger
59+
60+
instance ToValue Text where
61+
toValue = Text ()
62+
63+
instance ToValue String where
64+
toValue = Text () . Text.pack
65+
66+
instance {-# OVERLAPS #-} HasSpec String where
67+
anySpec = stringSpec
68+
69+
instance ToValue a => ToValue (Maybe a) where
70+
toValue = maybe (Atom () "nothing") toValue
71+
72+
enumToAtom :: forall a. Show a => a -> Value ()
73+
enumToAtom = Atom () . MkAtom . Text.toLower . Text.pack . show
74+
75+
maybeSpec :: forall a. ValueSpec a -> ValueSpec (Maybe a)
76+
maybeSpec spec =
77+
Nothing <$ caseAgnosticAtomSpec "nothing"
78+
<!> Just <$> spec
79+
80+
instance ToValue Rational where
81+
toValue x = Text () $ Text.pack $ show x
82+
83+
customRationalSpec :: ValueSpec Rational
84+
customRationalSpec =
85+
customSpec
86+
"Ratio number (\"1 % 2\") in"
87+
stringSpec
88+
( \x -> case matchRegex ratioRE x of
89+
Just [n, d] ->
90+
let n' = read n
91+
d' = read d
92+
in if d' == 0
93+
then Left "denominator should not be zero"
94+
else Right $ n' % d'
95+
_ -> Left $ Text.pack "Ratio format: '1 % 2'"
96+
)
97+
where
98+
ratioRE = mkRegex "^ *([0-9]+) *% *([0-9]+) *$"
99+
100+
pathSpec :: ValueSpec Text
101+
pathSpec = withNamePrefixSpec "path" anySpec
102+
103+
filepathSpec :: ValueSpec Text
104+
filepathSpec = withNamePrefixSpec "filepath" anySpec
105+
106+
toValueTextViaJSON :: forall a. ToJSON a => a -> Value ()
107+
toValueTextViaJSON = Text () . Text.pack . filter (/= '"') . toString . JSON.encode
108+
109+
textSpecViaJSON :: forall a. FromJSON a => Text -> ValueSpec a
110+
textSpecViaJSON name =
111+
customSpec
112+
name
113+
textSpec
114+
( \s -> case JSON.eitherDecode $ fromString $ wrap $ toString s of
115+
Left err -> Left $ "parse error: " <> fromString err
116+
Right res -> Right res
117+
)
118+
where
119+
wrap s = "\"" <> s <> "\""
120+
121+
instance ToValue BaseUrl where
122+
toValue = Text () . Text.pack . showBaseUrl
123+
124+
instance HasSpec BaseUrl where
125+
anySpec = baseUrlSpec
126+
127+
baseUrlSpec :: ValueSpec BaseUrl
128+
baseUrlSpec =
129+
customSpec
130+
"url"
131+
anySpec
132+
(first (Text.pack . show) . parseBaseUrl . Text.unpack)
133+
134+
portSpec :: ValueSpec Port
135+
portSpec = fromEnum <$> customSpec "port" naturalSpec Right
136+
137+
{- |Primitive specification for matching a particular atom in case way. E.g.:
138+
@caseAgnosticAtomSpec "Yes"@ will catch @yes@ and @YEs@.
139+
-}
140+
caseAgnosticAtomSpec :: Text -> ValueSpec ()
141+
caseAgnosticAtomSpec tag = customSpec "case insensitive atom" anyAtomSpec $
142+
\t ->
143+
if Text.toLower t == Text.toLower tag
144+
then Right ()
145+
else Left $ "should be " <> tag <> " (case insensitive), but actually: " <> t

0 commit comments

Comments
 (0)