Skip to content

Commit

Permalink
Merge pull request #1487 from Karry/style-error-struct
Browse files Browse the repository at this point in the history
expose style errors and warnings as structure, not just a string
  • Loading branch information
Framstag committed Aug 22, 2023
2 parents 44db3ac + 713501e commit c7674cf
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 89 deletions.
9 changes: 2 additions & 7 deletions StyleEditor/src/StyleAnalyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,11 @@ void StyleAnalyser::update(QString content)

QSet<int> errorLines;
QSet<int> warningLines;
int line;
for (const auto &w: styleConfig->GetWarnings()) {
// TODO: expose warning as some structure with line number
osmscout::StringToNumberSigned(w, line);
warningLines << line;
warningLines << w.GetLine();
}
for (const auto &w: styleConfig->GetErrors()) {
// TODO: expose warning as some structure with line number
osmscout::StringToNumberSigned(w, line);
errorLines << line;
errorLines << w.GetLine();
}

emit problematicLines(errorLines, warningLines);
Expand Down
29 changes: 0 additions & 29 deletions libosmscout-client-qt/include/osmscoutclientqt/DBInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,6 @@

namespace osmscout {

/**
* \ingroup QtAPI
*/
class OSMSCOUT_CLIENT_QT_API StyleError
{
enum StyleErrorType {
Symbol, Error, Warning, Exception
};

public:
StyleError(StyleErrorType type, int line, int column, const QString &text) :
type(type), line(line), column(column), text(text){}
explicit StyleError(QString msg);

StyleErrorType GetType() const { return type; }
QString GetTypeName() const;
int GetLine() const{ return line; }
int GetColumn() const{ return column; }
const QString &GetText() const { return text; }
QString GetDescription() const {return GetTypeName()+": "+GetText();}

private:
StyleErrorType type;
int line;
int column;
QString text;
};


/**
* \ingroup QtAPI
*
Expand Down
47 changes: 5 additions & 42 deletions libosmscout-client-qt/src/osmscoutclientqt/DBInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,14 @@

#include <osmscoutclientqt/DBInstance.h>

#include <osmscoutmap/StyleError.h>

#include <osmscout/log/Logger.h>

#include <QRegExp>

namespace osmscout {

StyleError::StyleError(QString msg){
QRegExp rx("(\\d+),(\\d+) (Symbol|Error|Warning|Exception):(.*)");
if(rx.exactMatch(msg)){
line = rx.cap(1).toInt();
column = rx.cap(2).toInt();
if(rx.cap(3) == "Symbol"){
type = Symbol;
} else if(rx.cap(3) == "Error"){
type = Error;
} else if(rx.cap(3) == "Warning"){
type = Warning;
} else {
type = Exception;
}
text = rx.cap(4);
}
}

QString StyleError::GetTypeName() const
{
switch(type){
case Symbol:
return QString("symbol");
break;
case Error:
return QString("error");
break;
case Warning:
return QString("warning");
break;
case Exception:
return QString("exception");
break;
default:
return QString("???");
}
}

bool DBInstance::LoadStyle(QString stylesheetFilename,
std::unordered_map<std::string,bool> stylesheetFlags,
QList<StyleError> &errors)
Expand Down Expand Up @@ -100,11 +64,10 @@ bool DBInstance::LoadStyle(QString stylesheetFilename,
osmscout::log.Info()<< "Created new style with " << stylesheetFilename.toStdString();
}
else {
std::list<std::string> errorsStrings=newStyleConfig->GetErrors();
std::list<StyleError> errorsStrings=newStyleConfig->GetErrors();

for(const auto& errorString : errorsStrings) {
StyleError err(QString::fromStdString(errorString));
qWarning() << "Style error:" << err.GetDescription();
for(const auto& err : errorsStrings) {
qWarning() << "Style error:" << QString::fromStdString(err.GetDescription());
errors.append(err);
}

Expand Down
2 changes: 1 addition & 1 deletion libosmscout-client-qt/src/osmscoutclientqt/MapWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ QString MapWidget::firstStylesheetErrorDescription() const
QList<StyleError> errors=dbThread->GetStyleErrors();
if (errors.isEmpty())
return "";
return errors.first().GetDescription();
return QString::fromStdString(errors.first().GetDescription());
}

bool MapWidget::toggleDebug()
Expand Down
1 change: 1 addition & 0 deletions libosmscout-map/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(HEADER_FILES
include/osmscoutmap/LabelProvider.h
include/osmscoutmap/LabelPath.h
include/osmscoutmap/Styles.h
include/osmscoutmap/StyleError.h
include/osmscoutmap/StyleDescription.h
include/osmscoutmap/StyleConfig.h
include/osmscoutmap/StyleProcessor.h
Expand Down
1 change: 1 addition & 0 deletions libosmscout-map/include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ osmscoutmapHeader = [
'osmscoutmap/MapParameter.h',
'osmscoutmap/LabelProvider.h',
'osmscoutmap/LabelPath.h',
'osmscoutmap/StyleError.h',
'osmscoutmap/Styles.h',
'osmscoutmap/StyleDescription.h',
'osmscoutmap/StyleConfig.h',
Expand Down
9 changes: 5 additions & 4 deletions libosmscout-map/include/osmscoutmap/StyleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <osmscoutmap/LabelProvider.h>
#include <osmscoutmap/StyleDescription.h>
#include <osmscoutmap/Styles.h>
#include <osmscoutmap/StyleError.h>

namespace osmscout {

Expand Down Expand Up @@ -634,8 +635,8 @@ namespace osmscout {
private:
std::unordered_map<std::string,bool> flags;
std::unordered_map<std::string,StyleConstantRef> constants;
std::list<std::string> errors;
std::list<std::string> warnings;
std::list<StyleError> errors;
std::list<StyleError> warnings;

private:
void Reset();
Expand Down Expand Up @@ -848,8 +849,8 @@ namespace osmscout {
bool Load(const std::string& styleFile,
ColorPostprocessor colorPostprocessor=nullptr,
bool submodule=false);
const std::list<std::string>& GetErrors() const;
const std::list<std::string>& GetWarnings() const;
const std::list<StyleError>& GetErrors() const;
const std::list<StyleError>& GetWarnings() const;
//@}
};

Expand Down
107 changes: 107 additions & 0 deletions libosmscout-map/include/osmscoutmap/StyleError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#ifndef OSMSCOUT_STYLEERROR_H
#define OSMSCOUT_STYLEERROR_H

/*
This source is part of the libosmscout library
Copyright (C) 2023 Lukas Karas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <osmscoutmap/MapImportExport.h>

#include <string>

namespace osmscout {

/**
* \ingroup Stylesheet
*/
class OSMSCOUT_MAP_API StyleError
{
public:
enum StyleErrorType {
Symbol, Error, Warning, Exception
};

public:
StyleError(StyleErrorType type, int line, int column, const std::string &text) :
type(type), line(line), column(column), text(text){}

StyleError(const StyleError&) = default;
StyleError(StyleError&&) = default;

StyleError& operator=(const StyleError&) = default;
StyleError& operator=(StyleError&&) = default;

~StyleError() = default;

StyleErrorType GetType() const
{
return type;
}

std::string GetTypeName() const
{
switch(type){
case Symbol:
return "Symbol";
case Error:
return "Error";
case Warning:
return "Warning";
case Exception:
return "Exception";
default:
assert(false);
return "???";
}
}

int GetLine() const
{
return line;
}

int GetColumn() const
{
return column;
}

const std::string &GetText() const
{
return text;
}

std::string GetShortDescription() const
{
return GetTypeName() + ": " + GetText();
}

std::string GetDescription() const
{
return std::to_string(GetLine()) + "," + std::to_string(GetColumn()) + " " + GetShortDescription();
}

private:
StyleErrorType type;
int line;
int column;
std::string text;
};

}

