sos_account/
types.rs

1//! Account management types.
2use sos_backend::AccessPoint;
3use sos_core::{commit::CommitState, events::Event, SecretId};
4use sos_login::PublicIdentity;
5use sos_vault::Summary;
6use std::sync::Arc;
7
8#[cfg(feature = "search")]
9use sos_search::SearchIndex;
10
11#[cfg(feature = "files")]
12use sos_external_files::FileMutationEvent;
13
14use serde::{Deserialize, Serialize};
15use tokio::sync::RwLock;
16
17#[cfg(feature = "clipboard")]
18use serde_json_path::JsonPath;
19
20/// Clipboard text formatter.
21#[cfg(feature = "clipboard")]
22#[typeshare::typeshare]
23#[derive(Debug, Serialize, Deserialize)]
24#[serde(
25    rename_all = "camelCase",
26    rename_all_fields = "camelCase",
27    tag = "kind",
28    content = "body"
29)]
30pub enum ClipboardTextFormat {
31    /// Parse as a RFC3339 date string and
32    /// format according to the given format string.
33    Date {
34        /// Format string.
35
36        // Typeshare doesn't respect rename_all_fields
37        #[serde(rename = "formatDescription")]
38        format_description: String,
39    },
40}
41
42/// Request a clipboard copy operation.
43#[cfg(feature = "clipboard")]
44#[typeshare::typeshare]
45#[derive(Default, Debug, Serialize, Deserialize)]
46#[serde(default)]
47pub struct ClipboardCopyRequest {
48    /// Target paths.
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub paths: Option<Vec<JsonPath>>,
51    /// Format option.
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub format: Option<ClipboardTextFormat>,
54}
55
56/// Result information for a change to an account.
57pub struct AccountChange<T> {
58    /// Event to be logged.
59    pub event: Event,
60    /// Result generated during a sync.
61    pub sync_result: T,
62}
63
64/// Result information for a created or updated secret.
65pub struct SecretChange<T> {
66    /// Secret identifier.
67    pub id: SecretId,
68    /// Event to be logged.
69    pub event: Event,
70    /// Commit state of the folder event log before
71    /// the secret was created (or updated).
72    pub commit_state: CommitState,
73    /// Folder containing the secret.
74    pub folder: Summary,
75    /// Result generated during a sync.
76    pub sync_result: T,
77    /// File mutation events.
78    #[cfg(feature = "files")]
79    pub file_events: Vec<FileMutationEvent>,
80}
81
82/// Result information for a bulk insert.
83pub struct SecretInsert<T> {
84    /// Created secrets.
85    pub results: Vec<SecretChange<T>>,
86    /// Result generated during a sync.
87    pub sync_result: T,
88}
89
90/// Result information for a secret move event.
91pub struct SecretMove<T> {
92    /// Secret identifier.
93    pub id: SecretId,
94    /// Event to be logged.
95    pub event: Event,
96    /// Result generated during a sync.
97    pub sync_result: T,
98    /// File mutation events.
99    #[cfg(feature = "files")]
100    pub file_events: Vec<FileMutationEvent>,
101}
102
103/// Result information for a deleted secret.
104pub struct SecretDelete<T> {
105    /// Event to be logged.
106    pub event: Event,
107    /// Commit state of the folder event log before
108    /// the secret was deleted.
109    pub commit_state: CommitState,
110    /// Folder the secret was deleted from.
111    pub folder: Summary,
112    /// Result generated during a sync.
113    pub sync_result: T,
114    /// File mutation events.
115    #[cfg(feature = "files")]
116    pub file_events: Vec<FileMutationEvent>,
117}
118
119/// Result information for folder creation.
120pub struct FolderCreate<T> {
121    /// Created folder.
122    pub folder: Summary,
123    /// Event to be logged.
124    pub event: Event,
125    /// Commit state of the new folder.
126    pub commit_state: CommitState,
127    /// Result generated during a sync.
128    pub sync_result: T,
129}
130
131/// Result information for changes to a folder's attributes.
132pub struct FolderChange<T> {
133    /// Event to be logged.
134    pub event: Event,
135    /// Commit state before the change.
136    pub commit_state: CommitState,
137    /// Result generated during a sync.
138    pub sync_result: T,
139}
140
141/// Result information for folder deletion.
142pub struct FolderDelete<T> {
143    /// Events to be logged.
144    pub events: Vec<Event>,
145    /// Commit state of the folder.
146    pub commit_state: CommitState,
147    /// Result generated during a sync.
148    pub sync_result: T,
149}
150
151/// Progress event when importing contacts.
152#[cfg(feature = "contacts")]
153pub enum ContactImportProgress {
154    /// Progress event when the number of contacts is known.
155    Ready {
156        /// Total number of contacts.
157        total: usize,
158    },
159    /// Progress event when a contact is being imported.
160    Item {
161        /// Label of the contact.
162        label: String,
163        /// Index of the contact.
164        index: usize,
165    },
166}
167
168/// Read-only view created from a specific event log commit.
169pub struct DetachedView {
170    pub(crate) keeper: AccessPoint,
171    #[cfg(feature = "search")]
172    pub(crate) index: Arc<RwLock<SearchIndex>>,
173}
174
175impl DetachedView {
176    /// Read-only access to the folder.
177    pub fn keeper(&self) -> &AccessPoint {
178        &self.keeper
179    }
180
181    /// Search index for the detached view.
182    #[cfg(feature = "search")]
183    pub fn index(&self) -> Arc<RwLock<SearchIndex>> {
184        Arc::clone(&self.index)
185    }
186}
187
188/// Data about an account.
189#[derive(Debug, Clone, Serialize, Deserialize)]
190pub struct AccountData {
191    /// Main account information.
192    #[serde(flatten)]
193    pub account: PublicIdentity,
194    /// AGE identity public recipient.
195    pub identity: String,
196    /// Account folders.
197    pub folders: Vec<Summary>,
198    /// Identifier of the device public key.
199    pub device_id: String,
200}