Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
static_contact
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Huss
static_contact
Commits
ee69d4a1
Commit
ee69d4a1
authored
Apr 3, 2020
by
David Huss
Browse files
Options
Downloads
Patches
Plain Diff
Make CONFIG_PATH lazy static
parent
d0330c45
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/config.rs
+14
-11
14 additions, 11 deletions
src/config.rs
with
14 additions
and
11 deletions
src/config.rs
+
14
−
11
View file @
ee69d4a1
...
@@ -9,9 +9,12 @@
...
@@ -9,9 +9,12 @@
use
std
::
path
::
PathBuf
;
use
std
::
path
::
PathBuf
;
use
serde
::
Deserialize
;
use
serde
::
Deserialize
;
use
std
::
fs
;
use
std
::
fs
;
use
lazy_static
::
lazy_static
;
/// Constant for Configuration
pub
const
CONFIG_PATH
:
PathBuf
=
PathBuf
::
from
(
"/etc/static_contact/config.toml"
);
lazy_static!
{
static
ref
CONFIG_PATH
:
PathBuf
=
PathBuf
::
from
(
"/etc/static_contact/config.toml"
);
}
/// Stores server and endpoints configuration, read from `config.toml`
/// Stores server and endpoints configuration, read from `config.toml`
#[derive(Debug,
Deserialize,
Clone,
PartialEq)]
#[derive(Debug,
Deserialize,
Clone,
PartialEq)]
...
@@ -57,36 +60,36 @@ impl Config{
...
@@ -57,36 +60,36 @@ impl Config{
/// let config = Config::new();
/// let config = Config::new();
/// ```
/// ```
pub
fn
new
()
->
Self
{
pub
fn
new
()
->
Self
{
let
contents
=
match
fs
::
read_to_string
(
&
CONFIG_PATH
){
let
contents
=
match
fs
::
read_to_string
(
CONFIG_PATH
.to_path_buf
()
){
Ok
(
c
)
=>
c
,
Ok
(
c
)
=>
c
,
Err
(
e
)
=>
{
Err
(
e
)
=>
{
match
e
.kind
()
{
match
e
.kind
()
{
std
::
io
::
ErrorKind
::
PermissionDenied
=>
{
std
::
io
::
ErrorKind
::
PermissionDenied
=>
{
match
fs
::
metadata
(
&
CONFIG_PATH
)
{
match
fs
::
metadata
(
&
CONFIG_PATH
.to_path_buf
()
)
{
Ok
(
meta
)
=>
{
Ok
(
meta
)
=>
{
if
meta
.is_dir
()
{
if
meta
.is_dir
()
{
eprintln!
(
"Error: cannot read the config at {:?} it seems to be a dir not a file..."
,
CONFIG_PATH
);
eprintln!
(
"Error: cannot read the config at {:?} it seems to be a dir not a file..."
,
CONFIG_PATH
.to_path_buf
()
);
}
else
{
}
else
{
let
permissions
=
meta
.permissions
();
let
permissions
=
meta
.permissions
();
if
cfg!
(
unix
)
{
if
cfg!
(
unix
)
{
use
std
::
os
::
unix
::
fs
::
PermissionsExt
;
use
std
::
os
::
unix
::
fs
::
PermissionsExt
;
eprintln!
(
"Error: insufficient permissions to read the config at {:?} ({:o})"
,
CONFIG_PATH
,
permissions
.mode
());
eprintln!
(
"Error: insufficient permissions to read the config at {:?} ({:o})"
,
CONFIG_PATH
.to_path_buf
()
,
permissions
.mode
());
}
else
{
}
else
{
eprintln!
(
"Error: insufficient permissions to read the config at {:?} (Readonly: {})"
,
CONFIG_PATH
,
permissions
.readonly
());
eprintln!
(
"Error: insufficient permissions to read the config at {:?} (Readonly: {})"
,
CONFIG_PATH
.to_path_buf
()
,
permissions
.readonly
());
}
}
}
}
},
},
Err
(
_e
)
=>
{
Err
(
_e
)
=>
{
eprintln!
(
"Error: insufficient permissions to read the config at {:?}"
,
CONFIG_PATH
);
eprintln!
(
"Error: insufficient permissions to read the config at {:?}"
,
CONFIG_PATH
.to_path_buf
()
);
}
}
}
}
},
},
std
::
io
::
ErrorKind
::
NotFound
=>
{
std
::
io
::
ErrorKind
::
NotFound
=>
{
eprintln!
(
"Error: No config.toml found at {:?}"
,
CONFIG_PATH
);
eprintln!
(
"Error: No config.toml found at {:?}"
,
CONFIG_PATH
.to_path_buf
()
);
}
}
_
=>
{
_
=>
{
eprintln!
(
"Error while attempting to read config from path {:?}: {:?}"
,
CONFIG_PATH
,
e
);
eprintln!
(
"Error while attempting to read config from path {:?}: {:?}"
,
CONFIG_PATH
.to_path_buf
()
,
e
);
}
}
}
}
std
::
process
::
exit
(
0
);
std
::
process
::
exit
(
0
);
...
@@ -103,6 +106,6 @@ impl Config{
...
@@ -103,6 +106,6 @@ impl Config{
/// println!("{:?}", path);
/// println!("{:?}", path);
/// ```
/// ```
pub
fn
path
(
&
self
)
->
PathBuf
{
pub
fn
path
(
&
self
)
->
PathBuf
{
CONFIG_PATH
CONFIG_PATH
.to_path_buf
()
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment