aboutsummaryrefslogtreecommitdiff
path: root/database/src/query_ser.rs
diff options
context:
space:
mode:
Diffstat (limited to 'database/src/query_ser.rs')
-rw-r--r--database/src/query_ser.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/database/src/query_ser.rs b/database/src/query_ser.rs
new file mode 100644
index 0000000..7698fdc
--- /dev/null
+++ b/database/src/query_ser.rs
@@ -0,0 +1,94 @@
+/*
+ This file is part of jellything (https://codeberg.org/metamuffin/jellything)
+ which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
+ Copyright (C) 2026 metamuffin <metamuffin.org>
+*/
+
+use crate::{DEBUG_TAGREG, Filter, Query, Sort};
+use jellyobject::{Path, Registry, Tag};
+use std::any::TypeId;
+
+impl Query {
+ pub fn show_debug(&self) -> String {
+ if let Some(r) = DEBUG_TAGREG.get() {
+ self.show(r)
+ } else {
+ "[debug tag registry disabled]".to_string()
+ }
+ }
+ pub fn show(&self, reg: &Registry) -> String {
+ let mut o = String::new();
+ if !matches!(self.filter, Filter::True) {
+ o += &format!("FILTER {}", self.filter.show(reg))
+ }
+ if !matches!(self.sort, Sort::None) {
+ o += &format!("SORT {}", self.sort.show(reg))
+ }
+ o
+ }
+}
+impl Filter {
+ pub fn show(&self, reg: &Registry) -> String {
+ match self {
+ Filter::True => "TRUE".to_string(),
+ Filter::All(filters) => format!(
+ "({})",
+ filters
+ .iter()
+ .map(|f| f.show(reg))
+ .collect::<Vec<_>>()
+ .join(" AND ")
+ ),
+ Filter::Any(filters) => format!(
+ "({})",
+ filters
+ .iter()
+ .map(|f| f.show(reg))
+ .collect::<Vec<_>>()
+ .join(" OR ")
+ ),
+ Filter::Match(path, value) => {
+ format!(
+ "{} = {}",
+ show_path(reg, path),
+ show_value(reg, *path.0.last().unwrap(), value)
+ )
+ }
+ Filter::Has(path) => show_path(reg, path),
+ }
+ }
+}
+impl Sort {
+ pub fn show(&self, reg: &Registry) -> String {
+ match self {
+ Sort::None => "NONE".to_string(),
+ Sort::Value(vs) => {
+ todo!()
+ }
+ Sort::TextSearch(path, value) => {
+ format!("TEXT SEARCH {} = {value:?}", show_path(reg, path),)
+ }
+ }
+ }
+}
+
+fn show_path(reg: &Registry, path: &Path) -> String {
+ path.0
+ .iter()
+ .map(|s| reg.name(*s))
+ .collect::<Vec<_>>()
+ .join(".")
+}
+fn show_value(reg: &Registry, tag: Tag, value: &[u8]) -> String {
+ if let Some(info) = reg.info(tag)
+ && let Some(ty) = info.r#type
+ {
+ if ty == TypeId::of::<&str>() {
+ format!("{:?}", str::from_utf8(value).unwrap())
+ } else {
+ format!("{value:?}")
+ }
+ } else {
+ format!("{value:?}")
+ }
+}