8 #include <fmt/format.h>
9 #include <fmt/ostream.h>
18 auto it = std::begin(ex);
19 auto end = std::end(ex);
22 throw std::invalid_argument(
"empty expression is invalid");
35 throw std::invalid_argument(
36 fmt::format(
"invalid operator '{}', must be one of '+' or '-': '{}'", *it, ex));
41 throw std::invalid_argument(
42 fmt::format(
"unexpected end of expression, expected ' ' or scope: '{}'", ex));
61 throw std::invalid_argument(
62 fmt::format(
"invalid scope '{}', must be one of 'v' (value), 'e' (ESO hierarchical) or "
63 "'c' (commentary): '{}'",
73 throw std::invalid_argument(
74 fmt::format(
"unexpected end of expression, expected pattern: '{}'", ex));
79 throw std::invalid_argument(
80 fmt::format(
"whitespace ' ' expected after scope, got '{}': ", *it, ex));
86 throw std::invalid_argument(
87 fmt::format(
"unexpected end of expression, expected pattern: '{}'", ex));
91 std::string pattern(it, end);
92 return {op, scope, pattern};
125 auto name = std::string(kw.
name);
126 auto match = fnmatch(rule.
pattern.c_str(), name.c_str(), 0);
130 }
else if (match == FNM_NOMATCH) {
134 throw std::invalid_argument(fmt::format(
"invalid fnmatch pattern '{}'", rule.
pattern));
139 std::string
RegexReplace(std::string_view str, std::regex
const& re,
char const* fmt) {
141 std::regex_replace(std::back_inserter(result), std::begin(str), std::end(str), re, fmt);
153 for (
auto const& rule : ex.
GetRules()) {
176 std::throw_with_nested(std::runtime_error(fmt::format(
177 "Keyword transform failed for keyword `{}` with transform format `{}`", keyword, fmt)));
187 std::throw_with_nested(std::runtime_error(fmt::format(
188 "Keyword transform failed for keyword `{}` with transform format `{}`", keyword, fmt)));
197 components.
name = new_name;
198 auto new_kw =
Format(components);
199 if (new_kw.GetType() != keyword.
GetType()) {
200 throw std::runtime_error(
201 fmt::format(
"Keyword transform mutated keyword into a different type. Before: {}, "
202 "after: {}. Before transformation: `{}`, after: `{}`",
206 new_kw.GetRecord()));
208 if (new_kw.GetComponents().value != components.value) {
209 throw std::runtime_error(fmt::format(
210 "Keyword transform mutated keyword such that the value field was affected "
211 " Before transformation: `{}`, after: `{}`",
213 new_kw.GetRecord()));
217 std::throw_with_nested(std::runtime_error(
218 fmt::format(
"Keyword transform failed for keyword `{}` with transform format `{}`",
Create keyword expression that memoize the provided string pattern.
constexpr std::vector< detail::Rule > const & GetRules() const noexcept
Represents the literal 80-character FITS keyword record.
constexpr Components GetComponents() const &noexcept
Get components of the keyword in its literal form with insignifant whitespaces removed.
constexpr KeywordType GetType() const noexcept
std::string_view GetRecord() const &noexcept
constexpr KeywordNameView GetName() const &noexcept
Query logical keyword name.
Contains data structure for FITS keywords.
std::optional< Operator > KeywordMatch(fits::KeywordNameView kw, Rule const &rule)
Determines if keyword match rule and if yes, whether it matches;.
Rule ParseEx(std::string_view ex)
Parse expression of the form documented in KeywordEx.
std::string RegexReplace(std::string_view str, std::regex const &re, char const *fmt)
bool KeywordScopeMatch(fits::KeywordType kwtype, Scope scope)
Represents a keyword rule expression.
fits::KeywordVariant KeywordTransform(fits::KeywordVariant const &keyword, std::regex const &re, char const *fmt)
Transforms keyword name using regex.
bool KeywordMatch(fits::KeywordVariant const &keyword, KeywordEx const &ex)
constexpr KeywordNameView GetKeywordName(EsoKeyword const &keyword) noexcept
Get keyword name from keyword.
KeywordType
Type of FITS keyword.
@ Eso
An ESO hiearchical keyword.
@ Commentary
A commentary keyword, which are keywords that do not fall into the previous categories.
LiteralKeyword Format(KeywordVariant const &keyword)
std::variant< ValueKeyword, EsoKeyword, LiteralKeyword > KeywordVariant
The different variants of keywords that are supported.
BasicKeyword< ValueKeywordTraits > ValueKeyword
Standard FITS value keyword.
BasicKeyword< EsoKeywordTraits > EsoKeyword
ESO hiearchical keyword.
A type safe version of LiteralKeyword that consist of the three basic components of a FITS keyword ke...
std::string name
Trimmed keyword name.
std::optional< std::string > comment
Trimmed keyword comment.