|
| 1 | +use anyhow::{bail, Result}; |
| 2 | +use std::path::{Path, PathBuf}; |
| 3 | + |
| 4 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 5 | +#[derive(Clone, PartialEq, Eq, Debug)] |
| 6 | +pub struct Config { |
| 7 | + #[cfg_attr(feature = "serde", serde(default))] |
| 8 | + pub target: Target, |
| 9 | + #[cfg_attr(feature = "serde", serde(default))] |
| 10 | + pub atomics: bool, |
| 11 | + #[cfg_attr(feature = "serde", serde(default))] |
| 12 | + pub atomics_feature: Option<String>, |
| 13 | + #[cfg_attr(feature = "serde", serde(default))] |
| 14 | + pub generic_mod: bool, |
| 15 | + #[cfg_attr(feature = "serde", serde(default))] |
| 16 | + pub make_mod: bool, |
| 17 | + #[cfg_attr(feature = "serde", serde(default))] |
| 18 | + pub ignore_groups: bool, |
| 19 | + #[cfg_attr(feature = "serde", serde(default))] |
| 20 | + pub keep_list: bool, |
| 21 | + #[cfg_attr(feature = "serde", serde(default))] |
| 22 | + pub strict: bool, |
| 23 | + #[cfg_attr(feature = "serde", serde(default))] |
| 24 | + pub pascal_enum_values: bool, |
| 25 | + #[cfg_attr(feature = "serde", serde(default))] |
| 26 | + pub feature_group: bool, |
| 27 | + #[cfg_attr(feature = "serde", serde(default))] |
| 28 | + pub feature_peripheral: bool, |
| 29 | + #[cfg_attr(feature = "serde", serde(default))] |
| 30 | + pub max_cluster_size: bool, |
| 31 | + #[cfg_attr(feature = "serde", serde(default))] |
| 32 | + pub impl_debug: bool, |
| 33 | + #[cfg_attr(feature = "serde", serde(default))] |
| 34 | + pub impl_debug_feature: Option<String>, |
| 35 | + #[cfg_attr(feature = "serde", serde(default = "current_dir"))] |
| 36 | + pub output_dir: PathBuf, |
| 37 | + #[cfg_attr(feature = "serde", serde(default))] |
| 38 | + pub input: Option<PathBuf>, |
| 39 | + #[cfg_attr(feature = "serde", serde(default))] |
| 40 | + pub source_type: SourceType, |
| 41 | + #[cfg_attr(feature = "serde", serde(default))] |
| 42 | + pub log_level: Option<String>, |
| 43 | + #[cfg_attr(feature = "serde", serde(default))] |
| 44 | + pub interrupt_link_section: Option<String>, |
| 45 | + #[cfg_attr(feature = "serde", serde(default))] |
| 46 | + pub reexport_core_peripherals: bool, |
| 47 | + #[cfg_attr(feature = "serde", serde(default))] |
| 48 | + pub reexport_interrupt: bool, |
| 49 | +} |
| 50 | + |
| 51 | +fn current_dir() -> PathBuf { |
| 52 | + PathBuf::from(".") |
| 53 | +} |
| 54 | + |
| 55 | +impl Default for Config { |
| 56 | + fn default() -> Self { |
| 57 | + Self { |
| 58 | + target: Target::default(), |
| 59 | + atomics: false, |
| 60 | + atomics_feature: None, |
| 61 | + generic_mod: false, |
| 62 | + make_mod: false, |
| 63 | + ignore_groups: false, |
| 64 | + keep_list: false, |
| 65 | + strict: false, |
| 66 | + pascal_enum_values: false, |
| 67 | + feature_group: false, |
| 68 | + feature_peripheral: false, |
| 69 | + max_cluster_size: false, |
| 70 | + impl_debug: false, |
| 71 | + impl_debug_feature: None, |
| 72 | + output_dir: current_dir(), |
| 73 | + input: None, |
| 74 | + source_type: SourceType::default(), |
| 75 | + log_level: None, |
| 76 | + interrupt_link_section: None, |
| 77 | + reexport_core_peripherals: false, |
| 78 | + reexport_interrupt: false, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[allow(clippy::upper_case_acronyms)] |
| 84 | +#[allow(non_camel_case_types)] |
| 85 | +#[cfg_attr(feature = "serde", derive(serde::Deserialize))] |
| 86 | +#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] |
| 87 | +pub enum Target { |
| 88 | + #[cfg_attr(feature = "serde", serde(rename = "cortex-m"))] |
| 89 | + #[default] |
| 90 | + CortexM, |
| 91 | + #[cfg_attr(feature = "serde", serde(rename = "msp430"))] |
| 92 | + Msp430, |
| 93 | + #[cfg_attr(feature = "serde", serde(rename = "riscv"))] |
| 94 | + RISCV, |
| 95 | + #[cfg_attr(feature = "serde", serde(rename = "xtensa-lx"))] |
| 96 | + XtensaLX, |
| 97 | + #[cfg_attr(feature = "serde", serde(rename = "mips"))] |
| 98 | + Mips, |
| 99 | + #[cfg_attr(feature = "serde", serde(rename = "none"))] |
| 100 | + None, |
| 101 | +} |
| 102 | + |
| 103 | +impl Target { |
| 104 | + pub fn parse(s: &str) -> Result<Self> { |
| 105 | + Ok(match s { |
| 106 | + "cortex-m" => Target::CortexM, |
| 107 | + "msp430" => Target::Msp430, |
| 108 | + "riscv" => Target::RISCV, |
| 109 | + "xtensa-lx" => Target::XtensaLX, |
| 110 | + "mips" => Target::Mips, |
| 111 | + "none" => Target::None, |
| 112 | + _ => bail!("unknown target {}", s), |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#[cfg_attr( |
| 118 | + feature = "serde", |
| 119 | + derive(serde::Deserialize), |
| 120 | + serde(rename_all = "lowercase") |
| 121 | +)] |
| 122 | +#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)] |
| 123 | +pub enum SourceType { |
| 124 | + #[default] |
| 125 | + Xml, |
| 126 | + #[cfg(feature = "yaml")] |
| 127 | + Yaml, |
| 128 | + #[cfg(feature = "json")] |
| 129 | + Json, |
| 130 | +} |
| 131 | + |
| 132 | +impl SourceType { |
| 133 | + /// Make a new [`SourceType`] from a given extension. |
| 134 | + pub fn from_extension(s: &str) -> Option<Self> { |
| 135 | + match s { |
| 136 | + "svd" | "xml" => Some(Self::Xml), |
| 137 | + #[cfg(feature = "yaml")] |
| 138 | + "yml" | "yaml" => Some(Self::Yaml), |
| 139 | + #[cfg(feature = "json")] |
| 140 | + "json" => Some(Self::Json), |
| 141 | + _ => None, |
| 142 | + } |
| 143 | + } |
| 144 | + pub fn from_path(path: &Path) -> Self { |
| 145 | + path.extension() |
| 146 | + .and_then(|e| e.to_str()) |
| 147 | + .and_then(Self::from_extension) |
| 148 | + .unwrap_or_default() |
| 149 | + } |
| 150 | +} |
0 commit comments