Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-swerve
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Louis
rust-swerve
Commits
22602d57
Unverified
Commit
22602d57
authored
7 years ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Add relevant command line options as config values, load config from file with fallback
parent
bbc563d6
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/cli/cli.rs
+15
-0
15 additions, 0 deletions
src/cli/cli.rs
src/cli/config_file.rs
+100
-7
100 additions, 7 deletions
src/cli/config_file.rs
with
115 additions
and
7 deletions
src/cli/cli.rs
+
15
−
0
View file @
22602d57
pub
use
std
::
path
::
PathBuf
;
pub
use
std
::
env
::
current_dir
;
pub
const
USAGE
:
&
'static
str
=
"
pub
const
USAGE
:
&
'static
str
=
"
Static file swerver and api mocker for local development.
Static file swerver and api mocker for local development.
For full documentation, visit https://swerve.louiscap.io.
For full documentation, visit https://swerve.louiscap.io.
...
@@ -39,4 +42,16 @@ pub struct Args {
...
@@ -39,4 +42,16 @@ pub struct Args {
pub
flag_upload
:
bool
,
pub
flag_upload
:
bool
,
pub
flag_upload_path
:
Option
<
String
>
,
pub
flag_upload_path
:
Option
<
String
>
,
pub
flag_license
:
bool
,
pub
flag_license
:
bool
,
}
impl
Args
{
pub
fn
get_dir
(
&
self
)
->
PathBuf
{
let
dir_flag
=
self
.flag_dir
.clone
();
PathBuf
::
from
(
dir_flag
.unwrap_or
(
current_dir
()
.unwrap_or
(
PathBuf
::
from
(
""
)
)
.to_string_lossy
()
.into_owned
())
)
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/cli/config_file.rs
+
100
−
7
View file @
22602d57
...
@@ -3,26 +3,119 @@ use std::convert::AsRef;
...
@@ -3,26 +3,119 @@ use std::convert::AsRef;
use
std
::
io
::
prelude
::
*
;
use
std
::
io
::
prelude
::
*
;
use
std
::
io
;
use
std
::
io
;
use
std
::
fs
::
File
;
use
std
::
fs
::
File
;
use
std
::
io
::
BufReader
;
use
std
::
default
::
Default
;
use
serde
::{
Deserialize
,
Deserializer
,
de
::{
self
,
Error
}};
use
std
::
fmt
;
use
serde_yaml
as
yaml
;
#[derive(Debug,
Copy,
Clone)]
pub
enum
HandlerMethod
{
pub
enum
HandlerMethod
{
Log
,
Log
,
File
,
File
,
}
}
struct
HandlerMethodVisitor
;
impl
<
'de
>
de
::
Visitor
<
'de
>
for
HandlerMethodVisitor
{
type
Value
=
HandlerMethod
;
fn
expecting
(
&
self
,
formatter
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
write!
(
formatter
,
"a string with value 'Log' or 'File'"
)
}
fn
visit_str
<
E
>
(
self
,
v
:
&
str
)
->
Result
<
Self
::
Value
,
E
>
where
E
:
de
::
Error
,
{
match
v
{
"Log"
=>
Ok
(
HandlerMethod
::
Log
),
"File"
=>
Ok
(
HandlerMethod
::
File
),
_
=>
Err
(
de
::
Error
::
unknown_variant
(
v
,
&
[
"Log"
,
"File"
])),
}
}
}
impl
<
'de
>
Deserialize
<
'de
>
for
HandlerMethod
{
fn
deserialize
<
D
>
(
deserializer
:
D
)
->
Result
<
Self
,
D
::
Error
>
where
D
:
Deserializer
<
'de
>
{
deserializer
.deserialize_str
(
HandlerMethodVisitor
)
}
}
impl
Default
for
HandlerMethod
{
fn
default
()
->
Self
{
HandlerMethod
::
Log
}
}
#[derive(Deserialize,
Debug,
Clone)]
pub
struct
SwerveConfig
{
pub
struct
SwerveConfig
{
#[serde(default)]
pub
field_handling
:
HandlerMethod
,
pub
field_handling
:
HandlerMethod
,
#[serde(default)]
pub
file_handling
:
HandlerMethod
,
pub
file_handling
:
HandlerMethod
,
#[serde(default)]
pub
server
:
ServerOptions
,
}
}
impl
SwerveConfig
{
#[derive(Deserialize,
Debug,
Clone)]
pub
fn
from_file
<
P
>
(
path
:
P
)
->
io
::
Result
<
SwerveConfig
>
where
P
:
AsRef
<
Path
>
{
pub
struct
ServerOptions
{
// let mut file = File::open(path)?;
#[serde(default=
"get_default_port"
)]
// let mut buffer = String::new();
pub
port
:
u16
,
// file.read_to_string(buffer)?;
#[serde(default=
"get_default_threads"
)]
pub
threads
:
u16
,
#[serde(default=
"get_default_address"
)]
pub
address
:
String
,
#[serde(default=
"get_default_quiet_attr"
)]
pub
quiet
:
bool
,
#[serde(default=
"get_default_index_attr"
)]
pub
no_index
:
bool
,
}
fn
get_default_port
()
->
u16
{
8200
}
fn
get_default_threads
()
->
u16
{
32
}
fn
get_default_address
()
->
String
{
String
::
from
(
"localhost"
)
}
fn
get_default_quiet_attr
()
->
bool
{
false
}
fn
get_default_index_attr
()
->
bool
{
false
}
Ok
(
SwerveConfig
{
impl
Default
for
SwerveConfig
{
fn
default
()
->
Self
{
SwerveConfig
{
field_handling
:
HandlerMethod
::
Log
,
field_handling
:
HandlerMethod
::
Log
,
file_handling
:
HandlerMethod
::
Log
,
file_handling
:
HandlerMethod
::
Log
,
})
server
:
ServerOptions
::
default
(),
}
}
}
impl
Default
for
ServerOptions
{
fn
default
()
->
Self
{
ServerOptions
{
port
:
get_default_port
(),
threads
:
get_default_threads
(),
address
:
get_default_address
(),
quiet
:
get_default_quiet_attr
(),
no_index
:
get_default_index_attr
(),
}
}
}
impl
SwerveConfig
{
pub
fn
from_file
<
P
>
(
path
:
P
)
->
io
::
Result
<
SwerveConfig
>
where
P
:
AsRef
<
Path
>
{
let
mut
buffer
=
String
::
new
();
{
match
File
::
open
(
path
)
{
Ok
(
mut
file
)
=>
file
.read_to_string
(
&
mut
buffer
)
?
,
Err
(
e
)
=>
{
if
e
.kind
()
==
io
::
ErrorKind
::
NotFound
{
return
Ok
(
SwerveConfig
::
default
());
}
else
{
return
Err
(
e
);
}
}
};
}
match
yaml
::
from_str
(
&
buffer
)
{
Ok
(
conf
)
=>
Ok
(
conf
),
Err
(
er
)
=>
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidData
,
format!
(
"{}"
,
er
))),
}
}
}
}
}
\ 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