text_variant.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/text/text_variant.h"
  8. namespace v::text {
  9. rpl::producer<QString> take_plain(
  10. data &&d,
  11. rpl::producer<QString> &&fallback) {
  12. using RplMarked = rpl::producer<TextWithEntities>;
  13. if (const auto empty = std::get_if<v::null_t>(&d)) {
  14. return std::move(fallback);
  15. } else if (const auto ptr = std::get_if<QString>(&d)) {
  16. return rpl::single(base::take(*ptr));
  17. } else if (const auto ptr = std::get_if<rpl::producer<QString>>(&d)) {
  18. return base::take(*ptr);
  19. } else if (const auto ptr = std::get_if<TextWithEntities>(&d)) {
  20. return rpl::single(base::take(*ptr).text);
  21. } else if (const auto ptr = std::get_if<RplMarked>(&d)) {
  22. return base::take(*ptr) | rpl::map([](const auto &marked) {
  23. return marked.text;
  24. });
  25. }
  26. Unexpected("Bad variant in take_plain.");
  27. }
  28. rpl::producer<TextWithEntities> take_marked(
  29. data &&d,
  30. rpl::producer<TextWithEntities> &&fallback) {
  31. using RplMarked = rpl::producer<TextWithEntities>;
  32. if (const auto empty = std::get_if<v::null_t>(&d)) {
  33. return std::move(fallback);
  34. } else if (const auto ptr = std::get_if<QString>(&d)) {
  35. return rpl::single(TextWithEntities{ base::take(*ptr) });
  36. } else if (const auto ptr = std::get_if<rpl::producer<QString>>(&d)) {
  37. return base::take(*ptr) | rpl::map(TextWithEntities::Simple);
  38. } else if (const auto ptr = std::get_if<TextWithEntities>(&d)) {
  39. return rpl::single(base::take(*ptr));
  40. } else if (const auto ptr = std::get_if<RplMarked>(&d)) {
  41. return base::take(*ptr);
  42. }
  43. Unexpected("Bad variant in take_marked.");
  44. }
  45. } // namespace v::text