|
| 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