Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse a resources field in init #2

Merged
merged 1 commit into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
**/*.rs.bk
.#*
/target/
Cargo.lock
44 changes: 21 additions & 23 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Idle {
pub struct Init {
/// `path: $Path`
pub path: Path,
/// `resources: $Resources`
pub resources: Resources,
_extensible: (),
}

Expand Down Expand Up @@ -80,8 +82,7 @@ fn idle(idle: Option<::Idle>) -> Result<Idle> {

Idle {
_extensible: (),
path: ::check::path("idle", idle.path)
.chain_err(|| "checking `path`")?,
path: ::check::path("idle", idle.path).chain_err(|| "checking `path`")?,
resources: ::check::resources("resources", idle.resources)?,
}
} else {
Expand All @@ -94,29 +95,28 @@ fn idle(idle: Option<::Idle>) -> Result<Idle> {
}

fn init(init: Option<::Init>) -> Result<Init> {
Ok(if let Some(init) = init {
if let Some(path) = init.path {
Init {
_extensible: (),
path: ::check::path("init", Some(path))
.chain_err(|| "checking `path`")?,
}
} else {
bail!("empty `init` field. It should be removed.");
}
let init_is_some = init.is_some();
let (path, resources) = if let Some(init) = init {
(init.path, init.resources)
} else {
Init {
_extensible: (),
path: util::mk_path("init"),
}
(None, None)
};

if init_is_some && path.is_none() && resources.is_none() {
bail!("empty `init` field. It should be removed.");
}

Ok(Init {
_extensible: (),
resources: resources.unwrap_or(Resources::new()),
path: ::check::path("init", path).chain_err(|| "checking `path`")?,
})
}

fn path(default: &str, path: Option<Path>) -> Result<Path> {
Ok(if let Some(path) = path {
ensure!(
path.segments.len() != 1 ||
path.segments[0].ident.as_ref() != default,
path.segments.len() != 1 || path.segments[0].ident.as_ref() != default,
"this is the default value. It should be omitted."
);

Expand Down Expand Up @@ -173,13 +173,11 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> {
enabled: task.enabled,
path: task.path,
priority: task.priority,
resources: ::check::resources(
"resources",
task.resources,
)?,
resources: ::check::resources("resources", task.resources)?,
},
))
})().chain_err(|| format!("checking task `{}`", name_))
})()
.chain_err(|| format!("checking task `{}`", name_))
})
.collect::<Result<_>>()?
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct App {
pub idle: Option<Idle>,
/// `init: { $Init }`
pub init: Option<Init>,
/// `resources: $Resources`
/// `resources: $Statics`
pub resources: Option<Statics>,
/// `tasks: { $Tasks }`
pub tasks: Option<Tasks>,
Expand All @@ -66,6 +66,8 @@ pub struct Idle {
pub struct Init {
/// `path: $Path`
pub path: Option<Path>,
/// `resources: $Resources`
pub resources: Option<Resources>,
_extensible: (),
}

Expand Down
8 changes: 8 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn idle(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Idle> {
fn init(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Init> {
::parse::delimited(tts, DelimToken::Brace, |tts| {
let mut path = None;
let mut resources = None;

::parse::fields(tts, |key, tts| {
match key.as_ref() {
Expand All @@ -178,6 +179,12 @@ fn init(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Init> {

path = Some(::parse::path(tts)?);
}
"resources" => {
ensure!(resources.is_none(), "duplicated `resources` field");

resources = Some(::parse::resources(tts)
.chain_err(|| "parsing `resources`")?);
}
_ => bail!("unknown field: `{}`", key),
}

Expand All @@ -187,6 +194,7 @@ fn init(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Init> {
Ok(Init {
_extensible: (),
path,
resources,
})
})
}
Expand Down