surrealdb_core/expr/
access.rs1use std::fmt::{self, Display, Formatter};
2use std::ops::Deref;
3use std::str;
4
5use revision::revisioned;
6use serde::{Deserialize, Serialize};
7
8use crate::expr::Ident;
9use crate::expr::escape::EscapeIdent;
10use crate::expr::fmt::Fmt;
11use crate::val::strand::no_nul_bytes;
12use crate::val::{Duration, RecordId, RecordIdKey};
13
14#[revisioned(revision = 1)]
15#[derive(Debug, Serialize, Deserialize, Hash, Clone, Eq, PartialEq, PartialOrd)]
16pub struct AccessDuration {
20 pub grant: Option<Duration>,
23 pub token: Option<Duration>,
26 pub session: Option<Duration>,
28}
29
30impl Default for AccessDuration {
31 fn default() -> Self {
32 Self {
33 grant: Some(Duration::from_days(30).expect("30 days should fit in a duration")),
35 token: Some(Duration::from_hours(1).expect("1 hour should fit in a duration")),
37 session: None,
39 }
40 }
41}
42
43#[revisioned(revision = 1)]
44#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
45pub struct Accesses(pub Vec<Access>);
46
47impl From<Access> for Accesses {
48 fn from(v: Access) -> Self {
49 Accesses(vec![v])
50 }
51}
52
53impl Deref for Accesses {
54 type Target = Vec<Access>;
55 fn deref(&self) -> &Self::Target {
56 &self.0
57 }
58}
59
60impl Display for Accesses {
61 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
62 Display::fmt(&Fmt::comma_separated(&self.0), f)
63 }
64}
65
66#[revisioned(revision = 1)]
67#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
68#[serde(rename = "$surrealdb::private::sql::Access")]
69pub struct Access(#[serde(with = "no_nul_bytes")] pub String);
70
71impl From<String> for Access {
72 fn from(v: String) -> Self {
73 Self(v)
74 }
75}
76
77impl From<&str> for Access {
78 fn from(v: &str) -> Self {
79 Self::from(String::from(v))
80 }
81}
82
83impl From<Ident> for Access {
84 fn from(v: Ident) -> Self {
85 Self(v.into_string())
86 }
87}
88
89impl Deref for Access {
90 type Target = String;
91 fn deref(&self) -> &Self::Target {
92 &self.0
93 }
94}
95
96impl Access {
97 pub fn generate(&self) -> RecordId {
98 RecordId {
99 table: self.0.clone(),
100 key: RecordIdKey::rand(),
101 }
102 }
103}
104
105impl Display for Access {
106 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
107 EscapeIdent(&self.0).fmt(f)
108 }
109}