lrcat/
collections.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::{Connection, Row};
8
9use crate::catalog::CatalogVersion;
10use crate::content::Content;
11use crate::fromdb::FromDb;
12use crate::lrobject::LrId;
13
14/// A collection as defined in Lightroom
15pub struct Collection {
16    /// Local id of the collection
17    id: LrId,
18    /// Name of the collection (displayed in the UI)
19    pub name: String,
20    /// Parent of the `Collection`
21    pub parent: LrId,
22    /// is system only (seems to be the Quick Pick collection)
23    pub system_only: bool,
24    /// content definition of the collection
25    pub content: Option<Content>,
26}
27
28impl FromDb for Collection {
29    fn read_from(version: CatalogVersion, row: &Row) -> crate::Result<Self> {
30        match version {
31            CatalogVersion::Lr4 | CatalogVersion::Lr6 => Ok(Collection {
32                id: row.get(0)?,
33                name: row.get(2)?,
34                parent: row.get(3).unwrap_or(0),
35                system_only: !matches!(row.get::<usize, f64>(4)? as i64, 0),
36                content: None,
37            }),
38            CatalogVersion::Lr2 => {
39                let tag_type: Box<str> = row.get(3)?;
40                let name: String = row.get(1).unwrap_or_else(|_| {
41                    if tag_type.as_ref() == "AgQuickCollectionTagKind" {
42                        "Quick Collection"
43                    } else {
44                        ""
45                    }
46                    .to_owned()
47                });
48                match tag_type.as_ref() {
49                    "AgQuickCollectionTagKind" | "AgCollectionTagKind" => Ok(Collection {
50                        id: row.get(0)?,
51                        name,
52                        parent: row.get(2).unwrap_or(0),
53                        system_only: matches!(tag_type.as_ref(), "AgQuickCollectionTagKind"),
54                        content: None,
55                    }),
56                    _ => Err(crate::Error::Skip),
57                }
58            }
59            _ => Err(crate::Error::UnsupportedVersion),
60        }
61    }
62
63    fn read_db_tables(version: CatalogVersion) -> &'static str {
64        match version {
65            CatalogVersion::Lr4 | CatalogVersion::Lr6 => "AgLibraryCollection",
66            CatalogVersion::Lr2 => "AgLibraryTag",
67            _ => "",
68        }
69    }
70
71    fn read_db_columns(version: CatalogVersion) -> &'static str {
72        match version {
73            CatalogVersion::Lr4 | CatalogVersion::Lr6 => {
74                "id_local,genealogy,name,parent,systemOnly"
75            }
76            CatalogVersion::Lr2 => "id_local,name,parent,kindName",
77            _ => "",
78        }
79    }
80}
81
82impl Collection {
83    /// Return the local_id of the collection.
84    pub fn id(&self) -> LrId {
85        self.id
86    }
87
88    /// Read the `content` for this collection from the database.
89    pub fn read_content(&self, conn: &Connection) -> Content {
90        Content::from_db(conn, "AgLibraryCollectionContent", "collection", self.id)
91    }
92}