Skip to content

Commit 350edd5

Browse files
Emilgardisburrbull
authored andcommitted
move load_from to lib.rs
this also makes `util` and `generate` visible to library consumers.
1 parent 2ccab6f commit 350edd5

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

src/lib.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@
502502
use quote::quote;
503503
use svd_parser::svd;
504504

505-
mod generate;
506-
mod util;
505+
pub mod generate;
506+
pub mod util;
507507

508508
pub use crate::util::{Config, Target};
509509

@@ -519,7 +519,8 @@ pub struct DeviceSpecific {
519519
pub build_rs: String,
520520
}
521521

522-
use anyhow::Result;
522+
use anyhow::{Context, Result};
523+
523524
#[derive(Debug, thiserror::Error)]
524525
pub enum SvdError {
525526
#[error("Cannot format crate")]
@@ -532,7 +533,7 @@ pub enum SvdError {
532533
pub fn generate(input: &str, config: &Config) -> Result<Generation> {
533534
use std::fmt::Write;
534535

535-
let device = crate::util::load_from(input, config)?;
536+
let device = load_from(input, config)?;
536537
let mut device_x = String::new();
537538
let items =
538539
generate::device::render(&device, config, &mut device_x).or(Err(SvdError::Render))?;
@@ -562,6 +563,30 @@ pub fn generate(input: &str, config: &Config) -> Result<Generation> {
562563
})
563564
}
564565

566+
/// Load a [Device] from a string slice with given [config](crate::util::Config).
567+
pub fn load_from(input: &str, config: &crate::util::Config) -> Result<svd::Device> {
568+
use self::util::SourceType;
569+
use svd_parser::ValidateLevel;
570+
571+
Ok(match config.source_type {
572+
SourceType::Xml => {
573+
let mut parser_config = svd_parser::Config::default();
574+
parser_config.validate_level = if config.strict {
575+
ValidateLevel::Strict
576+
} else {
577+
ValidateLevel::Weak
578+
};
579+
580+
svd_parser::parse_with_config(input, &parser_config)
581+
.with_context(|| "Error parsing SVD XML file".to_string())?
582+
}
583+
SourceType::Yaml => serde_yaml::from_str(input)
584+
.with_context(|| "Error parsing SVD YAML file".to_string())?,
585+
SourceType::Json => serde_json::from_str(input)
586+
.with_context(|| "Error parsing SVD JSON file".to_string())?,
587+
})
588+
}
589+
565590
/// Assigns a handler to an interrupt
566591
///
567592
/// **NOTE** The `interrupt!` macro on Cortex-M and MSP430 device crates is closer in syntax to the

src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
use log::{error, info};
44
use std::path::{Path, PathBuf};
5-
use svd_parser::svd;
6-
7-
mod generate;
8-
mod util;
95

106
use std::fs::File;
117
use std::io::Write;
@@ -14,7 +10,10 @@ use std::process;
1410
use anyhow::{Context, Result};
1511
use clap::{App, Arg};
1612

17-
use crate::util::{build_rs, load_from, Config, Source, Target};
13+
use svd2rust::{
14+
generate, load_from,
15+
util::{build_rs, Config, SourceType, Target},
16+
};
1817

1918
fn run() -> Result<()> {
2019
use clap_conf::prelude::*;
@@ -156,11 +155,11 @@ fn run() -> Result<()> {
156155
.arg("source_type")
157156
.conf("source_type")
158157
.done()
159-
.and_then(|s| Source::from_str(&s))
158+
.and_then(|s| SourceType::from_extension(&s))
160159
.unwrap_or_default();
161160

162161
if let Some(file) = matches.value_of("input") {
163-
source_type = Source::from_path(Path::new(file))
162+
source_type = SourceType::from_path(Path::new(file))
164163
}
165164

166165
let config = Config {

src/util.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22

3-
use crate::svd::{Access, Cluster, Device, Field, Register, RegisterCluster, RegisterInfo, RegisterProperties,, ValidateLevel};
3+
use crate::svd::{
4+
Access, Cluster, Field, Register, RegisterCluster, RegisterInfo, RegisterProperties,
5+
};
46
use inflections::Inflect;
57
use proc_macro2::{Ident, Literal, Span, TokenStream};
68
use quote::{quote, ToTokens};
@@ -14,26 +16,6 @@ pub const BITS_PER_BYTE: u32 = 8;
1416
/// that are not valid in Rust ident
1517
const BLACKLIST_CHARS: &[char] = &['(', ')', '[', ']', '/', ' ', '-'];
1618

17-
pub(crate) fn load_from(input: &str, config: &crate::util::Config) -> Result<Device> {
18-
Ok(match config.source_type {
19-
Source::Xml => {
20-
let mut parser_config = svd_parser::Config::default();
21-
parser_config.validate_level = if config.strict {
22-
ValidateLevel::Strict
23-
} else {
24-
ValidateLevel::Weak
25-
};
26-
27-
svd_parser::parse_with_config(input, &parser_config)
28-
.with_context(|| "Error parsing SVD XML file".to_string())?
29-
}
30-
Source::Yaml => serde_yaml::from_str(input)
31-
.with_context(|| "Error parsing SVD YAML file".to_string())?,
32-
Source::Json => serde_json::from_str(input)
33-
.with_context(|| "Error parsing SVD JSON file".to_string())?,
34-
})
35-
}
36-
3719
#[derive(Clone, PartialEq, Debug)]
3820
pub struct Config {
3921
pub target: Target,
@@ -44,7 +26,7 @@ pub struct Config {
4426
pub ignore_groups: bool,
4527
pub strict: bool,
4628
pub output_dir: PathBuf,
47-
pub source_type: Source,
29+
pub source_type: SourceType,
4830
}
4931

5032
impl Default for Config {
@@ -58,7 +40,7 @@ impl Default for Config {
5840
ignore_groups: false,
5941
strict: false,
6042
output_dir: PathBuf::from("."),
61-
source_type: Source::default(),
43+
source_type: SourceType::default(),
6244
}
6345
}
6446
}
@@ -96,20 +78,21 @@ impl Default for Target {
9678
}
9779

9880
#[derive(Clone, Copy, PartialEq, Debug)]
99-
pub enum Source {
81+
pub enum SourceType {
10082
Xml,
10183
Yaml,
10284
Json,
10385
}
10486

105-
impl Default for Source {
87+
impl Default for SourceType {
10688
fn default() -> Self {
10789
Self::Xml
10890
}
10991
}
11092

111-
impl Source {
112-
pub fn from_str(s: &str) -> Option<Self> {
93+
impl SourceType {
94+
/// Make a new [`Source`] from a given extension.
95+
pub fn from_extension(s: &str) -> Option<Self> {
11396
match s {
11497
"yml" | "yaml" => Some(Self::Yaml),
11598
"json" => Some(Self::Json),
@@ -120,7 +103,7 @@ impl Source {
120103
pub fn from_path(path: &Path) -> Self {
121104
path.extension()
122105
.and_then(|e| e.to_str())
123-
.and_then(Self::from_str)
106+
.and_then(Self::from_extension)
124107
.unwrap_or_default()
125108
}
126109
}

0 commit comments

Comments
 (0)