Skip to content

Commit

Permalink
Merge pull request #101 from miniBill/arg-custom-type-with
Browse files Browse the repository at this point in the history
Add customTypeWith
  • Loading branch information
mdgriffith authored Nov 30, 2024
2 parents a1b4451 + bf01e81 commit 939804a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/Elm/Arg.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Elm.Arg exposing
, aliasAs
, ignore, string, char, int
, list, item, items, listRemaining
, customType
, customType, customTypeWith
)

{-| An `Arg` can be used to pattern match on the arguments of a function.
Expand Down Expand Up @@ -64,7 +64,7 @@ Will generate
@docs list, item, items, listRemaining
@docs customType
@docs customType, customTypeWith
-}

Expand Down Expand Up @@ -283,3 +283,16 @@ Which will generate
customType : String -> a -> Arg a
customType =
Internal.Arg.customType


{-| Same as `customType`, but allows to specify the module name and type name.
-}
customTypeWith :
{ importFrom : List String
, typeName : String
, variantName : String
}
-> a
-> Arg a
customTypeWith =
Internal.Arg.customTypeWith
33 changes: 27 additions & 6 deletions src/Internal/Arg.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Internal.Arg exposing
, customType
, item, items, list, listRemaining, record, field
, ignore, unit
, customTypeWith
)

{-|
Expand Down Expand Up @@ -531,34 +532,54 @@ list toList =

customType : String -> a -> Arg a
customType name toType =
customTypeWith
{ typeName = Format.formatValue name
, variantName = name
, importFrom = []
}
toType


customTypeWith :
{ importFrom : List String
, typeName : String
, variantName : String
}
-> a
-> Arg a
customTypeWith { typeName, variantName, importFrom } toType =
Arg
(\index ->
let
annotation =
Ok
{ type_ = Internal.Types.custom [] (Format.formatValue name) []
{ type_ = Internal.Types.custom importFrom (Format.formatType typeName) []
, inferences = Dict.empty
, aliases = Compiler.emptyAliases
}

imports : List (List String)
imports =
[]
if List.isEmpty importFrom then
[]

else
[ importFrom ]
in
{ details =
{ imports = imports
, pattern =
Compiler.nodify
(Pattern.NamedPattern
{ moduleName = []
, name = Format.formatType name
{ moduleName = importFrom
, name = Format.formatType variantName
}
[]
)
, annotation = annotation
}
, index = Index.dive index
, value =
toType
, value = toType
}
)

Expand Down
27 changes: 27 additions & 0 deletions tests/Pattern.elm
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,33 @@ suite =
Nothing ->
"Oh, it's nothing."
"""
, test "custom type helpers - with imports" <|
\() ->
Elm.Expect.renderedAs
(Elm.fn
(Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity
|> Arg.item (Arg.char 'c')
)
(\_ -> Elm.string "There is 1 item")
)
"""
\\(From.Here.Variant 'c') -> "There is 1 item"
"""
, test "custom type helpers - type qualification" <|
\() ->
Elm.Expect.declarationAs
(Elm.declaration "fn" <|
Elm.fn
(Arg.customTypeWith { importFrom = [ "From", "Here" ], variantName = "Variant", typeName = "SomeType" } identity
|> Arg.item (Arg.char 'c')
)
(\_ -> Elm.string "There is 1 item")
)
"""
fn : From.Here.SomeType -> String
fn (From.Here.Variant 'c') =
"There is 1 item"
"""
, describe "literal patterns"
[ test "unit" <|
\() ->
Expand Down

0 comments on commit 939804a

Please sign in to comment.