asn (empty) → 0.1.0.0
raw patch · 5 files changed
+115/−0 lines, 5 filesdep +aesondep +basedep +hashablesetup-changed
Dependencies added: aeson, base, hashable, primitive, scientific, text
Files
- Asn.hs +53/−0
- ChangeLog.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- asn.cabal +25/−0
+ Asn.hs view
@@ -0,0 +1,53 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE UnboxedTuples #-}++module Asn+ ( Asn(..)+ , encode+ , decode+ ) where++import Data.Text (Text)+import Data.Primitive.Types (Prim)+import Data.Word (Word32)+import Data.Hashable (Hashable)+import Foreign.Storable (Storable)+import qualified Data.Aeson as AE+import qualified Data.Scientific as SCI+import qualified Data.Text as T+import qualified Data.Text.Read as TR++newtype Asn = Asn { getAsn :: Word32 }+ deriving (Show,Read,Eq,Ord,Prim,Hashable,Storable)++instance AE.ToJSON Asn where+ toJSON = AE.String . encode++instance AE.FromJSON Asn where+ parseJSON v = case v of+ AE.Object _ -> fail "expected ASN (String or Number), encountered Object"+ AE.Array _ -> fail "expected ASN (String or Number), encountered Array"+ AE.String t -> case decode t of+ Nothing -> fail "ASN string was invalid"+ Just w -> return w+ AE.Number n -> case SCI.toBoundedInteger n of+ Nothing -> fail "numeric ASN was invalid"+ Just w -> return (Asn w)+ AE.Bool _ -> fail "expected ASN (String or Number), encountered Bool"+ AE.Null -> fail "expected ASN (String or Number), encountered Null"++encode :: Asn -> Text+encode (Asn w) = T.pack (show w)++decode :: Text -> Maybe Asn+decode t+ | T.take 2 t == "AS" = decodeAsnDigits (T.drop 2 t)+ | otherwise = decodeAsnDigits t++decodeAsnDigits :: Text -> Maybe Asn+decodeAsnDigits t = case TR.decimal t of+ Left _ -> Nothing+ Right (w,extra) -> if T.null extra+ then Just (Asn w)+ else Nothing
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for asn++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Andrew Martin++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
+ asn.cabal view
@@ -0,0 +1,25 @@+name: asn+version: 0.1.0.0+synopsis: asn type and encoding/decoding+description: asn type and encoding/decoding api+homepage: https://github.com/chessai/asn.git+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: chessai <chessai1996@gmail.com>+copyright: Copyright (C) 2018 Layer 3 Communications, Andrew Martin +category: Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Asn+ build-depends:+ base >=4.7 && < 5.0+ , aeson+ , hashable+ , primitive+ , scientific+ , text+ default-language: Haskell2010