From 3639b049ad6f6f13efdced797e7cf3450fb6ac3b Mon Sep 17 00:00:00 2001 From: Danila Danko Date: Mon, 24 Jul 2023 01:00:00 +0300 Subject: [PATCH] upd for 0.15.10 --- .gitignore | 1 + README.md | 32 +++++++++---- packages.dhall | 105 +++++++++++++++++++++++++++++++++++++++++ spago.dhall | 23 +++++++++ src/Record/Format.purs | 15 +++--- test/Main.purs | 3 +- 6 files changed, 162 insertions(+), 17 deletions(-) create mode 100644 packages.dhall create mode 100644 spago.dhall diff --git a/.gitignore b/.gitignore index 9623fa5..db0e3de 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /.psc* /.purs* /.psa* +.spago \ No newline at end of file diff --git a/README.md b/README.md index 9600d1a..a4d5a0f 100644 --- a/README.md +++ b/README.md @@ -2,31 +2,27 @@ Record formatting from type-level format strings, based on [Justin Woo](https://github.com/justinwoo)'s idea. -This library uses the 0.12 version of the compiler. +This library uses the 0.15.10 version of the compiler. ## Example ```purescript -format - (SProxy :: SProxy "Hi {name}! Your favourite number is {number}") - {name : "Bill", number : 16} +format @"Hi {name}! Your favourite number is {number}" {name : "Bill", number : 16} ``` produces the string -``` +```console "Hi Bill! Your favourite number is 16" ``` A missing field results in a type-error: ```purescript -format - (SProxy "Hi {name}! Your favourite number is {number}") - {name : "Bill"} +format @"Hi {name}! Your favourite number is {number}" {name : "Bill"} ``` -``` +```console Could not match type ( number :: t2 @@ -41,3 +37,21 @@ format The only requirement is that all the types in the record have `Show` instances. + +## Nix flake + +1. Install Nix. + - Use [single-user installation](https://nixos.org/download.html), then [enable flakes](https://nixos.wiki/wiki/Flakes). + - Alternatively, use an [unofficial](https://github.com/DeterminateSystems/nix-installer#the-determinate-nix-installer) installer. + +2. Run the default `devShell`. + + ```console + nix develop + ``` + +3. Test the project + + ```console + spago test + ``` diff --git a/packages.dhall b/packages.dhall new file mode 100644 index 0000000..bd7cbfc --- /dev/null +++ b/packages.dhall @@ -0,0 +1,105 @@ +{- +Welcome to your new Dhall package-set! + +Below are instructions for how to edit this file for most use +cases, so that you don't need to know Dhall to use it. + +## Use Cases + +Most will want to do one or both of these options: +1. Override/Patch a package's dependency +2. Add a package not already in the default package set + +This file will continue to work whether you use one or both options. +Instructions for each option are explained below. + +### Overriding/Patching a package + +Purpose: +- Change a package's dependency to a newer/older release than the + default package set's release +- Use your own modified version of some dependency that may + include new API, changed API, removed API by + using your custom git repo of the library rather than + the package set's repo + +Syntax: +where `entityName` is one of the following: +- dependencies +- repo +- version +------------------------------- +let upstream = -- +in upstream + with packageName.entityName = "new value" +------------------------------- + +Example: +------------------------------- +let upstream = -- +in upstream + with halogen.version = "master" + with halogen.repo = "https://example.com/path/to/git/repo.git" + + with halogen-vdom.version = "v4.0.0" + with halogen-vdom.dependencies = [ "extra-dependency" ] # halogen-vdom.dependencies +------------------------------- + +### Additions + +Purpose: +- Add packages that aren't already included in the default package set + +Syntax: +where `` is: +- a tag (i.e. "v4.0.0") +- a branch (i.e. "master") +- commit hash (i.e. "701f3e44aafb1a6459281714858fadf2c4c2a977") +------------------------------- +let upstream = -- +in upstream + with new-package-name = + { dependencies = + [ "dependency1" + , "dependency2" + ] + , repo = + "https://example.com/path/to/git/repo.git" + , version = + "" + } +------------------------------- + +Example: +------------------------------- +let upstream = -- +in upstream + with benchotron = + { dependencies = + [ "arrays" + , "exists" + , "profunctor" + , "strings" + , "quickcheck" + , "lcg" + , "transformers" + , "foldable-traversable" + , "exceptions" + , "node-fs" + , "node-buffer" + , "node-readline" + , "datetime" + , "now" + ] + , repo = + "https://github.com/hdgarrood/purescript-benchotron.git" + , version = + "v7.0.0" + } +------------------------------- +-} +let upstream = + https://github.com/purescript/package-sets/releases/download/psc-0.15.9-20230619/packages.dhall + sha256:b785642d3425fd99933ddf3b705af06f30dc2dd87978cffa1a45798a2eb202af + +in upstream diff --git a/spago.dhall b/spago.dhall new file mode 100644 index 0000000..af542de --- /dev/null +++ b/spago.dhall @@ -0,0 +1,23 @@ +{- +Welcome to a Spago project! +You can edit this file as you like. + +Need help? See the following resources: +- Spago documentation: https://github.com/purescript/spago +- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html + +When creating a new Spago project, you can use +`spago init --no-comments` or `spago init -C` +to generate this file without the comments in this block. +-} +{ name = "record-format" +, dependencies = + [ "assert" + , "effect" + , "prelude" + , "record" + , "typelevel-prelude" + ] +, packages = ./packages.dhall +, sources = [ "src/**/*.purs", "test/**/*.purs" ] +} diff --git a/src/Record/Format.purs b/src/Record/Format.purs index 100fefe..df3c314 100644 --- a/src/Record/Format.purs +++ b/src/Record/Format.purs @@ -4,7 +4,8 @@ import Prelude (identity, (<>), class Show, show) import Prim.Row as Row import Prim.Symbol as Symbol import Record as Record -import Type.Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol) +import Type.Data.Symbol (class IsSymbol, reflectSymbol) +import Type.Prelude (class TypeEquals, Proxy(..)) -------------------------------------------------------------------------------- -- * Format strings @@ -23,14 +24,16 @@ data FProxy (fl :: FList) = FProxy -- | Format a row with a (type-level) format string. If @row@ doesn't contain -- all the necessary fields, constraint resolution fails class Format (string :: Symbol) (row :: Row Type) where - format :: SProxy string -> Record row -> String + format' :: Proxy string -> Record row -> String + format :: forall @l. TypeEquals l string => Record row -> String -- parse the format string and delegate the formatting to @FormatParsed@ instance formatParsedFormat :: ( Parse string parsed , FormatParsed parsed row ) => Format string row where - format _ = formatParsed (FProxy :: FProxy parsed) + format' _ = formatParsed (FProxy :: FProxy parsed) + format = format' (Proxy :: Proxy string) -- | Format a row with a list of format tokens. If @row@ doesn't contain -- all the necessary fields, constraint resolution fails @@ -48,7 +51,7 @@ instance formatVar :: ) => FormatParsed (FCons (Var key) ks) row where formatParsed _ row = var <> rest - where var = fmtVar (Record.get (SProxy :: SProxy key) row) + where var = fmtVar (Record.get (Proxy :: Proxy key) row) rest = formatParsed (FProxy :: FProxy ks) row instance formatLit :: @@ -57,7 +60,7 @@ instance formatLit :: ) => FormatParsed (FCons (Lit l) ks) row where formatParsed _ row = lit <> rest - where lit = reflectSymbol (SProxy :: SProxy l) + where lit = reflectSymbol (Proxy :: Proxy l) rest = formatParsed (FProxy :: FProxy ks) row -- | Formatting variables - we don't want to show the quotes around strings, so @@ -106,5 +109,5 @@ else instance dParseVar :: , Symbol.Cons h var var' ) => ParseVar h t (Var var') rest -parse :: forall i o. Parse i o => SProxy i -> FProxy o +parse :: forall i o. Parse i o => Proxy i -> FProxy o parse _ = FProxy :: FProxy o diff --git a/test/Main.purs b/test/Main.purs index c9331ea..d5a9db5 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -5,9 +5,8 @@ import Prelude import Effect (Effect) import Record.Format (format) import Test.Assert (assert) -import Type.Prelude (SProxy(..)) main :: Effect Unit main = do - let formatted = format (SProxy :: SProxy "Hi {name}! You are {number}") {name : "Bill", number : 16} + let formatted = format @"Hi {name}! You are {number}" {name : "Bill", number : 16} assert $ formatted == "Hi Bill! You are 16"