Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add customTypeWith #101

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading