Skip to content

Commit

Permalink
Button::PressingMilliseconds: using a template to properly handle sig…
Browse files Browse the repository at this point in the history
…ned (negative) values
  • Loading branch information
MacDada committed Apr 17, 2023
1 parent 61fe3dc commit 8a8ab7e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,12 @@ namespace DnWiFiDoorLock::Arduino::Esp8266::EspAsyncWebServer::Http {
return tl::unexpected{PSTR("No required data given")};
}

// todo: better conversion to int
// `toInt()` uses `atol()` and has no error handling at all…
// * https://en.cppreference.com/w/cpp/string/byte/strtol
// * https://www.cppstories.com/2018/12/fromchars/
const auto newPressingAngle = Servo::Angle::withDegrees(maybeNewPressingAngle->toInt());
const auto newNotPressingAngle = Servo::Angle::withDegrees(maybeNewNotPressingAngle->toInt());
// todo: what if a negative number was given?
const auto newMilliseconds = Button::PressingMilliseconds::create(maybeNewMilliseconds->toInt());

if (!newPressingAngle) {
Expand Down
5 changes: 3 additions & 2 deletions lib/DnWiFiDoorLock/DnWiFiDoorLock/Arduino/Servo/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ namespace DnWiFiDoorLock::Arduino::Servo {
public:
class PressingMilliseconds final {
public:
template<typename T>
static
auto create(
uint32_t value
T value
) -> tl::expected<PressingMilliseconds, String> {
if (value == 0) {
if (value <= 0) {
return tl::unexpected{PSTR("Positive integer required")};
}

Expand Down

0 comments on commit 8a8ab7e

Please sign in to comment.