Skip to content

Commit

Permalink
chore: add text codec functions for IPv4/IPv6 types
Browse files Browse the repository at this point in the history
  • Loading branch information
vst committed Apr 30, 2024
1 parent 20403a6 commit e11e1a1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Zamazingo/Net.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Zamazingo.Net where

import qualified Autodocodec as ADC
import qualified Data.Aeson as Aeson
import qualified Data.Text as T
import qualified Net.IPv4
import qualified Net.IPv6

Expand All @@ -24,6 +25,16 @@ instance ADC.HasCodec IPv4 where
_codec = ADC.dimapCodec MkIPv4 _unIPv4 (ADC.codecViaAeson "_IPv4")


ipv4FromText :: T.Text -> Maybe IPv4
ipv4FromText =
fmap MkIPv4 . Net.IPv4.decodeString . T.unpack


ipv4ToText :: IPv4 -> T.Text
ipv4ToText =
T.pack . Net.IPv4.encodeString . _unIPv4


newtype IPv6 = MkIPv6
{ _unIPv6 :: Net.IPv6.IPv6
}
Expand All @@ -37,3 +48,13 @@ instance ADC.HasCodec IPv6 where
_type = "IPv6"
_docs = "An IPv6 address"
_codec = ADC.dimapCodec MkIPv6 _unIPv6 (ADC.codecViaAeson "_IPv6")


ipv6FromText :: T.Text -> Maybe IPv6
ipv6FromText =
fmap MkIPv6 . Net.IPv6.decode


ipv6ToText :: IPv6 -> T.Text
ipv6ToText =
Net.IPv6.encode . _unIPv6

0 comments on commit e11e1a1

Please sign in to comment.