Skip to main content

server/
tag_state.rs

1//! A stored tag as the tag-edit form holds it, with the revision digesting
2//! its editable fields.
3
4use finance::tag::Tag;
5use sqlx::types::Uuid;
6
7use crate::error::ServerError;
8use crate::logical::hex_digest;
9use crate::user::User;
10
11/// The error text of a tag edit whose expected revision no longer matches.
12pub 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]
16pub fn tag_revision(tag: &Tag) -> String {
17    hex_digest(vec![format!(
18        "tag {:?}",
19        (&tag.tag_name, &tag.tag_value, &tag.description)
20    )])
21}
22
23impl User {
24    /// The tag, `None` when the user has no such tag.
25    ///
26    /// # Errors
27    /// The connection or the query failing.
28    pub async fn find_tag(&self, id: Uuid) -> Result<Option<Tag>, ServerError> {
29        let mut conn = self.get_connection().await?;
30        sqlx::query_file_as!(Tag, "sql/select/tags/by_id.sql", id)
31            .fetch_optional(&mut *conn)
32            .await
33            .map_err(|err| ServerError::DB(err.into()))
34    }
35}