Skip to main content

surql_parser/upstream/sql/
file.rs

1use surrealdb_types::{SqlFormat, ToSql, write_sql};
2#[derive(Clone, Debug)]
3#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
4pub struct File {
5	pub bucket: String,
6	pub key: String,
7}
8impl File {
9	/// Check if this File belongs to a certain bucket type
10	pub fn is_bucket_type(&self, types: &[String]) -> bool {
11		types.is_empty() || types.iter().any(|buc| **buc == self.bucket)
12	}
13}
14impl ToSql for File {
15	fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
16		write_sql!(
17			f,
18			fmt,
19			"f\"{}:{}\"",
20			fmt_inner(&self.bucket, true),
21			fmt_inner(&self.key, false)
22		)
23	}
24}
25fn fmt_inner(v: &str, escape_slash: bool) -> String {
26	v.chars()
27		.flat_map(|c| {
28			if c.is_ascii_alphanumeric()
29				|| matches!(c, '-' | '_' | '.')
30				|| (!escape_slash && c == '/')
31			{
32				vec![c]
33			} else {
34				vec!['\\', c]
35			}
36		})
37		.collect::<String>()
38}
39impl From<File> for crate::compat::val::File {
40	fn from(v: File) -> Self {
41		Self {
42			bucket: v.bucket,
43			key: v.key,
44		}
45	}
46}
47impl From<crate::compat::val::File> for File {
48	fn from(v: crate::compat::val::File) -> Self {
49		Self {
50			bucket: v.bucket,
51			key: v.key,
52		}
53	}
54}