$darkmode
path.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 #ifndef FABLE_SCHEMA_PATH_HPP_
26 #define FABLE_SCHEMA_PATH_HPP_
27 
28 #include <limits> // for numeric_limits<>
29 #include <string> // for string
30 #include <utility> // for move
31 
32 #include <boost/filesystem/path.hpp> // for path
33 
34 #include <fable/schema/interface.hpp> // for Base<>
35 
36 namespace fable {
37 
38 // Forward declarations:
39 class Environment; // from <fable/environment.hpp>
40 
41 namespace schema {
42 
58 class Path : public Base<Path> {
59  public: // Types and Constructors
60  using Type = boost::filesystem::path;
61 
65  enum class State {
66  Any,
67  Absent,
68  Exists,
69  Executable,
70  FileExists,
71  DirExists,
72  NotFile,
73  NotDir,
74  };
75 
76  Path(Type* ptr, std::string&& desc) : Base(JsonType::string, std::move(desc)), ptr_(ptr) {}
77 
78  public: // Special
82  State state() const { return req_state_; }
83 
87  Path state(State s) && {
88  req_state_ = s;
89  return std::move(*this);
90  }
91  void set_state(State s) { req_state_ = s; }
92 
93  Path absent() && { return std::move(*this).state(State::Absent); }
94  Path exists() && { return std::move(*this).state(State::Exists); }
95  Path executable() && { return std::move(*this).state(State::Executable); }
96  Path file_exists() && { return std::move(*this).state(State::FileExists); }
97  Path dir_exists() && { return std::move(*this).state(State::DirExists); }
98  Path not_file() && { return std::move(*this).state(State::NotFile); }
99  Path not_dir() && { return std::move(*this).state(State::NotDir); }
100 
101  Path not_empty() && {
102  set_min_length(1);
103  return std::move(*this);
104  }
105 
109  Path absolute() && {
110  req_abs_ = true;
111  return std::move(*this);
112  }
113 
119  bool resolve() const { return resolve_; }
120 
133  Path resolve(bool value) && {
134  resolve_ = value;
135  return std::move(*this);
136  }
137  void set_resolve(bool value) { resolve_ = value; }
138 
139  bool normalize() const { return normalize_; }
140  Path normalize(bool value) && {
141  normalize_ = value;
142  return std::move(*this);
143  }
144  void set_normalize(bool value) { normalize_ = value; }
145 
146  bool interpolate() const { return interpolate_; }
147  void set_interpolate(bool value) { interpolate_ = value; }
148  Path interpolate(bool value) && {
149  interpolate_ = value;
150  return std::move(*this);
151  }
152 
153  Environment* environment() const { return env_; }
154  void set_environment(Environment* env) { env_ = env; }
155  Path environment(Environment* env) && {
156  env_ = env;
157  return std::move(*this);
158  }
159 
160  size_t min_length() const { return min_length_; }
161  void set_min_length(size_t value) { min_length_ = value; }
162  Path min_length(size_t value) && {
163  min_length_ = value;
164  return std::move(*this);
165  }
166 
167  size_t max_length() const { return max_length_; }
168  void set_max_length(size_t value) { max_length_ = value; }
169  Path max_length(size_t value) && {
170  max_length_ = value;
171  return std::move(*this);
172  }
173 
174  const std::string& pattern() const { return pattern_; }
175  void set_pattern(const std::string& value) { pattern_ = value; }
176  Path pattern(const std::string& value) && {
177  pattern_ = value;
178  return std::move(*this);
179  }
180 
181  public: // Overrides
182  Json json_schema() const override;
183  void validate(const Conf& c) const override;
184 
185  using Interface::to_json;
186  void to_json(Json& j) const override {
187  assert(ptr_ != nullptr);
188  j = serialize(*ptr_);
189  }
190 
191  void from_conf(const Conf& c) override {
192  assert(ptr_ != nullptr);
193  *ptr_ = deserialize(c);
194  }
195 
196  Json serialize(const Type& x) const { return x.native(); }
197 
198  Type deserialize(const Conf& c) const;
199 
200  void reset_ptr() override { ptr_ = nullptr; }
201 
202  private:
203  Type resolve_path(const Conf&, const Type&) const;
204 
205  private:
206  State req_state_{State::Any};
207  bool req_abs_{false};
208  bool resolve_{true};
209  bool normalize_{false};
210  bool interpolate_{false};
211  size_t min_length_{0};
212  size_t max_length_{std::numeric_limits<size_t>::max()};
213  std::string pattern_{};
214  Environment* env_{nullptr};
215  Type* ptr_{nullptr};
216 };
217 
218 inline Path make_schema(boost::filesystem::path* ptr, std::string&& desc) {
219  return Path(ptr, std::move(desc));
220 }
221 
222 } // namespace schema
223 } // namespace fable
224 
225 namespace nlohmann {
226 
227 template <>
228 struct adl_serializer<boost::filesystem::path> {
229  static void to_json(json& j, const boost::filesystem::path& p) { j = p.native(); }
230  static void from_json(const json& j, boost::filesystem::path& p) { p = j.get<std::string>(); }
231 };
232 
233 } // namespace nlohmann
234 
235 #endif // FABLE_SCHEMA_PATH_HPP_
path does not exist or is a directory
Definition: conf.hpp:74
State state() const
Definition: path.hpp:82
Path absolute() &&
Definition: path.hpp:109
Definition: conf.hpp:64
path exists and is a directory
State
Definition: path.hpp:65
path exists and is a file
Path state(State s) &&
Definition: path.hpp:87
Definition: path.hpp:58
nlohmann::json Json
Definition: json.hpp:62
void from_conf(const Conf &c) override
Definition: path.hpp:191
Definition: conf.hpp:70
Definition: confable.hpp:132
Definition: environment.hpp:37
void to_json(Json &j) const override
Definition: path.hpp:186
path exists in search path or locally and has executable permission
bool resolve() const
Definition: path.hpp:119
virtual Json to_json() const
Definition: interface.hpp:220
Json json_schema() const override
Definition: path.cpp:65
void reset_ptr() override
Definition: path.hpp:200
void validate(const Conf &c) const override
Definition: path.cpp:87
Definition: interface.hpp:354
Path resolve(bool value) &&
Definition: path.hpp:133