packages feed

rap (empty) → 0.1.0.0

raw patch · 7 files changed

+264/−0 lines, 7 filesdep +basedep +binarydep +bytestringbinary-added

Dependencies added: base, binary, bytestring, filepath, rap, tasty, tasty-hunit

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for rap++## 0.1.0.0 -- 11-19-2026++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2026 David Cox++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,11 @@+# rap++[Resource Interchange File Format (RIFF)](https://www.loc.gov/preservation/digital/formats/fdd/fdd000025.shtml)++Development++```+cabal build+cabal haddock+cabal run test+```
+ data/hello.wav view

binary file changed (absent → 31640 bytes)

+ lib/Data/Rap.hs view
@@ -0,0 +1,82 @@+module Data.Rap (Rap, getRap, putRap, Riff(..)) where++import Control.Applicative+import Control.Monad+import Data.Binary.Get+import Data.Binary.Put+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Lazy as BS+import qualified Data.ByteString.Lazy.Char8 as BSC+import Data.Function+import Data.Word++type Rap = [Riff]++getRap :: Get Rap+getRap = many getRiff++putRap :: Rap -> Put+putRap = foldMap putRiff++data Riff = Verse String ByteString | Stanza String String Rap+  deriving (Eq, Read, Show)++getRiff :: Get Riff+getRiff = uncurry getElem =<< getHeader++getElems :: Word32 -> Get Rap+getElems ssz = go 0+  where+    go cnt+      | cnt == ssz = return []+      | cnt >  ssz = fail "getElems: overflow"+      | otherwise  = do+        (cid, csz) <- getHeader+        (:) <$> getElem cid csz <*> go (applyWhen (odd csz) (+ 1) $ cnt + 8 + csz)++getElem :: String -> Word32 -> Get Riff+getElem cid csz+  | cid == "RIFF" || cid == "LIST" =+    Stanza cid <$> fmap BSC.unpack (getLazyByteString 4) <*> getElems (csz - 4)+  | otherwise = Verse cid <$> getLazyByteString (fromIntegral csz) <* getPad csz++getHeader :: Get (String, Word32)+getHeader = (,) <$> fmap BSC.unpack (getLazyByteString 4) <*> getWord32le++getPad :: Word32 -> Get ()+getPad csz = when (odd csz) (void getWord8)++putRiff :: Riff -> Put+putRiff r = case r of+  Verse cid bs -> putVerse cid bs+  Stanza cid cty rs -> do+    putLazyByteString $ BSC.pack cid+    let (es, esz) = putElems rs+    putWord32le $ esz + 4+    putLazyByteString $ BSC.pack cty+    es++putElems :: Rap -> (Put, Word32)+putElems = accumElems . map putElem++accumElems :: [(Put, Word32)] -> (Put, Word32)+accumElems es = let (ps, cs) = unzip es+                in (mconcat ps, sum cs)++putElem :: Riff -> (Put, Word32)+putElem r = case r of+  Verse cid bs -> let cnt = fromIntegral $ BS.length bs+                  in (putVerse cid bs, 8 + applyWhen (odd cnt) (+ 1) cnt)+  Stanza cid cty rs ->+    let (es, cnt) = putElems rs+        p = mconcat [putLazyByteString $ BSC.pack cid, putWord32le $ 4 + cnt+          , putLazyByteString $ BSC.pack cty, es]+    in (p, 8 + 4 + cnt)++putVerse :: String -> ByteString -> Put+putVerse cid bs = do+  putLazyByteString $ BSC.pack cid+  let csz = fromIntegral $ BS.length bs+  putWord32le csz+  putLazyByteString bs+  when (odd csz) $ putWord8 0x00
+ rap.cabal view
@@ -0,0 +1,48 @@+cabal-version:      3.0+name:               rap+version:            0.1.0.0+synopsis:           Resource Interchange File Format (RIFF)+description:        RIFF expression language+homepage:           https://github.com/dopamane/rap+license:            MIT+license-file:       LICENSE+author:             dopamane+maintainer:         dwc1295@gmail.com+copyright:          David Cox+category:           Data+build-type:         Simple+extra-doc-files:    CHANGELOG.md+                    README.md+data-dir:           data+data-files:         hello.wav++source-repository head+  type: git+  location: https://github.com/dopamane/rap++library+  default-language: Haskell2010+  ghc-options:      -Wall -O2+  hs-source-dirs:   lib+  exposed-modules:  Data.Rap+  build-depends:+    base < 5,+    binary,+    bytestring++test-suite test+  default-language: Haskell2010+  ghc-options:      -Wall -O2 -threaded+  hs-source-dirs:   test+  type:             exitcode-stdio-1.0+  main-is:          Main.hs+  other-modules:    Paths_rap+  autogen-modules:  Paths_rap+  build-depends:+    base < 5,+    binary,+    bytestring,+    filepath,+    rap,+    tasty,+    tasty-hunit
+ test/Main.hs view
@@ -0,0 +1,97 @@+{-# LANGUAGE OverloadedStrings #-}++module Main (main) where++import Data.Binary.Get+import Data.Binary.Put+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Lazy as BS+import qualified Data.ByteString.Lazy.Char8 as BSC+import Data.Rap+import Paths_rap+import System.FilePath+import Test.Tasty+import Test.Tasty.HUnit++main :: IO ()+main = getDataDir >>= \dataDir -> defaultMain $ testGroup "Data.Rap"+  [ getEmptyTest, getVerseTest, getStanzaTest+  , putEmptyTest, putVerseTest, putStanzaTest+  , tillICollapseTest, wavTest dataDir+  ]++getEmptyTest :: TestTree+getEmptyTest = testCase "Get empty" $ runGet getRap mempty @?= []++getVerseTest :: TestTree+getVerseTest = testCase "Get verse" $ runGet getRap l33tVerseBS @?= l33tVerse++getStanzaTest :: TestTree+getStanzaTest = testCase "Get stanza" $+  runGet getRap sampStanzaBS @?= sampStanza++putEmptyTest :: TestTree+putEmptyTest = testCase "Put empty" $ runPut (putRap []) @?= mempty++putVerseTest :: TestTree+putVerseTest = testCase "Put verse" $ runPut (putRap l33tVerse) @?= l33tVerseBS++putStanzaTest :: TestTree+putStanzaTest = testCase "Put stanza" $+  BS.unpack (runPut (putRap sampStanza)) @?= BS.unpack sampStanzaBS++sampStanza :: Rap+sampStanza = [Stanza "RIFF" "riff" $ l33tVerse ++ krs1Verse]++sampStanzaBS :: ByteString+sampStanzaBS = mconcat ["RIFF"+  , runPut (putWord32le $ fromIntegral $ 4 + BS.length l33tVerseBS + BS.length krs1BS)+  , "riff" <> l33tVerseBS <> krs1BS]++l33tVerse :: Rap+l33tVerse = [Verse "1337" "hello world"]++l33tVerseBS :: ByteString+l33tVerseBS = mconcat ["1337" <> runPut (putWord32le 11)+  , BSC.pack "hello world" <> BS.singleton 0x00]++krs1Verse :: Rap+krs1Verse = [Verse "krs1" $ BSC.pack stepIntoAWorld]++krs1BS :: ByteString+krs1BS = mconcat [BSC.pack "krs1" <>+  runPut (putWord32le $ fromIntegral $ length stepIntoAWorld),+  BSC.pack stepIntoAWorld]++stepIntoAWorld :: String+stepIntoAWorld = "step into a world where theres no one left but the very best"++tillICollapse :: Rap+tillICollapse =+  [ Stanza "RIFF" "mnem"+  [ Verse "lin1" "Til I collapse, I'm spillin' these raps long as you feel em"+  , Verse "lin2" "Til the day that I drop, you'll never say that I'm not killin em"+  , Verse "lin3" "Cause when I am not, then Ima stop pennin em"+  , Verse "lin4" "And I am not hip-hop and Im just not Eminem"+  , Verse "lin5" "Subliminal throughts, when Ima stop sendin em?"+  , Verse "lin6" "Women are caught in webs, spin em and hock venom"+  , Verse "lin7" "Adrenaline shots of penicillin could not get the illin to stop"+  , Verse "lin8" "Amoxicillins just not real enough"+  , Verse "lin9" "The criminal, cop-killin, hip-hop villain"+  , Verse "li10" "A minimal swap to cop millions of Pac listeners"+  , Verse "li11" "Youre comin with me, feel it or not"+  , Verse "li12" "You're gonna fear it like I showed ya the spirit of God lives in us"+  , Verse "li13" "You hear it a lot, lyrics to shock"+  , Verse "li14" "Is it a miracle or am I just product of pop fizzin' up?"+  , Verse "li15" "Fa' shizzle, my wizzle, this is the plot, listen up"+  , Verse "li16" "You bizzles forgot, Slizzle does not give a fuck"+  ]]++tillICollapseTest :: TestTree+tillICollapseTest = testCase "TillICollapse" $+  runGet getRap (runPut $ putRap tillICollapse) @?= tillICollapse++wavTest :: FilePath -> TestTree+wavTest dataDir = testCase "wav" $ do+  bs <- BS.readFile $ dataDir </> "hello.wav"+  runPut (putRap $ runGet getRap bs) @?= bs