Skip to content

Instantly share code, notes, and snippets.

@Orasund
Created May 29, 2022 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orasund/30a900d84af443e9e23f56a35a5b1255 to your computer and use it in GitHub Desktop.
Save Orasund/30a900d84af443e9e23f56a35a5b1255 to your computer and use it in GitHub Desktop.
Generated Files from Elm-pen 0.0.6
module Gen.Record.Model exposing (..)
{-| Module generated by [Elm-Pen](https://orasund.github.io/elm-pen).
This module contains the Model Record.
type alias Model =
{ weekday : Weekday
, todos : Array String
}
# Type
@docs Model
# Getter
@docs getWeekday, getTodos
# Setter
@docs setWeekday, setTodos
# Mapper
@docs mapWeekday, mapTodos
# Serialization
@docs encoder
@docs decoder
-}
import Array exposing (Array)
import Gen.Enum.Weekday as Weekday exposing (Weekday)
import Json.Decode as D
import Json.Encode as E
-- This is a generated file. DO NOT CHANGE ANYTHING IN HERE.
-------------------------------------------------------------------------------
-- TYPE
-------------------------------------------------------------------------------
{-| Model record
-}
type alias Model =
{ weekday : Weekday
, todos : Array String
}
-------------------------------------------------------------------------------
-- GETTER
-------------------------------------------------------------------------------
{-| Get the value of the weekday field.
getWeekday : Model -> Weekday
getWeekday =
.weekday
-}
getWeekday : Model -> Weekday
getWeekday =
.weekday
{-| Get the value of the todos field.
getTodos : Model -> Array String
getTodos =
.todos
-}
getTodos : Model -> Array String
getTodos =
.todos
-------------------------------------------------------------------------------
-- SETTER
-------------------------------------------------------------------------------
{-| Set the value of the weekday field.
setWeekday : Weekday -> Model -> Model
setWeekday weekday model =
{ model | weekday = weekday }
-}
setWeekday : Weekday -> Model -> Model
setWeekday weekday model =
{ model | weekday = weekday }
{-| Set the value of the todos field.
setTodos : Array String -> Model -> Model
setTodos todos model =
{ model | todos = todos }
-}
setTodos : Array String -> Model -> Model
setTodos todos model =
{ model | todos = todos }
-------------------------------------------------------------------------------
-- MAPPER
-------------------------------------------------------------------------------
{-| Map the value of the weekday field.
mapWeekday : (Weekday -> Weekday) -> Model -> Model
mapWeekday fun model =
{ model | weekday = fun model.weekday }
-}
mapWeekday : (Weekday -> Weekday) -> Model -> Model
mapWeekday fun model =
{ model | weekday = fun model.weekday }
{-| Map the value of the todos field.
mapTodos : (Array String -> Array String) -> Model -> Model
mapTodos fun model =
{ model | todos = fun model.todos }
-}
mapTodos : (Array String -> Array String) -> Model -> Model
mapTodos fun model =
{ model | todos = fun model.todos }
-------------------------------------------------------------------------------
-- SERIALIZATION
-------------------------------------------------------------------------------
{-| Json encoder for Model
-}
encoder : Model -> E.Value
encoder model =
E.object
[ ( "weekday", Weekday.encoder model.weekday )
, ( "todos", E.array E.string model.todos )
]
{-| Json decoder for Model
-}
decoder : D.Decoder Model
decoder =
D.succeed
(\weekday todos ->
{ weekday = weekday
, todos = todos
}
)
|> D.andThen (\fun -> D.map fun Weekday.decoder)
|> D.andThen (\fun -> D.map fun (D.array D.string))
-- Generated with [Elm-Pen](https://orasund.github.io/elm-pen) Version 0.0.6
module Gen.Union.RemoteData exposing (..)
{-| This module contains the RemoteData Union.
@docs RemoteData
## Is
@docs isWaiting, isResult, isFailure, isSuccess
## If
@docs ifResult, ifFailure, ifSuccess
## Map
@docs mapResult, mapFailure, mapSuccess
-}
-- This is a generated file. DO NOT CHANGE ANYTHING IN HERE.
{-| RemoteData type
-}
type RemoteData a1 a3
= Waiting
| Result (Result String a1)
| Failure String
| Success a3
-------------------------------------------------------------------------------
-- IS
-------------------------------------------------------------------------------
{-| Compute if the value is a Waiting.
Useful if you don't want to do a full case distinction inside an if condition.
-}
isWaiting : RemoteData a1 a3 -> Bool
isWaiting remoteData =
case remoteData of
Waiting ->
True
Result _ ->
False
Failure _ ->
False
Success _ ->
False
{-| Compute if the value is a Result.
Useful if you don't want to do a full case distinction inside an if condition.
-}
isResult : RemoteData a1 a3 -> Bool
isResult remoteData =
case remoteData of
Waiting ->
False
Result _ ->
True
Failure _ ->
False
Success _ ->
False
{-| Compute if the value is a Failure.
Useful if you don't want to do a full case distinction inside an if condition.
-}
isFailure : RemoteData a1 a3 -> Bool
isFailure remoteData =
case remoteData of
Waiting ->
False
Result _ ->
False
Failure _ ->
True
Success _ ->
False
{-| Compute if the value is a Success.
Useful if you don't want to do a full case distinction inside an if condition.
-}
isSuccess : RemoteData a1 a3 -> Bool
isSuccess remoteData =
case remoteData of
Waiting ->
False
Result _ ->
False
Failure _ ->
False
Success _ ->
True
-------------------------------------------------------------------------------
-- IF
-------------------------------------------------------------------------------
{-| Calls a function, if the value is a Result.
You can turn the function into a getter by passing the identity as function
-}
ifResult : (Result String a1 -> out) -> RemoteData a1 a3 -> Maybe out
ifResult fun remoteData =
case remoteData of
Waiting ->
Nothing
Result t ->
Just (fun t)
Failure a ->
Nothing
Success a ->
Nothing
{-| Calls a function, if the value is a Failure.
You can turn the function into a getter by passing the identity as function
-}
ifFailure : (String -> out) -> RemoteData a1 a3 -> Maybe out
ifFailure fun remoteData =
case remoteData of
Waiting ->
Nothing
Result a ->
Nothing
Failure t ->
Just (fun t)
Success a ->
Nothing
{-| Calls a function, if the value is a Success.
You can turn the function into a getter by passing the identity as function
-}
ifSuccess : (a3 -> out) -> RemoteData a1 a3 -> Maybe out
ifSuccess fun remoteData =
case remoteData of
Waiting ->
Nothing
Result a ->
Nothing
Failure a ->
Nothing
Success t ->
Just (fun t)
-------------------------------------------------------------------------------
-- MAP
-------------------------------------------------------------------------------
{-| Map the argument of Result.
-}
mapResult : (Result String a1 -> Result String b1) -> RemoteData a1 a3 -> RemoteData b1 a3
mapResult fun remoteData =
case remoteData of
Waiting ->
Waiting
Result t ->
Result (fun t)
Failure a ->
Failure a
Success a ->
Success a
{-| Map the argument of Failure.
-}
mapFailure : (String -> String) -> RemoteData a1 a3 -> RemoteData a1 a3
mapFailure fun remoteData =
case remoteData of
Waiting ->
Waiting
Result a ->
Result a
Failure t ->
Failure (fun t)
Success a ->
Success a
{-| Map the argument of Success.
-}
mapSuccess : (a3 -> b3) -> RemoteData a1 a3 -> RemoteData a1 b3
mapSuccess fun remoteData =
case remoteData of
Waiting ->
Waiting
Result a ->
Result a
Failure a ->
Failure a
Success t ->
Success (fun t)
-- Generated with [Elm-pen](https://orasund.github.io/elm-pen) Version 0.0.6
module Gen.Enum.Weekday exposing (..)
{-| Module generated by [Elm-Pen](https://orasund.github.io/elm-pen).
This module contains the Weekday Enum.
type Weekday
= Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
| Sunday
# Basics
@docs Weekday, asList, first, next, prev, last
# Converters
@docs toInt, fromInt, toString, fromString
# Json Serialization
@docs encode, decoder, decoderWithError
-}
import Array exposing (Array)
import Json.Decode
import Json.Encode
-- This is a generated file. DO NOT CHANGE ANYTHING IN HERE.
-------------------------------------------------------------------------------
-- BASICS
-------------------------------------------------------------------------------
{-| Weekday type.
-}
type Weekday
= Monday
| Tuesday
| Wednesday
| Thursday
| Friday
| Saturday
| Sunday
{-| Lists all possible values of Weekday
asList : List Weekday
asList =
[ Monday
, Tuesday
, Wednesday
, Thursday
, Friday
, Saturday
, Sunday
]
-}
asList : List Weekday
asList =
[ Monday
, Tuesday
, Wednesday
, Thursday
, Friday
, Saturday
, Sunday
]
{-| Get the first constructor of the Weekday
first : Weekday
first =
Monday
-}
first : Weekday
first =
Monday
{-| Get the next Weekday.
-}
next : Weekday -> Weekday
next weekday =
weekday
|> toInt
|> (+) 1
|> fromInt
|> Maybe.withDefault Monday
{-| Get the next Weekday.
-}
prev : Weekday -> Weekday
prev weekday =
weekday
|> toInt
|> (+) -1
|> fromInt
|> Maybe.withDefault Sunday
{-| Get the last constructor of the Weekday
last : Weekday
last =
Sunday
-}
last : Weekday
last =
Sunday
-------------------------------------------------------------------------------
-- CONVERTERS
-------------------------------------------------------------------------------
{-| Convert `Weekday` into `Int`.
toInt : Weekday -> Int
toInt arg =
case arg of
Monday ->
0
Tuesday ->
1
Wednesday ->
2
Thursday ->
3
Friday ->
4
Saturday ->
5
Sunday ->
6
If you need to convert all values of Weekday into ints, use `asList` instead.
asList |> List.indexedMap (\i _ -> i)
--> asList |> List.map toInt
-}
toInt : Weekday -> Int
toInt weekday =
case weekday of
Monday ->
0
Tuesday ->
1
Wednesday ->
2
Thursday ->
3
Friday ->
4
Saturday ->
5
Sunday ->
6
{-| Convert `Int` into `Weekday`
Returns `Nothing` if the values is out of bounds.
fromInt : Int -> Maybe Weekday
fromInt int =
case int of
0 ->
Just Monday
1 ->
Just Tuesday
2 ->
Just Wednesday
3 ->
Just Thursday
4 ->
Just Friday
5 ->
Just Saturday
6 ->
Just Sunday
_ ->
Nothing
-}
fromInt : Int -> Maybe Weekday
fromInt int =
case int of
0 ->
Just Monday
1 ->
Just Tuesday
2 ->
Just Wednesday
3 ->
Just Thursday
4 ->
Just Friday
5 ->
Just Saturday
6 ->
Just Sunday
_ ->
Nothing
{-| Convert Weekday into String
toString : Weekday -> String
toString arg =
case arg of
Monday ->
"Monday"
Tuesday ->
"Tuesday"
Wednesday ->
"Wednesday"
Thursday ->
"Thursday"
Friday ->
"Friday"
Saturday ->
"Saturday"
Sunday ->
"Sunday"
-}
toString : Weekday -> String
toString weekday =
case weekday of
Monday ->
"Monday"
Tuesday ->
"Tuesday"
Wednesday ->
"Wednesday"
Thursday ->
"Thursday"
Friday ->
"Friday"
Saturday ->
"Saturday"
Sunday ->
"Sunday"
{-| Convert a String into a Weekday
Returns Nothing if the string is not valid.
fromString : String -> Maybe Weekday
fromString arg =
case arg of
"Monday" ->
Just Monday
"Tuesday" ->
Just Tuesday
"Wednesday" ->
Just Wednesday
"Thursday" ->
Just Thursday
"Friday" ->
Just Friday
"Saturday" ->
Just Saturday
"Sunday" ->
Just Sunday
_ ->
Nothing
-}
fromString : String -> Maybe Weekday
fromString string =
case string of
"Monday" ->
Just Monday
"Tuesday" ->
Just Tuesday
"Wednesday" ->
Just Wednesday
"Thursday" ->
Just Thursday
"Friday" ->
Just Friday
"Saturday" ->
Just Saturday
"Sunday" ->
Just Sunday
_ ->
Nothing
-------------------------------------------------------------------------------
-- JSON SERIALIZATION
-------------------------------------------------------------------------------
{-| Encodes the Weekday into a json value.
encoder : Weekday -> Json.Encode.Value
encoder arg =
arg
|> toString
|> Json.Encode.string
-}
encoder : Weekday -> Json.Encode.Value
encoder weekday =
weekday
|> toString
|> Json.Encode.string
{-| Decoder for decoding a json value into a Weekday
decoder : Json.Decode.Decoder Weekday
decoder =
"Weekday expected. Valid values are \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\" and \"Sunday\""
|> decoderWithError
-}
decoder : Json.Decode.Decoder Weekday
decoder =
"Weekday expected. Valid values are \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\" and \"Sunday\""
|> decoderWithError
{-| Decodes a json value into a Weekday.
Takes an error message as an argument.
-}
decoderWithError : String -> Json.Decode.Decoder Weekday
decoderWithError errorMessage =
Json.Decode.string
|> Json.Decode.andThen
(\string ->
case fromString string of
Just weekday ->
Json.Decode.succeed weekday
Nothing ->
Json.Decode.fail errorMessage
)
-- Generated with [Elm-Pen](https://orasund.github.io/elm-pen) Version 0.0.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment