From 5d8f8dac3529fba0a69215e4184b49713c74874a Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sat, 6 Nov 2021 11:36:24 -0400 Subject: [PATCH] validateWriteContext: Refactor to return the underlying reason for invalidity directly --- src/commands/CmdContext.cpp | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/commands/CmdContext.cpp b/src/commands/CmdContext.cpp index e09e76e54..8f4b1821c 100644 --- a/src/commands/CmdContext.cpp +++ b/src/commands/CmdContext.cpp @@ -117,41 +117,36 @@ std::string CmdContext::joinWords (const std::vector & words, unsig // invalid due to a wrong modifier use, the modifier string will contain the // first invalid modifier. // -bool CmdContext::validateWriteContext (const std::vector & lexedArgs, std::string& modifier_token) +bool CmdContext::validateWriteContext (const std::vector & lexedArgs, std::string& reason) { - bool contains_or = false; - bool contains_modifier = false; - bool contains_tag_exclusion = false; - for (auto &arg: lexedArgs) { if (arg._lextype == Lexer::Type::op) if (arg.attribute ("raw") == "or") { - contains_or = true; - break; + reason = "contains the 'OR' operator"; + return false; } if (arg._lextype == Lexer::Type::pair) { auto modifier = arg.attribute ("modifier"); if (modifier != "" && modifier != "is" && modifier != "equals") { - contains_modifier = true; - modifier_token = arg.attribute ("raw"); - break; + reason = format ("contains an attribute modifier '{1}'", arg.attribute ("raw")); + return false; } } if (arg._lextype == Lexer::Type::tag) { if (arg.attribute ("sign") == "-") { - contains_tag_exclusion = true; - break; + reason = format ("contains tag exclusion '{1}'", arg.attribute ("raw")); + return false; } } } - return not contains_or and not contains_modifier and not contains_tag_exclusion; + return true; } ////////////////////////////////////////////////////////////////////////////////