Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause 2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp) 3 : /// @file 4 : /// @author Simon Heybrock 5 : #pragma once 6 : 7 : #include <algorithm> 8 : #include <stdexcept> 9 : #include <string> 10 : 11 : #include "scipp-dataset_export.h" 12 : #include "scipp/core/except.h" 13 : #include "scipp/dataset/string.h" 14 : #include "scipp/variable/except.h" 15 : 16 : namespace scipp::dataset { 17 : 18 : class Dataset; 19 : class DataArray; 20 : 21 : } // namespace scipp::dataset 22 : 23 : namespace scipp::except { 24 : 25 : struct SCIPP_DATASET_EXPORT DataArrayError : public Error<dataset::DataArray> { 26 : explicit DataArrayError(const std::string &msg); 27 : }; 28 : 29 : template <> 30 : [[noreturn]] SCIPP_DATASET_EXPORT void 31 : throw_mismatch_error(const dataset::DataArray &expected, 32 : const dataset::DataArray &actual, 33 : const std::string &optional_message); 34 : 35 : struct SCIPP_DATASET_EXPORT DatasetError : public Error<dataset::Dataset> { 36 : explicit DatasetError(const std::string &msg); 37 : }; 38 : 39 : template <> 40 : [[noreturn]] SCIPP_DATASET_EXPORT void 41 : throw_mismatch_error(const dataset::Dataset &expected, 42 : const dataset::Dataset &actual, 43 : const std::string &optional_message); 44 : 45 : struct SCIPP_DATASET_EXPORT CoordMismatchError : public DatasetError { 46 : CoordMismatchError(const Dim dim, const Variable &a, const Variable &b, 47 : std::string_view opname = ""); 48 : using DatasetError::DatasetError; 49 : }; 50 : 51 : } // namespace scipp::except 52 : 53 : namespace scipp::expect { 54 : template <class Key, class Value> 55 227278 : void contains(const scipp::dataset::SizedDict<Key, Value> &a, const Key &b) { 56 : using core::to_string; 57 227278 : if (!a.contains(b)) 58 5616 : throw except::NotFoundError("Expected '" + to_string(b) + "' in " + 59 : scipp::dataset::dict_keys_to_string(a) + "."); 60 221662 : } 61 : 62 : template <> 63 : SCIPP_DATASET_EXPORT void contains(const scipp::dataset::Dataset &a, 64 : const std::string &b); 65 : } // namespace scipp::expect 66 : 67 : namespace scipp::dataset::expect { 68 : 69 : SCIPP_DATASET_EXPORT void coords_are_superset(const DataArray &a, 70 : const DataArray &b, 71 : std::string_view opname); 72 : SCIPP_DATASET_EXPORT void coords_are_superset(const Coords &a, const Coords &b, 73 : std::string_view opname); 74 : SCIPP_DATASET_EXPORT void matching_coord(const Dim dim, const Variable &a, 75 : const Variable &b, 76 : std::string_view opname); 77 : 78 : SCIPP_DATASET_EXPORT void is_key(const Variable &key); 79 : 80 : } // namespace scipp::dataset::expect