summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchessai <>2018-06-13 16:08:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-06-13 16:08:00 (GMT)
commit3fc79ad33aacb57e8dd8e93f64fa8dd1687cc534 (patch)
tree541999a846870cc56d6e3e50cb2702212533fd46
version 0.1.0.0HEAD0.1.0.0master
-rw-r--r--Asn.hs53
-rw-r--r--ChangeLog.md5
-rw-r--r--LICENSE30
-rw-r--r--Setup.hs2
-rw-r--r--asn.cabal25
5 files changed, 115 insertions, 0 deletions
diff --git a/Asn.hs b/Asn.hs
new file mode 100644
index 0000000..88f35d5
--- /dev/null
+++ b/Asn.hs
@@ -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
diff --git a/ChangeLog.md b/ChangeLog.md
new file mode 100644
index 0000000..0f621c1
--- /dev/null
+++ b/ChangeLog.md
@@ -0,0 +1,5 @@
+# Revision history for asn
+
+## 0.1.0.0 -- YYYY-mm-dd
+
+* First version. Released on an unsuspecting world.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..20309fa
--- /dev/null
+++ b/LICENSE
@@ -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.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/asn.cabal b/asn.cabal
new file mode 100644
index 0000000..845373d
--- /dev/null
+++ b/asn.cabal
@@ -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