1
//! A stored tag as the tag-edit form holds it, with the revision digesting
2
//! its editable fields.
3

            
4
use finance::tag::Tag;
5
use sqlx::types::Uuid;
6

            
7
use crate::error::ServerError;
8
use crate::logical::hex_digest;
9
use crate::user::User;
10

            
11
/// The error text of a tag edit whose expected revision no longer matches.
12
pub const STALE_TAG: &str = "the tag changed since it was loaded; reload it";
13

            
14
/// Digest of the tag's name, value and description.
15
#[must_use]
16
7111
pub fn tag_revision(tag: &Tag) -> String {
17
7111
    hex_digest(vec![format!(
18
7111
        "tag {:?}",
19
7111
        (&tag.tag_name, &tag.tag_value, &tag.description)
20
7111
    )])
21
7111
}
22

            
23
impl User {
24
    /// The tag, `None` when the user has no such tag.
25
    ///
26
    /// # Errors
27
    /// The connection or the query failing.
28
8168
    pub async fn find_tag(&self, id: Uuid) -> Result<Option<Tag>, ServerError> {
29
4247
        let mut conn = self.get_connection().await?;
30
4247
        sqlx::query_file_as!(Tag, "sql/select/tags/by_id.sql", id)
31
4247
            .fetch_optional(&mut *conn)
32
4247
            .await
33
            .map_err(|err| ServerError::DB(err.into()))
34
4247
    }
35
}