1use std::fmt;
8
9use rusqlite::Connection;
10
11use crate::lron;
12
13#[derive(Clone, Copy, Debug, PartialEq)]
14pub enum SortDirection {
16 Ascending,
18 Descending,
20 Unknown,
22}
23
24#[derive(Default, Clone)]
26pub struct Content {
27 pub filter: Option<String>,
29 pub sort_type: Option<String>,
31 pub sort_direction: Option<SortDirection>,
33 pub smart_collection: Option<lron::Object>,
35}
36
37impl fmt::Debug for Content {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 let mut empty: bool = true;
40 if let Some(ref filter) = self.filter {
41 write!(f, "filter: {filter:?}")?;
42 empty = false;
43 }
44 if let Some(ref sort_type) = self.sort_type {
45 if !empty {
46 write!(f, ", ")?;
47 }
48 write!(f, "sort: {sort_type:?}")?;
49 empty = false;
50 }
51 if let Some(ref direction) = self.sort_direction {
52 if !empty {
53 write!(f, ", ")?;
54 }
55 write!(f, "direction: {direction:?}")?;
56 empty = false;
57 }
58 if let Some(ref smart_coll) = self.smart_collection {
59 if !empty {
60 write!(f, ", ")?;
61 }
62 write!(f, "smart_collection: {smart_coll:?}")?;
63 }
64 Ok(())
65 }
66}
67
68impl Content {
69 pub fn from_db(
70 conn: &Connection,
71 table: &str,
72 container_col: &str,
73 container_id: i64,
74 ) -> Content {
75 let mut content = Content::default();
76
77 let query = format!("SELECT content, owningModule from {table} where {container_col}=?1",);
78 if let Ok(mut stmt) = conn.prepare(&query) {
79 let mut rows = stmt.query([&container_id]).unwrap();
80 while let Ok(Some(row)) = rows.next() {
81 let _ = row.get(1).map(|owning_module: String| {
84 let value = row.get(0);
85 match owning_module.as_str() {
86 "com.adobe.ag.library.filter" => content.filter = value.ok(),
87 "com.adobe.ag.library.sortType" => content.sort_type = value.ok(),
88 "com.adobe.ag.library.sortDirection" => {
89 content.sort_direction = if let Ok(sd) = value {
90 match sd.as_str() {
91 "ascending" => Some(SortDirection::Ascending),
92 "descending" => Some(SortDirection::Descending),
93 _ => Some(SortDirection::Unknown),
94 }
95 } else {
96 None
97 }
98 }
99 "ag.library.smart_collection" => {
100 if let Ok(ref sc) = value {
101 content.smart_collection = lron::Object::from_string(sc).ok();
102 }
103 }
104 _ => (),
105 };
106 });
107 }
108 }
109 content
110 }
111}