needed for fmt::join
[ghsmtp.git] / Domain.hpp
blob33afb3a87837a2dbec239e90f28ddc7e64f9b497
1 #ifndef DOMAIN_DOT_HPP
2 #define DOMAIN_DOT_HPP
4 #include "IP.hpp"
6 #include <compare>
7 #include <iostream>
8 #include <string>
9 #include <string_view>
11 #include "iequal.hpp"
13 // The 'domain' part of an email address: DNS domain, or IP address, or address
14 // literal, or empty.
16 class Domain {
17 public:
18 Domain() = default;
20 inline explicit Domain(std::string_view dom);
22 inline static bool
23 validate(std::string_view domain, std::string& msg, Domain& dom);
25 inline void clear();
27 inline bool empty() const;
29 inline bool operator==(Domain const& rhs) const;
30 inline auto operator<=>(Domain const& rhs) const;
32 inline bool is_address_literal() const;
33 inline bool is_unicode() const;
35 inline std::string const& ascii() const;
36 inline std::string const& utf8() const;
38 private:
39 bool set_(std::string_view dom, bool should_throw, std::string& msg);
41 std::string ascii_; // A-labels
42 std::string utf8_; // U-labels, or empty
44 bool is_address_literal_{false};
47 Domain::Domain(std::string_view dom)
49 std::string msg;
50 set_(dom, true /* throw */, msg);
53 bool Domain::validate(std::string_view domain, std::string& msg, Domain& dom)
55 return dom.set_(domain, false /* don't throw */, msg);
58 void Domain::clear()
60 ascii_.clear();
61 utf8_.clear();
62 is_address_literal_ = false;
65 bool Domain::empty() const { return ascii_.empty(); }
67 bool Domain::operator==(Domain const& rhs) const
69 return ascii_ == rhs.ascii_;
72 auto Domain::operator<=>(const Domain& rhs) const
74 return ascii_ <=> rhs.ascii_;
77 bool Domain::is_address_literal() const { return is_address_literal_; }
78 bool Domain::is_unicode() const
80 return (!utf8().empty()) && (utf8() != ascii());
83 std::string const& Domain::ascii() const { return ascii_; }
84 std::string const& Domain::utf8() const
86 return utf8_.empty() ? ascii_ : utf8_;
89 inline std::ostream& operator<<(std::ostream& os, Domain const& dom)
91 if (dom.is_unicode())
92 return os << '{' << dom.ascii() << ',' << dom.utf8() << '}';
93 return os << dom.ascii();
96 namespace domain {
97 bool is_fully_qualified(Domain const& dom, std::string& msg);
100 namespace std {
101 template <>
102 struct hash<Domain> {
103 std::size_t operator()(Domain const& k) const
105 return hash<std::string>()(k.ascii());
108 } // namespace std
110 #endif // DOMAIN_DOT_HPP