Skip to content

Commit

Permalink
switch to more structured toml validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy authored and blackheaven committed Oct 4, 2023
1 parent 0a28007 commit b04d938
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion code/hsec-tools/hsec-tools.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ library
containers >= 0.6 && < 0.7,
commonmark ^>= 0.2.2,
aeson >= 2.0.1.0 && < 3,
toml-parser ^>=1.2.1.0,
toml-parser ^>=1.3.0.0,
pandoc-types >= 1.22 && < 2,
pathwalk >= 0.3,
parsec >= 3 && < 4,
Expand Down
9 changes: 5 additions & 4 deletions code/hsec-tools/src/Security/Advisories/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import qualified Commonmark.Parser as Commonmark
import Commonmark.Types (HasAttributes(..), IsBlock(..), IsInline(..), Rangeable(..), SourceRange(..))
import Commonmark.Pandoc (Cm(unCm))
import qualified Toml
import qualified Toml.Pretty as Toml
import qualified Toml.FromValue as Toml
import qualified Toml.FromValue.Matcher as Toml
import qualified Toml.ToValue as Toml
Expand Down Expand Up @@ -77,7 +78,7 @@ data ParseAdvisoryError
= MarkdownError Commonmark.ParseError T.Text
| MarkdownFormatError T.Text
| TomlError String T.Text
| AdvisoryError [String] T.Text
| AdvisoryError [Toml.MatchMessage] T.Text
deriving stock (Eq, Show, Generic)

-- | The main parsing function. 'OutOfBandAttributes' are handled
Expand Down Expand Up @@ -126,10 +127,10 @@ parseAdvisory policy attrs raw = do
(Commonmark.commonmark "input" raw :: Either Commonmark.ParseError (Html ()))

case parseAdvisoryTable attrs policy doc summary details html table of
Toml.Failure es -> Left (AdvisoryError es (T.pack (unlines es)))
Toml.Failure es -> Left (AdvisoryError es (T.pack (unlines (map Toml.prettyMatchMessage es))))
Toml.Success warnings adv
| null warnings -> pure adv
| otherwise -> Left (AdvisoryError warnings (T.pack (unlines warnings))) -- treat warnings as errors
| otherwise -> Left (AdvisoryError warnings (T.pack (unlines (map Toml.prettyMatchMessage warnings)))) -- treat warnings as errors

where
firstPretty
Expand All @@ -154,7 +155,7 @@ parseAdvisoryTable
-> T.Text -- ^ details
-> T.Text -- ^ rendered HTML
-> Toml.Table
-> Toml.Result Advisory
-> Toml.Result Toml.MatchMessage Advisory
parseAdvisoryTable oob policy doc summary details html tab =
Toml.runMatcher $
do fm <- Toml.fromValue (Toml.Table tab)
Expand Down

0 comments on commit b04d938

Please sign in to comment.