catch_reporter_spec_parser.hpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #ifndef CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED 9 #define CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED 10 11 #include <catch2/internal/catch_console_colour.hpp> 12 #include <catch2/internal/catch_optional.hpp> 13 #include <catch2/internal/catch_stringref.hpp> 14 15 #include <map> 16 #include <string> 17 #include <vector> 18 19 namespace Catch { 20 21 enum class ColourMode : std::uint8_t; 22 23 namespace Detail { 24 //! Splits the reporter spec into reporter name and kv-pair options 25 std::vector<std::string> splitReporterSpec( StringRef reporterSpec ); 26 27 Optional<ColourMode> stringToColourMode( StringRef colourMode ); 28 } 29 30 /** 31 * Structured reporter spec that a reporter can be created from 32 * 33 * Parsing has been validated, but semantics have not. This means e.g. 34 * that the colour mode is known to Catch2, but it might not be 35 * compiled into the binary, and the output filename might not be 36 * openable. 37 */ 38 class ReporterSpec { 39 std::string m_name; 40 Optional<std::string> m_outputFileName; 41 Optional<ColourMode> m_colourMode; 42 std::map<std::string, std::string> m_customOptions; 43 44 friend bool operator==( ReporterSpec const& lhs, 45 ReporterSpec const& rhs ); 46 friend bool operator!=( ReporterSpec const& lhs, 47 ReporterSpec const& rhs ) { 48 return !( lhs == rhs ); 49 } 50 51 public: 52 ReporterSpec( 53 std::string name, 54 Optional<std::string> outputFileName, 55 Optional<ColourMode> colourMode, 56 std::map<std::string, std::string> customOptions ); 57 58 std::string const& name() const { return m_name; } 59 60 Optional<std::string> const& outputFile() const { 61 return m_outputFileName; 62 } 63 64 Optional<ColourMode> const& colourMode() const { return m_colourMode; } 65 66 std::map<std::string, std::string> const& customOptions() const { 67 return m_customOptions; 68 } 69 }; 70 71 /** 72 * Parses provided reporter spec string into 73 * 74 * Returns empty optional on errors, e.g. 75 * * field that is not first and not a key+value pair 76 * * duplicated keys in kv pair 77 * * unknown catch reporter option 78 * * empty key/value in an custom kv pair 79 * * ... 80 */ 81 Optional<ReporterSpec> parseReporterSpec( StringRef reporterSpec ); 82 83 } 84 85 #endif // CATCH_REPORTER_SPEC_PARSER_HPP_INCLUDED