Skip to main content

lrcat/
keywords.rs

1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7use rusqlite::Row;
8
9use crate::catalog::CatalogVersion;
10use crate::fromdb::FromDb;
11use crate::lrobject::{LrId, LrObject};
12
13/// A Lightroom keyword.
14#[derive(Clone)]
15pub struct Keyword {
16    /// Local id
17    id: LrId,
18    /// Global UUID
19    uuid: String,
20    //  date_created: DateTime<Utc>,
21    /// the actual keyword
22    pub name: String,
23    /// The parent. For top-level the value is `Catalog::root_keyword_id`
24    pub parent: LrId,
25}
26
27impl LrObject for Keyword {
28    fn id(&self) -> LrId {
29        self.id
30    }
31    fn uuid(&self) -> &str {
32        &self.uuid
33    }
34}
35
36impl FromDb for Keyword {
37    fn read_from(_version: CatalogVersion, row: &Row) -> crate::Result<Self> {
38        let name = row.get(3).ok();
39        let parent = row.get(4).ok();
40        Ok(Keyword {
41            id: row.get(0)?,
42            uuid: row.get(1)?,
43            name: name.unwrap_or_default(),
44            parent: parent.unwrap_or(0),
45        })
46    }
47
48    fn read_db_tables(_version: CatalogVersion) -> &'static str {
49        "AgLibraryKeyword"
50    }
51
52    fn read_db_columns(_version: CatalogVersion) -> &'static str {
53        "id_local,id_global,cast(dateCreated as text),name,parent"
54    }
55}
56
57impl Keyword {
58    /// Initialize a new keyword.
59    pub fn new(id: LrId, parent: LrId, uuid: &str, name: &str) -> Keyword {
60        Keyword {
61            id,
62            parent,
63            uuid: String::from(uuid),
64            name: String::from(name),
65        }
66    }
67}