tcvectordb 0.1.9

Rust SDK for Tencent Cloud VectorDB
Documentation
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Filter {
    condition: String,
}

impl Filter {
    pub fn new(condition: impl Into<String>) -> Self {
        Self {
            condition: condition.into(),
        }
    }

    pub fn and(mut self, condition: impl Into<String>) -> Self {
        self.condition = format!("({}) and ({})", self.condition, condition.into());
        self
    }

    pub fn or(mut self, condition: impl Into<String>) -> Self {
        self.condition = format!("({}) or ({})", self.condition, condition.into());
        self
    }

    pub fn and_not(mut self, condition: impl Into<String>) -> Self {
        self.condition = format!("({}) and not ({})", self.condition, condition.into());
        self
    }

    pub fn or_not(mut self, condition: impl Into<String>) -> Self {
        self.condition = format!("({}) or not ({})", self.condition, condition.into());
        self
    }

    pub fn include(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
        let key = key.into();
        let values: Vec<String> = values.into_iter().map(|v| {
            let val = v.into();
            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
                val
            } else {
                format!("\"{}\"", val)
            }
        }).collect();
        format!("{} include ({})", key, values.join(","))
    }

    pub fn exclude(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
        let key = key.into();
        let values: Vec<String> = values.into_iter().map(|v| {
            let val = v.into();
            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
                val
            } else {
                format!("\"{}\"", val)
            }
        }).collect();
        format!("{} exclude ({})", key, values.join(","))
    }

    pub fn include_all(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
        let key = key.into();
        let values: Vec<String> = values.into_iter().map(|v| {
            let val = v.into();
            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
                val
            } else {
                format!("\"{}\"", val)
            }
        }).collect();
        format!("{} include all ({})", key, values.join(","))
    }

    pub fn in_values(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
        let key = key.into();
        let values: Vec<String> = values.into_iter().map(|v| {
            let val = v.into();
            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
                val
            } else {
                format!("\"{}\"", val)
            }
        }).collect();
        format!("{} in ({})", key, values.join(","))
    }

    pub fn not_in(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
        let key = key.into();
        let values: Vec<String> = values.into_iter().map(|v| {
            let val = v.into();
            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
                val
            } else {
                format!("\"{}\"", val)
            }
        }).collect();
        format!("{} not in ({})", key, values.join(","))
    }

    pub fn condition(&self) -> &str {
        &self.condition
    }
}

impl From<&str> for Filter {
    fn from(condition: &str) -> Self {
        Self::new(condition)
    }
}

impl From<String> for Filter {
    fn from(condition: String) -> Self {
        Self::new(condition)
    }
}