Skip to content

Commit 1d58822

Browse files
Use pattern synonyms to simplify xml match
1 parent 9424f91 commit 1d58822

File tree

1 file changed

+6
-14
lines changed

1 file changed

+6
-14
lines changed

code/cwe/RenderCsvData.hs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{- cabal:
33
build-depends: base, xml
44
-}
5-
{-# LANGUAGE NamedFieldPuns #-}
5+
{-# LANGUAGE NamedFieldPuns, PatternSynonyms #-}
66
-- | Use this script to update the Security.CWE.Data module:
77
-- Download and extract https://cwe.mitre.org/data/xml/cwec_latest.xml.zip
88
-- Run the following command: cat cwec_v4.12.xml | ./RenderCsvData.hs | fourmolu --stdin-input-file ./src/Security/CWE/Data.hs > src/Security/CWE/Data.hs
@@ -24,21 +24,13 @@ data Weakness = Weakness
2424
, wname :: String
2525
}
2626

27+
pattern XElement name content <- XML.Element (XML.QName name _ _) _ content _
28+
2729
readXML :: String -> [Weakness]
2830
readXML str = case XML.parseXMLDoc str of
29-
Just
30-
( XML.Element
31-
(XML.QName "Weakness_Catalog" _ _)
32-
_
33-
( _
34-
: ( XML.Elem
35-
((XML.Element (XML.QName "Weaknesses" _ _) _ xs _))
36-
)
37-
: _
38-
)
39-
_
40-
) -> mapMaybe toWeakness xs
41-
n -> error (show n)
31+
Just (XElement "Weakness_Catalog" (_ : (XML.Elem (XElement "Weaknesses" xs)) : _)) ->
32+
mapMaybe toWeakness xs
33+
n -> error $ "Couldn't match: " <> take 512 (show n)
4234
where
4335
toWeakness (XML.Elem (XML.Element (XML.QName "Weakness" _ _) attrs _ _)) = Just (Weakness{wid, wname})
4436
where

0 commit comments

Comments
 (0)