2017-07-15 01:54:54 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2017-07-18 23:49:59 +02:00
|
|
|
use syn::{Ident, Path};
|
2017-07-15 03:47:06 +02:00
|
|
|
use syntax::check::{self, Idle, Init};
|
|
|
|
use syntax::{self, Idents, Statics};
|
2017-07-15 01:54:54 +02:00
|
|
|
|
2017-07-15 03:47:06 +02:00
|
|
|
use syntax::error::*;
|
2017-07-15 01:54:54 +02:00
|
|
|
|
|
|
|
pub struct App {
|
2017-07-18 23:49:59 +02:00
|
|
|
pub device: Path,
|
2017-07-15 01:54:54 +02:00
|
|
|
pub idle: Idle,
|
|
|
|
pub init: Init,
|
|
|
|
pub resources: Statics,
|
|
|
|
pub tasks: Tasks,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Tasks = HashMap<Ident, Task>;
|
|
|
|
|
|
|
|
pub struct Task {
|
|
|
|
pub enabled: Option<bool>,
|
|
|
|
pub priority: u8,
|
|
|
|
pub resources: Idents,
|
|
|
|
}
|
|
|
|
|
2017-07-15 03:47:06 +02:00
|
|
|
pub fn app(app: check::App) -> Result<App> {
|
2017-07-15 01:54:54 +02:00
|
|
|
let app = App {
|
|
|
|
device: app.device,
|
|
|
|
idle: app.idle,
|
|
|
|
init: app.init,
|
|
|
|
resources: app.resources,
|
2017-07-15 03:47:06 +02:00
|
|
|
tasks: app.tasks
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| {
|
|
|
|
let name = k.clone();
|
|
|
|
Ok((
|
|
|
|
k,
|
|
|
|
::check::task(v)
|
|
|
|
.chain_err(|| format!("checking task `{}`", name))?,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Result<_>>()
|
|
|
|
.chain_err(|| "checking `tasks`")?,
|
2017-07-15 01:54:54 +02:00
|
|
|
};
|
|
|
|
|
2017-07-15 03:47:06 +02:00
|
|
|
::check::resources(&app)
|
|
|
|
.chain_err(|| "checking `resources`")?;
|
2017-07-15 01:54:54 +02:00
|
|
|
|
|
|
|
Ok(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resources(app: &App) -> Result<()> {
|
|
|
|
for resource in app.resources.keys() {
|
|
|
|
if app.idle.resources.contains(resource) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if app.tasks
|
|
|
|
.values()
|
|
|
|
.any(|task| task.resources.contains(resource))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("resource `{}` is unused", resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-07-15 03:47:06 +02:00
|
|
|
fn task(task: syntax::check::Task) -> Result<Task> {
|
2017-07-15 01:54:54 +02:00
|
|
|
if let Some(priority) = task.priority {
|
|
|
|
Ok(Task {
|
|
|
|
enabled: task.enabled,
|
|
|
|
priority,
|
|
|
|
resources: task.resources,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
bail!("should contain a `priority` field")
|
|
|
|
}
|
2017-07-04 18:26:11 +02:00
|
|
|
}
|