yesod-ip (empty) → 0.1
raw patch · 6 files changed
+189/−0 lines, 6 filesdep +basedep +ipdep +persistentsetup-changed
Dependencies added: base, ip, persistent, text, yesod-core, yesod-form
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Database/Persist/Net/PostgreSQL.hs +25/−0
- src/Database/Persist/Net/Simple.hs +40/−0
- src/Yesod/Form/Net.hs +59/−0
- yesod-ip.cabal +33/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Andrew Martin (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Andrew Martin nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Database/Persist/Net/PostgreSQL.hs view
@@ -0,0 +1,25 @@+-- | This module provides orphan instances for data types+-- from the @ip@ package. These instances only work for+-- PostgresSQL. The following PostgreSQL column types are+-- used for each data types:+-- +-- * 'IPv4': @inet@+-- * 'Mac': @macaddr@+-- +-- This module is not yet complete.+module Database.Persist.Net.PostgreSQL + () where++import Database.Persist+import Database.Persist.Class+import Database.Persist.Sql++import Data.Text (Text)+import Data.Monoid+import Net.IPv4 (IPv4)+import Net.Mac (Mac)+import qualified Data.Text as Text+import qualified Net.IPv4.Text as IPv4Text+++
+ src/Database/Persist/Net/Simple.hs view
@@ -0,0 +1,40 @@+ {-# LANGUAGE OverloadedStrings #-}++-- | This module provides orphan instances for data types+-- from the @ip@ package. These instances will choose the+-- standard text type for the database column. If you are+-- using PostgreSQL, you may want to consider importing +-- the @Database.Persist.Net.PostgreSQL@ module instead.+module Database.Persist.Net.Simple + () where++import Database.Persist+import Database.Persist.Class+import Database.Persist.Sql++import Data.Text (Text)+import Data.Monoid+import Net.IPv4 (IPv4)+import Net.Mac (Mac)+import qualified Data.Text as Text+import qualified Net.IPv4.Text as IPv4Text+import qualified Net.Mac.Text as MacText++instance PersistField IPv4 where+ toPersistValue = toPersistValueTextShow IPv4Text.encode+ fromPersistValue = fromPersistValueTextRead IPv4Text.decode++instance PersistField Mac where+ toPersistValue = toPersistValueTextShow MacText.encode+ fromPersistValue = fromPersistValueTextRead MacText.decode++fromPersistValueTextRead :: (Text -> Maybe a) -> PersistValue -> Either Text a+fromPersistValueTextRead fromText z = do+ t <- fromPersistValueText z+ case fromText t of+ Nothing -> Left $ "Could not parse the following text:" <> Text.pack (show t)+ Just v -> Right v++toPersistValueTextShow :: (a -> Text) -> a -> PersistValue+toPersistValueTextShow f a = PersistText (f a)+
+ src/Yesod/Form/Net.hs view
@@ -0,0 +1,59 @@+ {-# LANGUAGE FlexibleContexts #-}+ {-# LANGUAGE OverloadedStrings #-}+module Yesod.Form.Net + ( ipv4Field+ , macField+ , NetFormMessage(..)+ ) where++import Yesod.Core+import Yesod.Form.Fields+import Yesod.Form.Types+import Net.IPv4 (IPv4)+import Net.Mac (Mac)+import Data.Text (Text)+import qualified Net.IPv4 as IPv4+import qualified Net.IPv4.Text as IPv4Text+import qualified Net.Mac as Mac+import qualified Net.Mac.Text as MacText++data NetFormMessage+ = MsgInvalidIPv4+ | MsgInvalidMac++englishNetFormMessage :: NetFormMessage -> Text+englishNetFormMessage x = case x of+ MsgInvalidIPv4 -> "Please enter an IPv4 address in dot decimal notation." + MsgInvalidMac -> "Please enter a valid MAC address."++ipv4Field :: ( Monad m+ , RenderMessage (HandlerSite m) NetFormMessage + , RenderMessage (HandlerSite m) FormMessage + ) => Field m IPv4+ipv4Field = mapField IPv4Text.encode from textField+ where + from t = case IPv4Text.decode t of+ Nothing -> Left (SomeMessage MsgInvalidIPv4)+ Just ipv4 -> Right ipv4++macField :: ( Monad m+ , RenderMessage (HandlerSite m) NetFormMessage + , RenderMessage (HandlerSite m) FormMessage + ) => Field m Mac+macField = mapField MacText.encode from textField+ where + from t = case MacText.decode t of+ Nothing -> Left (SomeMessage MsgInvalidMac)+ Just mac -> Right mac++mapField :: Monad m => (a -> b) -> (b -> Either (SomeMessage (HandlerSite m)) a) -> Field m b -> Field m a+mapField fwd bck (Field parse view enctype) = Field+ (\ts fis -> do+ eres <- parse ts fis + return $ eres >>= (\mb -> case mb of+ Just b -> Just <$> bck b + Nothing -> Right Nothing)+ )+ (\a b c d e -> view a b c (fmap fwd d) e)+ enctype+
+ yesod-ip.cabal view
@@ -0,0 +1,33 @@+name: yesod-ip+version: 0.1+synopsis: Code for using the ip package with yesod+description: Please see README.md+homepage: https://github.com/andrewthad/yesod-ip#readme+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2016 Andrew Martin+category: web+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules:+ Yesod.Form.Net+ Database.Persist.Net.Simple+ Database.Persist.Net.PostgreSQL+ build-depends: + base >= 4.7 && < 5+ , text+ , yesod-core+ , yesod-form+ , persistent+ , ip == 0.4+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/andrewthad/yesod-ip