xch (empty) → 0.1.0.0
raw patch · 7 files changed
+539/−0 lines, 7 filesdep +basedep +hedgehogdep +megaparsec
Dependencies added: base, hedgehog, megaparsec, optparse-applicative, tasty, tasty-hedgehog, tasty-hunit, time, xch
Files
- CHANGELOG.md +5/−0
- LICENSE +21/−0
- README.md +26/−0
- exe/Main.hs +79/−0
- lib/Data/Xch.hs +210/−0
- test/Main.hs +141/−0
- xch.cabal +57/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for xch++## 0.1.0.0 -- 11-13-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,26 @@+# Xch+Standard Guidance for Space Data Exchange++[ASTM F3514-21](https://store.astm.org/catalog/product/view/id/1126697/)++Usage++```+xch --help+xch w --excellent | xch r+```++Install++```+cabal install xch+```++Development++```+cabal build+cabal haddock+cabal test+cabal run xch -- --help+```
+ exe/Main.hs view
@@ -0,0 +1,79 @@+module Main (main) where++import Data.Version+import Data.Xch+import Options.Applicative+import Paths_xch+import qualified Text.Megaparsec as M++main :: IO ()+main = do+ cli <- customExecParser prefs' $ pinfo $ showVersion version+ case cli of+ ReadXch fM -> do+ i <- maybe getContents readFile fM+ putStrLn $ either M.errorBundlePretty show $ M.parse pxch "" i+ WriteXch xch fM -> maybe putStrLn writeFile fM $ wxch xch++prefs' :: ParserPrefs+prefs' = prefs $ showHelpOnError <> showHelpOnEmpty++pinfo :: String -> ParserInfo CLI+pinfo v = info (parser <**> simpleVersioner v <**> helper) $+ progDesc "Space Data Exchange"++data CLI = ReadXch (Maybe FilePath) | WriteXch Xch (Maybe FilePath)++parser :: Parser CLI+parser = hsubparser $ mconcat+ [ command "r" $ info (ReadXch <$>+ optional (fileArg "input file or stream STDIN")) $+ progDesc "Read space data"+ , command "w" $ info (WriteXch <$> many parseXDat <*>+ optional (fileArg "output file or stream STDOUT")) $+ progDesc "Write space data"+ ]++fileArg :: String -> Parser FilePath+fileArg h = strArgument $ metavar "FILE" <> completer (bashCompleter "file") <> help h++parseXDat :: Parser XDat+parseXDat = asum+ [ parserOptionGroup "state vector data" $ SVDat <$>+ strOption (long "state" <> metavar "STR" <> help "identifier") <*>+ (many . asum)+ [ fmap Time $ option auto $ long "time" <>+ help "POSIX time e.g. $(echo \"$(date +%s.%N)s\")"+ , Pos <$> parseNED "pos", Vel <$> parseNED "vel"]+ , parserOptionGroup "mission event triggers" $ MsnEvt <$>+ strOption (long "trig" <> metavar "STR" <> help "trigger") <*>+ many (strOption $ long "evt" <> metavar "STR" <> help "event")+ , parserOptionGroup "communication health status" $ ComStat <$> asum+ [ flag' Excellent $ long "excellent" <> help "excellent connection"+ , flag' Good $ long "good" <> help "good connection"+ , flag' Bad $ long "bad" <> help "bad connection"+ , flag' Lost $ long "lost" <> help "lost connection"+ ]+ , parserOptionGroup "anomaly/emergency data" $ AEDat+ <$> strOption (long "ae" <> metavar "STR" <> help "anomaly/emergency identifier")+ <*> (many . asum)+ [ ActDurTime <$> option auto (long "act" <> metavar "INT" <> help "activate time mins")+ <*> option auto (long "dur" <> metavar "INT" <> help "duration time mins")+ , LocGeo <$> strOption (long "geo" <> metavar "STR" <> help "geographic descriptor")+ <*> many ((,) <$> parseCord "lat" <*> parseCord "lon")+ <*> optional (option auto $ long "alt" <> metavar "DBL" <> help "altitude")+ , Dfn <$> strOption (long "dfn" <> metavar "STR" <> help "definition")+ ]+ ]++parseCord :: String -> Parser Cord+parseCord pfx = Cord+ <$> option auto (long (pfx ++ "d") <> metavar "INT" <> help (pfx ++ " coordinate degrees"))+ <*> option auto (long (pfx ++ "m") <> metavar "INT" <> help (pfx ++ " coordinate minutes"))+ <*> option auto (long (pfx ++ "s") <> metavar "DBL" <> help (pfx ++ " coordinate seconds"))++parseNED :: String -> Parser NED+parseNED pfx = NED+ <$> option auto (long (pfx ++ "n") <> metavar "DBL" <> help (pfx ++ " north coordinate"))+ <*> option auto (long (pfx ++ "e") <> metavar "DBL" <> help (pfx ++ " east coordinate"))+ <*> option auto (long (pfx ++ "d") <> metavar "DBL" <> help (pfx ++ " down coordinate"))
+ lib/Data/Xch.hs view
@@ -0,0 +1,210 @@+-- | Standard Guidance for Space Data Exchange+module Data.Xch (Xch, wxch, pxch, XDat(..), SVDat(..), ComStat(..)+ , AEInfo(..), Cord(..), NED(..)) where++import Data.Functor+import Data.List+import Data.Time.Clock.POSIX+import Data.Time.Format.ISO8601+import Data.Void+import Text.Megaparsec+import Text.Megaparsec.Char+import qualified Text.Megaparsec.Char.Lexer as L++-- | exchange+type Xch = [XDat]++-- | write exchange data in XML format+wxch :: Xch -> String+wxch [] = tag_ "xch"+wxch xs = intercalate "\n" $ [bgn "xch"] ++ map wxdat xs ++ [end "xch"]++-- | parse exchange XML data+pxch :: Parser Xch+pxch = try (ptag_ "xch" $> []) <|> ptag "xch" (many pxdat)++-- | space data+data XDat = SVDat String [SVDat] | MsnEvt String [String] | ComStat ComStat+ | AEDat String [AEInfo]+ deriving (Eq, Read, Show)++wxdat :: XDat -> String+wxdat xdat = intercalate "\n" $ case xdat of+ SVDat stte sv -> [lvl 2 ++ bgn "svdat", lvl 4 ++ stag "state" stte] +++ map wsvdat sv ++ [lvl 2 ++ end "svdat"]+ MsnEvt trig es -> [lvl 2 ++ bgn "misn", lvl 4 ++ stag "trig" trig] +++ [lvl 4 ++ stag "evt" e | e <- es] ++ [lvl 2 ++ end "misn"]+ ComStat s -> [lvl 2 ++ bgn "com", wcomstat s, lvl 2 ++ end "com"]+ AEDat idnt inf -> [lvl 2 ++ bgn "aedat", lvl 4 ++ stag "ae" idnt] +++ map waeinf inf ++ [lvl 2 ++ end "aedat"]++pxdat :: Parser XDat+pxdat = choice [try $ ptag "svdat" $ SVDat <$> pstag "state" <*> many psvdat+ , try $ ptag "misn" $ MsnEvt <$> pstag "trig" <*> many (try $ pstag "evt")+ , try $ ComStat <$> ptag "com" pcomstat+ , try $ ptag "aedat" $ AEDat <$> pstag "ae" <*> many paeinf]++-- | State vector data+data SVDat+ = -- | timestamp+ Time POSIXTime+ | -- | position+ Pos NED+ | -- | velocity+ Vel NED+ deriving (Eq, Read, Show)++wsvdat :: SVDat -> String+wsvdat s = case s of+ Time t -> lvl 4 ++ stag "time" (iso8601Show $ posixSecondsToUTCTime t)+ Pos ned -> intercalate "\n" [lvl 4 ++ bgn "pos", wned ned, lvl 4 ++ end "pos"]+ Vel ned -> intercalate "\n" [lvl 4 ++ bgn "vel", wned ned, lvl 4 ++ end "vel"]++psvdat :: Parser SVDat+psvdat = choice+ [ try $ ptag "time" $ Time <$> ptime, try $ ptag "pos" $ Pos <$> pned+ , try $ ptag "vel" $ Vel <$> pned]++ptime :: Parser POSIXTime+ptime = do+ yyyy <- count 4 digitChar <* char '-'+ mm <- count 2 digitChar <* char '-'+ dd <- count 2 digitChar <* char 'T'+ hh <- count 2 digitChar <* char ':'+ mm' <- count 2 digitChar <* char ':'+ ss <- count 2 digitChar+ ssM <- optional $ char '.' *> some digitChar+ _ <- char 'Z'+ fmap utcTimeToPOSIXSeconds $ iso8601ParseM $+ concat [yyyy, "-", mm, "-", dd, "T", hh, ":", mm', ":", ss, maybe "" ("." ++) ssM, "Z"]++-- | Communication health status+data ComStat = Excellent | Good | Bad | Lost+ deriving (Bounded, Enum, Eq, Read, Show)++wcomstat :: ComStat -> String+wcomstat s = mappend " " $ tag_ $ case s of+ Excellent -> "excellent"+ Good -> "good"+ Bad -> "bad"+ Lost -> "lost"++pcomstat :: Parser ComStat+pcomstat = choice [try $ ptag_ "excellent" $> Excellent, try $ ptag_ "good" $> Good+ , try $ ptag_ "bad" $> Bad, try $ ptag_ "lost" $> Lost]++-- | Anomaly and emergency info+data AEInfo+ = -- | activation time and duration+ ActDurTime Integer Integer+ | -- | location\/geometry: name, bounds @(lat,lon)@ and altitude+ LocGeo String [(Cord, Cord)] (Maybe Double)+ | -- | definition of anomaly+ Dfn String+ deriving (Eq, Read, Show)++waeinf :: AEInfo -> String+waeinf ae = case ae of+ ActDurTime t d -> intercalate "\n" [lvl 4 ++ bgn "actdur", lvl 6 ++ tag "act" t+ , lvl 6 ++ tag "dur" d, lvl 4 ++ end "actdur"]+ LocGeo idnt cs altM -> intercalate "\n" $ concat+ [ [lvl 4 ++ bgn "loc"]+ , [lvl 6 ++ stag "geo" idnt]+ , [intercalate "\n" [ intercalate "\n"+ [ lvl 6 ++ bgn "lat"+ , wcord lat+ , lvl 6 ++ end "lat"+ , lvl 6 ++ bgn "lon"+ , wcord lon+ , lvl 6 ++ end "lon"+ ] | (lat, lon) <- cs] | not $ null cs]+ , case altM of+ Nothing -> []+ Just al -> [lvl 6 ++ tag "alt" al]+ , [lvl 4 ++ end "loc"]+ ]+ Dfn d -> lvl 4 ++ stag "dfn" d++paeinf :: Parser AEInfo+paeinf = choice+ [ try $ ptag "actdur" $ ActDurTime <$> ptag "act" pint <*> ptag "dur" pint+ , try $ ptag "loc" $ LocGeo <$> pstag "geo"+ <*> many (try $ (,) <$> ptag "lat" pcord <*> ptag "lon" pcord)+ <*> optional (try $ ptag "alt" pdbl)+ , try $ Dfn <$> pstag "dfn"+ ]++-- | coordinate: degrees, minutes, seconds+data Cord = Cord{dgs :: Integer, mns :: Integer, sds :: Double}+ deriving (Eq, Read, Show)++wcord :: Cord -> String+wcord (Cord d m s) = intercalate "\n" [lvl 8 ++ bgn "cord"+ , lvl 10 ++ tag "dgs" d, lvl 10 ++ tag "mns" m+ , lvl 10 ++ tag "sds" s, lvl 8 ++ end "cord"]++pcord :: Parser Cord+pcord = ptag "cord" $ Cord <$> ptag "dgs" pint <*> ptag "mns" pint+ <*> ptag "sds" pdbl++-- | coordinate: north, east, down+data NED = NED{north :: Double, east :: Double, down :: Double}+ deriving (Eq, Read, Show)++wned :: NED -> String+wned (NED n e d) = intercalate "\n" [lvl 6 ++ bgn "ned", lvl 8 ++ tag "north" n+ , lvl 8 ++ tag "east" e, lvl 8 ++ tag "down" d, lvl 6 ++ end "ned"]++pned :: Parser NED+pned = ptag "ned" $ NED <$> ptag "north" pdbl <*> ptag "east" pdbl+ <*> ptag "down" pdbl++lvl :: Int -> String+lvl i = replicate i ' '++bgn :: String -> String+bgn t = "<" ++ t ++ ">"++end :: String -> String+end t = "</" ++ t ++ ">"++stag :: String -> String -> String+stag t s = bgn t ++ s ++ end t++tag :: Show a => String -> a -> String+tag t = stag t . show++tag_ :: String -> String+tag_ t = "<" ++ t ++ "/>"++type Parser = Parsec Void String++sc :: Parser ()+sc = L.space space1 empty $ L.skipBlockComment "<!--" "-->"++lexe :: Parser a -> Parser a+lexe = L.lexeme sc++sym :: String -> Parser String+sym = L.symbol sc++pbgn :: String -> Parser String+pbgn = between (sym "<") (sym ">") . sym++pend :: String -> Parser String+pend = between (sym "</") (sym ">") . sym++ptag :: String -> Parser a -> Parser a+ptag t = between (pbgn t) (pend t)++ptag_ :: String -> Parser String+ptag_ = between (sym "<") (sym "/>") . sym++pdbl :: Parser Double+pdbl = try (L.signed sc $ lexe L.float) <|> L.signed sc (lexe L.decimal)++pint :: Parser Integer+pint = L.signed sc $ lexe L.decimal++pstag :: String -> Parser String+pstag t = sym "<" *> sym t *> char '>' *> manyTill anySingle (pend t)
+ test/Main.hs view
@@ -0,0 +1,141 @@+module Main (main) where++import Data.List+import Data.Word+import Data.Xch+import Hedgehog+import qualified Hedgehog.Gen as Gen+import qualified Hedgehog.Range as Range+import Text.Megaparsec+import Test.Tasty+import Test.Tasty.HUnit+import Test.Tasty.Hedgehog++main :: IO ()+main = defaultMain $ testGroup "Test.Xch"+ [testEmpty, testSVDat, testMsnEvt, testComStat, testAEDat, ptrip]++testEmpty :: TestTree+testEmpty = testCase "empty" $ do+ ptest "<xch/>" []+ ptest "<xch></xch>" []++testSVDat :: TestTree+testSVDat = testCaseSteps "SVDat" $ \step -> do+ step "state"+ ptest "<xch><svdat><state>foo</state></svdat></xch>" [SVDat "foo" []]+ ptest "<xch><svdat><state>foo bar</state></svdat></xch>" [SVDat "foo bar" []]+ ptest "<xch><svdat><state><escape</state></svdat></xch>" [SVDat "<escape" []]+ ptest "<xch><svdat><state>\n\t</state></svdat></xch>" [SVDat "\n\t" []]+ step "time"+ ptest "<xch><svdat><state>foo</state><time>2026-11-14T04:58:32.583506318Z</time></svdat></xch>"+ [SVDat "foo" [Time 1794632312.583506318]]+ ptest "<xch><svdat><state></state><time>2026-11-14T04:58:32Z</time></svdat></xch>"+ [SVDat "" [Time 1794632312]]+ step "pos"+ ptest "<xch><svdat><state>foo</state><pos><ned>\+ \<north>3</north><east>-4</east><down>5</down>\+ \</ned></pos></svdat></xch>"+ [SVDat "foo" [Pos $ NED 3.0 (-4.0) 5.0]]+ ptest "<xch><svdat><state>foo</state><pos><ned>\+ \<north>3.1</north><east>-4.2</east><down>5.5</down>\+ \</ned></pos></svdat></xch>"+ [SVDat "foo" [Pos $ NED 3.1 (-4.2) 5.5]]+ step "vel"+ ptest "<xch><svdat><state>foo</state><vel><ned>\+ \<north>+3</north><east>4</east><down>5.2</down>\+ \</ned></vel></svdat></xch>"+ [SVDat "foo" [Vel $ NED 3.0 4.0 5.2]]+ ptest "<xch><svdat><state>foo</state><vel><ned>\+ \<north>3.1e5</north><east>- 4.2</east><down>5.5</down>\+ \</ned></vel></svdat></xch>"+ [SVDat "foo" [Vel $ NED 3.1e5 (-4.2) 5.5]]++testMsnEvt :: TestTree+testMsnEvt = testCase "MsnEvt" $ do+ ptest "<xch><misn><trig>foo</trig></misn></xch>" [MsnEvt "foo" []]+ ptest "<xch><misn><trig>bar</trig><evt>up</evt></misn></xch>" [MsnEvt "bar" ["up"]]+ ptest "<xch><misn><trig>baz</trig><evt>left</evt><evt>right</evt></misn></xch>"+ [MsnEvt "baz" ["left", "right"]]++testComStat :: TestTree+testComStat = testCase "ComStat" $ do+ ptest "<xch><com><excellent/></com></xch>" [ComStat Excellent]+ ptest "<xch><com><good/></com></xch>" [ComStat Good]+ ptest "<xch><com><bad/></com></xch>" [ComStat Bad]+ ptest "<xch><com><lost/></com></xch>" [ComStat Lost]++testAEDat :: TestTree+testAEDat = testCase "AEDat" $ do+ ptest "<xch><aedat><ae>foo</ae></aedat></xch>" [AEDat "foo" []]+ ptest "<xch><aedat><ae>bar</ae><actdur><act>4</act><dur>5</dur></actdur></aedat></xch>"+ [AEDat "bar" [ActDurTime 4 5]]+ ptest "<xch><aedat><ae>baz</ae><dfn>zoo</dfn></aedat></xch>"+ [AEDat "baz" [Dfn "zoo"]]+ ptest "<xch><aedat><ae>bar</ae>\+ \<loc><geo>San Diego</geo>\+ \ <lat><cord><dgs>34</dgs><mns>-2</mns><sds>3.14</sds></cord></lat>\+ \ <lon><cord><dgs>0</dgs><mns>55</mns><sds>1.0001</sds></cord></lon>\+ \ <alt>45</alt>\+ \</loc></aedat></xch>"+ [AEDat "bar" [LocGeo "San Diego" [(Cord 34 (-2) 3.14, Cord 0 55 1.0001)] $ Just 45]]++ptest :: String -> Xch -> Assertion+ptest i expt = case parse pxch "" i of+ Left err -> assertFailure $ errorBundlePretty err+ Right act -> act @?= expt++ptrip :: TestTree+ptrip = testProperty "Trip" $ property $ do+ x <- forAll gxch+ tripping x wxch (parse pxch "")++gxch :: MonadGen m => m Xch+gxch = glist gxdat++gxdat :: MonadGen m => m XDat+gxdat = Gen.choice+ [ SVDat <$> gstr "state" <*> glist gsvdat+ , MsnEvt <$> gstr "trig" <*> glist (gstr "evt")+ , ComStat <$> Gen.element [Excellent, Good, Bad, Lost]+ , AEDat <$> gstr "ae" <*> glist gaeinf+ ]++gsvdat :: MonadGen m => m SVDat+gsvdat = Gen.choice+ [ Time <$> Gen.realFrac_ (Range.linearFracFrom 0 0 $ fromIntegral (maxBound :: Word32))+ , Pos <$> gned+ , Vel <$> gned+ ]++gaeinf :: MonadGen m => m AEInfo+gaeinf = Gen.choice+ [ ActDurTime <$> gint <*> gint+ , LocGeo <$> gstr "geo" <*> glist ((,) <$> gcord <*> gcord) <*> Gen.maybe gdbl+ , Dfn <$> gstr "dfn"+ ]++gcord :: MonadGen m => m Cord+gcord = Cord <$> gint <*> gint <*> gdbl++gned :: MonadGen m => m NED+gned = NED <$> gdbl <*> gdbl <*> gdbl++gint :: MonadGen m => m Integer+gint = Gen.integral_ $ Range.linearFrom 0 (-999999999999999) 999999999999999++gdbl :: MonadGen m => m Double+gdbl = Gen.double $ Range.linearFracFrom 0 (-999999999999999) 999999999999999++gstr :: MonadGen m => String -> m String+gstr t = do+ str <- Gen.string rangeList Gen.unicode+ if concat ["<", t, "/>"] `isInfixOf` str+ then Gen.discard+ else return str++glist :: MonadGen m => m a -> m [a]+glist = Gen.list rangeList++rangeList :: Range Int+rangeList = Range.linearFrom 0 0 64
+ xch.cabal view
@@ -0,0 +1,57 @@+cabal-version: 3.0+name: xch+version: 0.1.0.0+synopsis: Space Data Exchange+description: ASTM F3514-21+homepage: https://github.com/dopamane/xch+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++source-repository head+ type: git+ location: https://github.com/dopamane/xch++library+ default-language: Haskell2010+ ghc-options: -Wall -O2+ hs-source-dirs: lib+ exposed-modules: Data.Xch+ build-depends:+ base < 5,+ megaparsec,+ time++executable xch+ default-language: Haskell2010+ ghc-options: -Wall -O2 -threaded+ hs-source-dirs: exe+ main-is: Main.hs+ other-modules: Paths_xch+ autogen-modules: Paths_xch+ build-depends:+ base < 5,+ megaparsec,+ optparse-applicative,+ xch,++test-suite test+ default-language: Haskell2010+ ghc-options: -Wall -O2 -threaded+ hs-source-dirs: test+ type: exitcode-stdio-1.0+ main-is: Main.hs+ build-depends:+ base < 5,+ hedgehog,+ megaparsec,+ tasty,+ tasty-hedgehog,+ tasty-hunit,+ xch