module Biobase.Types.Names where
import Control.Applicative
import Control.DeepSeq (NFData(..))
import Data.Aeson as A
import Data.Binary as DB
import Data.Hashable
import Data.Serialize as DS
import Data.Serialize.Text
import Data.Stringable as SA
import Data.String as IS
import Data.Text.Binary
import Data.Text (Text,pack)
import Data.Vector.Unboxed.Deriving
import GHC.Generics
import Data.Interned.Text
import Data.Interned
import Biobase.Types.Names.Internal
newtype SpeciesName = SpeciesName { getSpeciesNameRep :: Int }
deriving (Eq,Generic)
derivingUnbox "SpeciesName"
[t| SpeciesName -> Int |]
[| getSpeciesNameRep |]
[| SpeciesName |]
instance Ord SpeciesName where
SpeciesName l `compare` SpeciesName r = speciesNameBimapLookupInt l `compare` speciesNameBimapLookupInt r
speciesName :: Text -> SpeciesName
speciesName = SpeciesName . speciesNameBimapAdd
instance IsString SpeciesName where
fromString = speciesName . IS.fromString
instance Show SpeciesName where
showsPrec p i r = showsPrec p (toString i) r
instance Read SpeciesName where
readsPrec p str = [ (speciesName $ IS.fromString s, y) | (s,y) <- readsPrec p str ]
instance Hashable SpeciesName
instance Stringable SpeciesName where
toString = toString . speciesNameBimapLookupInt . getSpeciesNameRep
fromString = speciesName . SA.fromString
length = SA.length . speciesNameBimapLookupInt . getSpeciesNameRep
toText = toText . speciesNameBimapLookupInt . getSpeciesNameRep
fromText = speciesName . fromText
instance NFData SpeciesName
instance Binary SpeciesName where
put = DB.put . toText
get = fromText <$> DB.get
instance Serialize SpeciesName where
put = DS.put . toText
get = fromText <$> DS.get
instance FromJSON SpeciesName where
parseJSON s = fromText <$> parseJSON s
instance ToJSON SpeciesName where
toJSON = toJSON . toText
newtype TaxonomicRank = TaxonomicRank { getTaxonomicRank :: InternedText }
deriving (IsString,Eq,Ord,Show,Generic)
instance NFData TaxonomicRank where
rnf (TaxonomicRank it) = rnf (internedTextId it)
instance Stringable TaxonomicRank where
toString = toString . unintern . getTaxonomicRank
fromString = fromText . pack
length = SA.length . toText
toText = unintern . getTaxonomicRank
fromText = TaxonomicRank . intern
instance Hashable TaxonomicRank where
hashWithSalt s (TaxonomicRank it) = hashWithSalt s (internedTextId it)
instance Read TaxonomicRank where
readsPrec p str = [ (IS.fromString s, y) | (s,y) <- readsPrec p str ]
instance Binary TaxonomicRank where
put = DB.put . toText
get = fromText <$> DB.get
instance Serialize TaxonomicRank where
put = DS.put . toText
get = fromText <$> DS.get
instance FromJSON TaxonomicRank where
parseJSON s = fromText <$> parseJSON s
instance ToJSON TaxonomicRank where
toJSON = toJSON . toText