Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F34566969
db.rs
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
db.rs
View Options
// -------------------------------------------------------------
// Alkane :: Database
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Project: Nasqueron
// License: BSD-2-Clause
// -------------------------------------------------------------
use
std
::
fs
::
OpenOptions
;
use
std
::
io
::
Error
as
IOError
;
use
std
::
path
::
{
Path
,
PathBuf
};
use
log
::
warn
;
use
crate
::
config
::
AlkaneConfig
;
pub
struct
Database
{
root
:
String
,
}
impl
Database
{
pub
fn
new
<
S
>
(
root
:
S
)
->
Self
where
S
:
AsRef
<
str
>
,
{
Self
{
root
:
root
.
as_ref
().
to_string
(),
}
}
pub
fn
from_config
(
config
:
&
AlkaneConfig
)
->
Option
<
Self
>
{
config
.
get_root
(
"db"
).
map
(
Self
::
new
)
}
pub
fn
is_initialized
(
&
self
,
site_name
:
&
str
)
->
bool
{
self
.
get_initialized_path
(
site_name
).
exists
()
}
pub
fn
set_initialized
(
&
self
,
site_name
:
&
str
)
->
bool
{
let
path
=
self
.
get_initialized_path
(
site_name
);
if
!
path
.
exists
()
{
match
touch
(
&
path
)
{
Ok
(
_
)
=>
true
,
Err
(
error
)
=>
{
warn
!
(
"Can't mark site {} as initialized: {:?}"
,
site_name
,
error
);
false
}
}
}
else
{
true
}
}
fn
get_initialized_path
(
&
self
,
site_name
:
&
str
)
->
PathBuf
{
Path
::
new
(
&
self
.
root
).
join
(
"initialized"
).
join
(
site_name
)
}
}
/// Creates an empty file, similar to the touch command
/// Ignores existing files.
fn
touch
(
path
:
&
PathBuf
)
->
Result
<
(),
IOError
>
{
let
mut
options
=
OpenOptions
::
new
();
options
.
create
(
true
).
write
(
true
);
options
.
open
(
path
).
map
(
|
_
|
())
}
// -------------------------------------------------------------
// Tests
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#[cfg(test)]
mod
tests
{
use
std
::
fs
;
use
super
::
*
;
#[test]
pub
fn
test_touch
()
{
let
path
=
Path
::
new
(
"tmp-touch.empty"
);
assert
!
(
!
path
.
exists
(),
"Temporary file tmp-touch.empty shouldn't exist when test starts"
);
touch
(
&
path
.
to_path_buf
()).
expect
(
"File can't be created"
);
assert
!
(
path
.
exists
(),
"Function touch returned Ok but temporary file does NOT exist."
);
fs
::
remove_file
(
path
).
expect
(
"Can't remove file after having created it."
)
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Jul 4, 03:31 (5 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3874526
Default Alt Text
db.rs (2 KB)
Attached To
Mode
rALK Alkane
Attached
Detach File
Event Timeline
Log In to Comment