CmdConfig: Extract write context validation into a separate method

This commit is contained in:
Tomas Babej 2021-09-28 00:52:56 -04:00
parent 2b0730d905
commit 5f8ec9bbe0
2 changed files with 38 additions and 27 deletions

View file

@ -106,6 +106,39 @@ std::string CmdContext::joinWords (const std::vector <std::string>& words, unsig
return value;
}
////////////////////////////////////////////////////////////////////////////////
// Validate the context as valid for writing and fail the write context definition
// A valid write context:
// - does not contain any operators except AND
// - does not use modifiers
//
// Returns True if the context is a valid write context. If the context is
// invalid due to a wrong modifier use, the modifier string will contain the
// first invalid modifier.
bool CmdContext::validateWriteContext (const std::vector <A2>& lexedArgs, std::string& modifier_token)
{
bool contains_or = false;
bool contains_modifier = false;
for (auto &arg: lexedArgs) {
if (arg._lextype == Lexer::Type::op)
if (arg.attribute ("raw") == "or")
contains_or = true;
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;
}
}
}
return not contains_or and not contains_modifier;
}
////////////////////////////////////////////////////////////////////////////////
// Returns all user defined contexts.
//
@ -177,38 +210,14 @@ void CmdContext::defineContext (const std::vector <std::string>& words, std::str
! confirm (format ("The filter '{1}' matches 0 pending tasks. Do you wish to continue?", value)))
throw std::string ("Context definition aborted.");
// Validate the context as valid for writing and fail the write context definition
// A valid write context:
// - does not contain any operators except AND
// - does not use modifiers
bool contains_or = false;
bool contains_modifier = false;
std::string modifier_token;
for (auto &arg: lexedArgs) {
if (arg._lextype == Lexer::Type::op)
if (arg.attribute ("raw") == "or")
contains_or = true;
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");
out << "The modifier value is '" << modifier << "'.\n";
break;
}
}
}
bool valid_write_context = not contains_or and not contains_modifier;
std::string modifier_token = "";
bool valid_write_context = CmdContext::validateWriteContext (lexedArgs, modifier_token);
if (! valid_write_context)
{
std::stringstream warning;
warning << format ("The filter '{1}' is not a valid modification string, because it contains ", value)
<< ( contains_or ? "the OR operator." : format ("an attribute modifier ({1}).", modifier_token) )
<< ( modifier_token.empty () ? "the OR operator." : format ("an attribute modifier ({1}).", modifier_token) )
<< "\nAs such, value for the write context cannot be set (context will not apply on task add / task log).\n\n"
<< format ("Please use 'task config context.{1}.write <default mods>' to set default attribute values for new tasks in this context manually.\n\n", words[1]);
out << colorizeFootnote (warning.str ());

View file

@ -29,6 +29,7 @@
#include <string>
#include <Command.h>
#include <CLI2.h>
class CmdContext : public Command
{
@ -36,6 +37,7 @@ public:
CmdContext ();
int execute (std::string&);
std::string joinWords (const std::vector <std::string>&, unsigned int, unsigned int = 0);
bool validateWriteContext (const std::vector <A2>&, std::string&);
static std::vector <std::string> getContexts ();
void defineContext (const std::vector <std::string>&, std::stringstream&);
void deleteContext (const std::vector <std::string>&, std::stringstream&);