SlHelpers
Loading...
Searching...
No Matches
Person.h
1// SPDX-License-Identifier: GPL-2.0-only
2
3#pragma once
4
5#include <optional>
6#include <string>
7
8#include "../helpers/Enum.h"
9
10namespace SlKernCVS {
11
18enum class RoleType {
19 Author, First = Author,
20 SignedOffBy, FirstRole = SignedOffBy,
21 CoDevelopedBy,
22 SuggestedBy,
23 ReviewedBy,
24 AckedBy, LastRole = AckedBy,
25 TestedBy,
26 ReportedBy,
27 Maintainer,
28 Upstream, Last = Upstream,
29};
30
36class Role {
37private:
38 static constexpr const std::string_view roleNames[] = {
39 "Author",
40 "Signed-off-by",
41 "Co-developed-by",
42 "Suggested-by",
43 "Reviewed-by",
44 "Acked-by",
45 "Tested-by",
46 "Reported-by",
47 "Maintainer",
48 "Upstream"
49 };
50 static_assert(static_cast<size_t>(RoleType::Last) + 1 == std::size(roleNames));
51public:
52 Role() = delete;
54 Role(size_t index) : m_role(static_cast<RoleType>(index)) {}
56 Role(RoleType role) : m_role(role) {}
57
59 RoleType role() const { return m_role; }
60
62 static constexpr auto index(RoleType role) {
63 return static_cast<std::size_t>(role);
64 }
65
66 static constexpr auto toString(RoleType role) {
67 return roleNames[index(role)];
68 }
69
71 constexpr auto index() const { return index(m_role); }
73 constexpr auto toString() const { return toString(m_role); }
74private:
75 RoleType m_role;
76};
77
81class Person {
82public:
83 Person() = delete;
84
92 Person(Role r, std::string name, std::string email, unsigned count = 0) :
93 m_role(std::move(r)), m_name(std::move(name)), m_email(std::move(email)),
94 m_count(count) {}
95
97 const Role &role() const { return m_role; }
99 const std::string &name() const { return m_name; }
101 const std::string &email() const { return m_email; }
103 std::string userName() const { return m_email.substr(0, m_email.find("@")); }
104
109 auto count() const { return m_count; }
110
116 std::string pretty(bool includeName = true) const {
117 if (includeName && !name().empty())
118 return name() + " <" + email() + ">";
119 return email();
120 }
121
130 template <typename T>
131 requires requires (T t, std::string s) { { t(s) } -> std::convertible_to<std::string>; }
132 std::string pretty(const T &translate, bool includeName = true) const {
133 if (includeName && !name().empty())
134 return name() + " <" + translate(email()) + ">";
135 return translate(email());
136 }
137
142 void setEmail(const std::string &email) { m_email = email; }
143
154 static std::optional<Person> parsePerson(std::string_view src, Role role);
155
161 static std::optional<Person> parse(std::string_view src)
162 {
163 SlHelpers::EnumRange<RoleType> range{RoleType::FirstRole, RoleType::LastRole};
164 for (auto I: range) {
165 Role r(I);
166 if (src.starts_with(r.toString()))
167 if (auto p = parsePerson(src, std::move(r)))
168 return p;
169 }
170 return std::nullopt;
171 }
172private:
173 Role m_role;
174 std::string m_name;
175 std::string m_email;
176 unsigned m_count;
177
178};
179
180} // namespace
Helper class to iterate over enum values from Enum::First to Enum::Last.
Definition Enum.h:13
const std::string & email() const
Get e-mail of this Person.
Definition Person.h:101
const std::string & name() const
Get name of this Person.
Definition Person.h:99
static std::optional< Person > parse(std::string_view src)
Try to parse a line.
Definition Person.h:161
Person(Role r, std::string name, std::string email, unsigned count=0)
Create a Person with Role r, name, email and count of changes.
Definition Person.h:92
static std::optional< Person > parsePerson(std::string_view src, Role role)
Parse src into a Person.
std::string pretty(const T &translate, bool includeName=true) const
Pretty format as "username <e-mail>".
Definition Person.h:132
const Role & role() const
Get Role of this Person.
Definition Person.h:97
auto count() const
Get count of changes historically done by this Person.
Definition Person.h:109
std::string userName() const
Get user name (from e-mail).
Definition Person.h:103
void setEmail(const std::string &email)
Re-set e-mail to email.
Definition Person.h:142
std::string pretty(bool includeName=true) const
Pretty format as "username <e-mail>".
Definition Person.h:116
Role of a Person.
Definition Person.h:36
Role(RoleType role)
Construct new Role with role set.
Definition Person.h:56
RoleType role() const
Get the actual RoleType.
Definition Person.h:59
Role(size_t index)
Construct new Role with role set by the index.
Definition Person.h:54
constexpr auto toString() const
Convert Role to string.
Definition Person.h:73
constexpr auto index() const
Convert Role to number/index.
Definition Person.h:71
static constexpr auto index(RoleType role)
Convert Role to number/index.
Definition Person.h:62
static constexpr auto toString(RoleType role)
Convert Role to string.
Definition Person.h:66