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