diff --git a/.gitignore b/.gitignore index 6aa1064..f8d7c8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -/target/ **/*.rs.bk +.#* +/target/ Cargo.lock diff --git a/src/check.rs b/src/check.rs index f143c90..da27d62 100644 --- a/src/check.rs +++ b/src/check.rs @@ -41,6 +41,8 @@ pub struct Idle { pub struct Init { /// `path: $Path` pub path: Path, + /// `resources: $Resources` + pub resources: Resources, _extensible: (), } @@ -80,8 +82,7 @@ fn idle(idle: Option<::Idle>) -> Result { 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 { @@ -94,29 +95,28 @@ fn idle(idle: Option<::Idle>) -> Result { } fn init(init: Option<::Init>) -> Result { - 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) -> Result { 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." ); @@ -173,13 +173,11 @@ fn tasks(tasks: Option<::Tasks>) -> Result { 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::>()? } else { diff --git a/src/lib.rs b/src/lib.rs index 6a785ea..e5f0780 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,7 +44,7 @@ pub struct App { pub idle: Option, /// `init: { $Init }` pub init: Option, - /// `resources: $Resources` + /// `resources: $Statics` pub resources: Option, /// `tasks: { $Tasks }` pub tasks: Option, @@ -66,6 +66,8 @@ pub struct Idle { pub struct Init { /// `path: $Path` pub path: Option, + /// `resources: $Resources` + pub resources: Option, _extensible: (), } diff --git a/src/parse.rs b/src/parse.rs index 5de5d7e..6fbee0f 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -170,6 +170,7 @@ fn idle(tts: &mut Peekable>) -> Result { fn init(tts: &mut Peekable>) -> Result { ::parse::delimited(tts, DelimToken::Brace, |tts| { let mut path = None; + let mut resources = None; ::parse::fields(tts, |key, tts| { match key.as_ref() { @@ -178,6 +179,12 @@ fn init(tts: &mut Peekable>) -> Result { 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), } @@ -187,6 +194,7 @@ fn init(tts: &mut Peekable>) -> Result { Ok(Init { _extensible: (), path, + resources, }) }) }