Skip to content

Commit 589d4b6

Browse files
committed
Provide library access to code generation
1 parent 77ce4d5 commit 589d4b6

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

src/lib.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,58 @@
429429
//!
430430
//! - `#[feature(untagged_unions)]` for overlapping/overloaded registers
431431
432-
// NOTE This file is for documentation only
432+
#![recursion_limit = "128"]
433+
434+
extern crate cast;
435+
extern crate either;
436+
#[macro_use]
437+
extern crate error_chain;
438+
extern crate inflections;
439+
#[macro_use]
440+
extern crate quote;
441+
extern crate svd_parser as svd;
442+
extern crate syn;
443+
444+
mod errors;
445+
mod generate;
446+
mod util;
447+
448+
pub use util::Target;
449+
450+
pub struct Generation {
451+
pub lib_rs: String,
452+
pub device_x: String,
453+
}
454+
455+
type Result<T> = std::result::Result<T, SvdError>;
456+
#[derive(Debug)]
457+
pub enum SvdError {
458+
Fmt,
459+
Render,
460+
}
461+
462+
/// Generates rust code for the specified svd content.
463+
pub fn generate(xml: &str, target: &Target, nightly: bool) -> Result<Generation> {
464+
use std::fmt::Write;
465+
466+
let device = svd::parse(xml);
467+
let mut device_x = String::new();
468+
let items = generate::device::render(&device, target, nightly, &mut device_x)
469+
.or(Err(SvdError::Render))?;
470+
471+
let mut lib_rs = String::new();
472+
writeln!(
473+
&mut lib_rs,
474+
"{}",
475+
quote! {
476+
#(#items)*
477+
}
478+
).or(Err(SvdError::Fmt))?;
479+
Ok(Generation {
480+
lib_rs: lib_rs,
481+
device_x: device_x,
482+
})
483+
}
433484

434485
/// Assigns a handler to an interrupt
435486
///

src/main.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,7 @@ use quote::Tokens;
2323
use clap::{App, Arg};
2424

2525
use errors::*;
26-
27-
#[derive(Clone, Copy, PartialEq)]
28-
pub enum Target {
29-
CortexM,
30-
Msp430,
31-
RISCV,
32-
None,
33-
}
34-
35-
impl Target {
36-
fn parse(s: &str) -> Result<Self> {
37-
Ok(match s {
38-
"cortex-m" => Target::CortexM,
39-
"msp430" => Target::Msp430,
40-
"riscv" => Target::RISCV,
41-
"none" => Target::None,
42-
_ => bail!("unknown target {}", s),
43-
})
44-
}
45-
}
26+
use util::Target;
4627

4728
fn run() -> Result<()> {
4829
use std::io::Read;

src/util.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ pub const BITS_PER_BYTE: u32 = 8;
1414
/// that are not valid in Rust ident
1515
const BLACKLIST_CHARS: &'static [char] = &['(', ')', '[', ']', '/', ' '];
1616

17+
#[derive(Clone, Copy, PartialEq)]
18+
pub enum Target {
19+
CortexM,
20+
Msp430,
21+
RISCV,
22+
None,
23+
}
24+
25+
impl Target {
26+
pub fn parse(s: &str) -> Result<Self> {
27+
Ok(match s {
28+
"cortex-m" => Target::CortexM,
29+
"msp430" => Target::Msp430,
30+
"riscv" => Target::RISCV,
31+
"none" => Target::None,
32+
_ => bail!("unknown target {}", s),
33+
})
34+
}
35+
}
36+
1737
pub trait ToSanitizedPascalCase {
1838
fn to_sanitized_pascal_case(&self) -> Cow<str>;
1939
}

0 commit comments

Comments
 (0)