Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2888995
user.rs
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Advanced/Developer...
View Handle
View Hovercard
Size
1 KB
Referenced Files
None
Subscribers
None
user.rs
View Options
use
serde
::
Deserialize
;
use
serde
::
Serialize
;
use
rusqlite
::
Connection
;
use
rusqlite
::
Error
;
use
rusqlite
::
Result
;
use
ulid
::
Ulid
;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub
struct
User
{
pub
id
:
u32
,
pub
email
:
String
,
pub
real_name
:
String
,
pub
name
:
String
,
}
impl
User
{
// const TYPE:&str = "user";
pub
fn
lookup_by_eid
(
&
self
,
sqlite
:
&
Connection
)
->
Option
<
Ulid
>
{
let
result
:
Result
<
String
>
=
sqlite
.
query_row
(
"SELECT id FROM nodes WHERE type = 'user' AND eid = ?1"
,
[
self
.
id
],
|
row
|
row
.
get
(
0
),
);
match
result
{
Ok
(
s
)
=>
Some
(
Ulid
::
from_string
(
&
s
).
unwrap
()),
Err
(
error
)
=>
match
error
{
Error
::
QueryReturnedNoRows
=>
None
,
x
=>
panic
!
(
"Error while getting user {:?}"
,
x
),
},
}
}
pub
fn
insert
(
&
self
,
sqlite
:
&
Connection
)
->
Ulid
{
if
let
Some
(
ulid
)
=
self
.
lookup_by_eid
(
sqlite
)
{
return
ulid
;
}
let
ulid
=
Ulid
::
new
();
let
json
=
serde_json
::
to_string
(
&
self
).
unwrap
();
match
sqlite
.
execute
(
"INSERT INTO nodes (id, type, eid, raw)
VALUES (?1, 'user', ?2, ?3)"
,
(
ulid
.
to_string
(),
self
.
id
,
&
json
),
)
{
Ok
(
_
)
=>
(),
Err
(
err
)
=>
panic
!
(
"insert failed: {}"
,
err
),
}
sqlite
.
execute
(
"INSERT INTO users (id, email, real_name, name)
VALUES (?1, ?2, ?3, ?4)"
,
(
ulid
.
to_string
(),
&
self
.
email
,
&
self
.
real_name
,
&
self
.
name
),
)
.
unwrap
();
ulid
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Jan 19 2025, 10:57 (5 w, 23 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1117242
Default Alt Text
user.rs (1 KB)
Attached To
Mode
R4 Rust Conduit Client
Attached
Detach File
Event Timeline
Log In to Comment