#endif //OSMSCOUT_STYLEERROR_H
12 changes: 6 additions & 6 deletions libosmscout-map/src/osmscoutmap/StyleConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,16 +1621,16 @@ namespace osmscout {
for (const auto& err : parser->errors->errors) {
switch(err.type) {
case oss::Errors::Err::Symbol:
errors.push_back(std::to_string(err.line)+","+std::to_string(err.column)+std::string(" Symbol:")+err.text);
errors.push_back(StyleError(StyleError::Symbol, err.line, err.column, err.text));
break;
case oss::Errors::Err::Error:
errors.push_back(std::to_string(err.line)+","+std::to_string(err.column)+std::string(" Error:")+err.text);
errors.push_back(StyleError(StyleError::Error, err.line, err.column, err.text));
break;
case oss::Errors::Err::Warning:
warnings.push_back(std::to_string(err.line)+","+std::to_string(err.column)+std::string(" Warning:")+err.text);
warnings.push_back(StyleError(StyleError::Warning, err.line, err.column, err.text));
break;
case oss::Errors::Err::Exception:
errors.push_back(std::to_string(err.line)+","+std::to_string(err.column)+std::string(" Exception:")+err.text);
errors.push_back(StyleError(StyleError::Exception, err.line, err.column, err.text));
break;
default:
break;
Expand Down Expand Up @@ -1713,12 +1713,12 @@ namespace osmscout {
return success;
}

const std::list<std::string>& StyleConfig::GetErrors() const
const std::list<StyleError>& StyleConfig::GetErrors() const
{
return errors;
}

const std::list<std::string>& StyleConfig::GetWarnings() const
const std::list<StyleError>& StyleConfig::GetWarnings() const
{
return warnings;
}
Expand Down

0 comments on commit c7674cf

Please sign in to comment.