Skip to content

Commit 1570f53

Browse files
authored
Merge pull request #2 from japaric/init-resources
parse a `resources` field in `init`
2 parents 5d48b8b + 9e5afa5 commit 1570f53

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/target/
21
**/*.rs.bk
2+
.#*
3+
/target/
34
Cargo.lock

src/check.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub struct Idle {
4141
pub struct Init {
4242
/// `path: $Path`
4343
pub path: Path,
44+
/// `resources: $Resources`
45+
pub resources: Resources,
4446
_extensible: (),
4547
}
4648

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

8183
Idle {
8284
_extensible: (),
83-
path: ::check::path("idle", idle.path)
84-
.chain_err(|| "checking `path`")?,
85+
path: ::check::path("idle", idle.path).chain_err(|| "checking `path`")?,
8586
resources: ::check::resources("resources", idle.resources)?,
8687
}
8788
} else {
@@ -94,29 +95,28 @@ fn idle(idle: Option<::Idle>) -> Result<Idle> {
9495
}
9596

9697
fn init(init: Option<::Init>) -> Result<Init> {
97-
Ok(if let Some(init) = init {
98-
if let Some(path) = init.path {
99-
Init {
100-
_extensible: (),
101-
path: ::check::path("init", Some(path))
102-
.chain_err(|| "checking `path`")?,
103-
}
104-
} else {
105-
bail!("empty `init` field. It should be removed.");
106-
}
98+
let init_is_some = init.is_some();
99+
let (path, resources) = if let Some(init) = init {
100+
(init.path, init.resources)
107101
} else {
108-
Init {
109-
_extensible: (),
110-
path: util::mk_path("init"),
111-
}
102+
(None, None)
103+
};
104+
105+
if init_is_some && path.is_none() && resources.is_none() {
106+
bail!("empty `init` field. It should be removed.");
107+
}
108+
109+
Ok(Init {
110+
_extensible: (),
111+
resources: resources.unwrap_or(Resources::new()),
112+
path: ::check::path("init", path).chain_err(|| "checking `path`")?,
112113
})
113114
}
114115

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

@@ -173,13 +173,11 @@ fn tasks(tasks: Option<::Tasks>) -> Result<Tasks> {
173173
enabled: task.enabled,
174174
path: task.path,
175175
priority: task.priority,
176-
resources: ::check::resources(
177-
"resources",
178-
task.resources,
179-
)?,
176+
resources: ::check::resources("resources", task.resources)?,
180177
},
181178
))
182-
})().chain_err(|| format!("checking task `{}`", name_))
179+
})()
180+
.chain_err(|| format!("checking task `{}`", name_))
183181
})
184182
.collect::<Result<_>>()?
185183
} else {

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct App {
4444
pub idle: Option<Idle>,
4545
/// `init: { $Init }`
4646
pub init: Option<Init>,
47-
/// `resources: $Resources`
47+
/// `resources: $Statics`
4848
pub resources: Option<Statics>,
4949
/// `tasks: { $Tasks }`
5050
pub tasks: Option<Tasks>,
@@ -66,6 +66,8 @@ pub struct Idle {
6666
pub struct Init {
6767
/// `path: $Path`
6868
pub path: Option<Path>,
69+
/// `resources: $Resources`
70+
pub resources: Option<Resources>,
6971
_extensible: (),
7072
}
7173

src/parse.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ fn idle(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Idle> {
170170
fn init(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Init> {
171171
::parse::delimited(tts, DelimToken::Brace, |tts| {
172172
let mut path = None;
173+
let mut resources = None;
173174

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

179180
path = Some(::parse::path(tts)?);
180181
}
182+
"resources" => {
183+
ensure!(resources.is_none(), "duplicated `resources` field");
184+
185+
resources = Some(::parse::resources(tts)
186+
.chain_err(|| "parsing `resources`")?);
187+
}
181188
_ => bail!("unknown field: `{}`", key),
182189
}
183190

@@ -187,6 +194,7 @@ fn init(tts: &mut Peekable<Iter<TokenTree>>) -> Result<Init> {
187194
Ok(Init {
188195
_extensible: (),
189196
path,
197+
resources,
190198
})
191199
})
192200
}

0 commit comments

Comments
 (0)