/
Sync.elm
68 lines (54 loc) · 1.82 KB
/
Sync.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
module PouchDB.Sync exposing (Sync, decoder, encode, isValid, sorter, toUrl)
import Json.Decode as Decode exposing (Decoder)
import Json.Decode.Pipeline as Pipeline exposing (required)
import Json.Encode as Encode
import Sort exposing (Sorter)
import Url
type alias Sync =
{ host : String
, database : String
, username : String
, password : String
}
isValid : Sync -> Bool
isValid { host, database, username, password } =
(Url.fromString host /= Nothing)
&& (database /= "")
&& (username /= "")
&& (password /= "")
sorter : Sorter Sync
sorter =
Sort.by .host Sort.alphabetical
|> Sort.tiebreaker (Sort.by .database Sort.alphabetical)
|> Sort.tiebreaker (Sort.by .username Sort.alphabetical)
|> Sort.tiebreaker (Sort.by .password Sort.alphabetical)
decoder : Decoder Sync
decoder =
Decode.succeed Sync
|> required "host" Decode.string
|> required "database" Decode.string
|> required "username" Decode.string
|> required "password" Decode.string
encode : Sync -> Encode.Value
encode sync =
Encode.object
[ ( "host", Encode.string sync.host )
, ( "database", Encode.string sync.database )
, ( "username", Encode.string sync.username )
, ( "password", Encode.string sync.password )
]
toUrl : Sync -> Maybe String
toUrl { host, database, username, password } =
Url.fromString host
|> Maybe.map
(\url ->
{ url
| host =
if username /= "" || password /= "" then
username ++ ":" ++ password ++ "@" ++ url.host
else
url.host
, path = "/" ++ database
}
)
|> Maybe.map Url.toString