You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * meli - configuration module.
  3. *
  4. * Copyright 2017 Manos Pitsidianakis
  5. *
  6. * This file is part of meli.
  7. *
  8. * meli is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * meli is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with meli. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. use crate::backends::SpecialUsageMailbox;
  22. use serde::{Deserialize, Deserializer, Serialize, Serializer};
  23. use std::collections::HashMap;
  24. #[derive(Debug, Serialize, Default, Clone)]
  25. pub struct AccountSettings {
  26. pub name: String,
  27. pub root_mailbox: String,
  28. pub format: String,
  29. pub identity: String,
  30. pub read_only: bool,
  31. pub display_name: Option<String>,
  32. pub subscribed_mailboxes: Vec<String>,
  33. #[serde(default)]
  34. pub mailboxes: HashMap<String, MailboxConf>,
  35. #[serde(default)]
  36. pub manual_refresh: bool,
  37. #[serde(flatten)]
  38. pub extra: HashMap<String, String>,
  39. }
  40. impl AccountSettings {
  41. pub fn format(&self) -> &str {
  42. &self.format
  43. }
  44. pub fn name(&self) -> &str {
  45. &self.name
  46. }
  47. pub fn set_name(&mut self, s: String) {
  48. self.name = s;
  49. }
  50. pub fn root_mailbox(&self) -> &str {
  51. &self.root_mailbox
  52. }
  53. pub fn identity(&self) -> &str {
  54. &self.identity
  55. }
  56. pub fn read_only(&self) -> bool {
  57. self.read_only
  58. }
  59. pub fn display_name(&self) -> Option<&String> {
  60. self.display_name.as_ref()
  61. }
  62. pub fn subscribed_mailboxes(&self) -> &Vec<String> {
  63. &self.subscribed_mailboxes
  64. }
  65. #[cfg(feature = "vcard")]
  66. pub fn vcard_folder(&self) -> Option<&str> {
  67. self.extra.get("vcard_folder").map(String::as_str)
  68. }
  69. }
  70. #[serde(default)]
  71. #[derive(Debug, Clone, Serialize, Deserialize)]
  72. pub struct MailboxConf {
  73. pub alias: Option<String>,
  74. #[serde(default = "false_val")]
  75. pub autoload: bool,
  76. #[serde(default)]
  77. pub subscribe: ToggleFlag,
  78. #[serde(default)]
  79. pub ignore: ToggleFlag,
  80. #[serde(default = "none")]
  81. pub usage: Option<SpecialUsageMailbox>,
  82. #[serde(flatten)]
  83. pub extra: HashMap<String, String>,
  84. }
  85. impl Default for MailboxConf {
  86. fn default() -> Self {
  87. MailboxConf {
  88. alias: None,
  89. autoload: false,
  90. subscribe: ToggleFlag::Unset,
  91. ignore: ToggleFlag::Unset,
  92. usage: None,
  93. extra: HashMap::default(),
  94. }
  95. }
  96. }
  97. impl MailboxConf {
  98. pub fn alias(&self) -> Option<&str> {
  99. self.alias.as_ref().map(String::as_str)
  100. }
  101. }
  102. pub fn true_val() -> bool {
  103. true
  104. }
  105. pub fn false_val() -> bool {
  106. false
  107. }
  108. pub fn none<T>() -> Option<T> {
  109. None
  110. }
  111. #[derive(Copy, Debug, Clone, PartialEq)]
  112. pub enum ToggleFlag {
  113. Unset,
  114. InternalVal(bool),
  115. False,
  116. True,
  117. }
  118. impl From<bool> for ToggleFlag {
  119. fn from(val: bool) -> Self {
  120. if val {
  121. ToggleFlag::True
  122. } else {
  123. ToggleFlag::False
  124. }
  125. }
  126. }
  127. impl Default for ToggleFlag {
  128. fn default() -> Self {
  129. ToggleFlag::Unset
  130. }
  131. }
  132. impl ToggleFlag {
  133. pub fn is_unset(&self) -> bool {
  134. ToggleFlag::Unset == *self
  135. }
  136. pub fn is_internal(&self) -> bool {
  137. if let ToggleFlag::InternalVal(_) = *self {
  138. true
  139. } else {
  140. false
  141. }
  142. }
  143. pub fn is_false(&self) -> bool {
  144. ToggleFlag::False == *self || ToggleFlag::InternalVal(false) == *self
  145. }
  146. pub fn is_true(&self) -> bool {
  147. ToggleFlag::True == *self || ToggleFlag::InternalVal(true) == *self
  148. }
  149. }
  150. impl Serialize for ToggleFlag {
  151. fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
  152. where
  153. S: Serializer,
  154. {
  155. match self {
  156. ToggleFlag::Unset | ToggleFlag::InternalVal(_) => serializer.serialize_none(),
  157. ToggleFlag::False => serializer.serialize_bool(false),
  158. ToggleFlag::True => serializer.serialize_bool(true),
  159. }
  160. }
  161. }
  162. impl<'de> Deserialize<'de> for ToggleFlag {
  163. fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
  164. where
  165. D: Deserializer<'de>,
  166. {
  167. let s = <bool>::deserialize(deserializer);
  168. Ok(match s? {
  169. true => ToggleFlag::True,
  170. false => ToggleFlag::False,
  171. })
  172. }
  173. }