Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ jobs:
- name: Build Project
run: cmake --build build --config Release

build-examples:
name: Build Examples
runs-on: ${{ matrix.os }}-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu, windows]
steps:
- name: Checkout
uses: actions/[email protected]

- name: Configure Project
uses: threeal/[email protected]
with:
options: BUILD_EXAMPLES=ON

- name: Build Examples
run: cmake --build build --target examples

build-docs:
name: Build Documentation
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_include_directories(errors PUBLIC include)
# Check if this project is the main project
if(NOT_SUBPROJECT)
option(BUILD_DOCS "Enable documentations build" OFF)
option(BUILD_EXAMPLES "Enable examples build" OFF)

# Statically analyze code by checking for warnings
cpmaddpackage(gh:threeal/[email protected])
Expand Down Expand Up @@ -70,6 +71,10 @@ if(NOT_SUBPROJECT)
include(cmake/add_xml_docs.cmake)
add_xml_docs(errors_docs include/errors/error.hpp)
endif()

if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
endif()

add_subdirectory(components)
9 changes: 9 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_custom_target(examples)

add_executable(example_print_hex print_hex.cpp)
target_link_libraries(example_print_hex PRIVATE errors)
add_dependencies(examples example_print_hex)

add_executable(example_read_file read_file.cpp)
target_link_libraries(example_read_file PRIVATE errors errors_format)
add_dependencies(examples example_read_file)
27 changes: 27 additions & 0 deletions examples/print_hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <errors/error.hpp>
#include <iostream>

errors::Error print_hex(const char* number_str) {
int number = std::atoi(number_str);
if (number == 0) {
return errors::make("is not a number");
}

std::cout << std::hex << number << std::endl;
return errors::nil();
}

int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "usage: " << argv[0] << " <number>" << std::endl;
return 1;
}

const auto err = print_hex(argv[1]);
if (err) {
std::cerr << err << std::endl;
return 1;
}

return 0;
}
32 changes: 32 additions & 0 deletions examples/read_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <errors/error.hpp>
#include <errors/format.hpp>
#include <fstream>

errors::Error read_file(const char* filepath) {
std::ifstream file(filepath);
if (!file.is_open()) {
return errors::format("failed to open `{}` ({})", filepath, static_cast<int>(file.rdstate()));
}

std::string line;
while (std::getline(file, line)) {
fmt::print("{}\n", line);
}

return errors::nil();
}

int main(int argc, char **argv) {
if (argc < 2) {
fmt::print(stderr, "usage: {} <filepath>\n", argv[0]);
return 1;
}

const auto err = read_file(argv[1]);
if (err) {
fmt::print(stderr, "{}\n", err);
return 1;
}

return 0;
}