Skip to content

move all check_name's under strict feature #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,19 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test

test strict:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
24 changes: 16 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

pub use anyhow::{Context, Result};
use core::u64;
#[cfg(feature = "strict")]
use once_cell::sync::Lazy;
#[cfg(feature = "strict")]
use regex::Regex;
use xmltree::Element;

Expand Down Expand Up @@ -87,6 +89,7 @@ pub enum ResetValueError {
MaskTooLarge(u64, u32),
}

#[cfg(feature = "strict")]
pub(crate) fn check_name(name: &str, tag: &str) -> Result<()> {
static PATTERN: Lazy<Regex> = Lazy::new(|| Regex::new("^[_A-Za-z0-9]*$").unwrap());
if PATTERN.is_match(name) {
Expand All @@ -98,6 +101,7 @@ pub(crate) fn check_name(name: &str, tag: &str) -> Result<()> {
}
}

#[cfg(feature = "strict")]
pub(crate) fn check_dimable_name(name: &str, tag: &str) -> Result<()> {
static PATTERN: Lazy<Regex> = Lazy::new(|| {
Regex::new("^(((%s)|(%s)[_A-Za-z]{1}[_A-Za-z0-9]*)|([_A-Za-z]{1}[_A-Za-z0-9]*(\\[%s\\])?)|([_A-Za-z]{1}[_A-Za-z0-9]*(%s)?[_A-Za-z0-9]*))$").unwrap()
Expand All @@ -117,6 +121,7 @@ pub(crate) fn check_has_placeholder(name: &str, tag: &str) -> Result<()> {
}
}

#[cfg(feature = "strict")]
pub(crate) fn check_derived_name(name: &str, tag: &str) -> Result<()> {
for x in name.split('.') {
check_dimable_name(x, tag)?
Expand All @@ -127,7 +132,7 @@ pub(crate) fn check_derived_name(name: &str, tag: &str) -> Result<()> {
pub(crate) fn check_reset_value(
size: Option<u32>,
value: Option<u64>,
mask: Option<u64>,
_mask: Option<u64>,
) -> Result<()> {
const MAX_BITS: u32 = u64::MAX.count_ones();

Expand All @@ -136,14 +141,17 @@ pub(crate) fn check_reset_value(
return Err(ResetValueError::ValueTooLarge(value, size).into());
}
}
if let (Some(size), Some(mask)) = (size, mask) {
if MAX_BITS - mask.leading_zeros() > size {
return Err(ResetValueError::MaskTooLarge(mask, size).into());
#[cfg(feature = "strict")]
{
if let (Some(size), Some(mask)) = (size, _mask) {
if MAX_BITS - mask.leading_zeros() > size {
return Err(ResetValueError::MaskTooLarge(mask, size).into());
}
}
}
if let (Some(value), Some(mask)) = (value, mask) {
if value & mask != value {
return Err(ResetValueError::MaskConflict(value, mask).into());
if let (Some(value), Some(mask)) = (value, _mask) {
if value & mask != value {
return Err(ResetValueError::MaskConflict(value, mask).into());
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/svd/clusterinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ impl ClusterInfoBuilder {

impl ClusterInfo {
fn validate(self) -> Result<Self> {
#[cfg(feature = "strict")]
check_dimable_name(&self.name, "name")?;
if let Some(name) = self.derived_from.as_ref() {
check_derived_name(name, "derivedFrom")?;
if let Some(_name) = self.derived_from.as_ref() {
#[cfg(feature = "strict")]
check_derived_name(_name, "derivedFrom")?;
} else if self.children.is_empty() {
#[cfg(feature = "strict")]
return Err(SVDError::EmptyCluster)?;
Expand Down
1 change: 1 addition & 0 deletions src/svd/enumeratedvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl EnumeratedValueBuilder {

impl EnumeratedValue {
fn validate(self) -> Result<Self> {
#[cfg(feature = "strict")]
check_name(&self.name, "name")?;
match (&self.value, &self.is_default) {
(Some(_), None) | (None, Some(_)) => Ok(self),
Expand Down
12 changes: 8 additions & 4 deletions src/svd/enumeratedvalues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ impl EnumeratedValuesBuilder {

impl EnumeratedValues {
fn validate(self) -> Result<Self> {
if let Some(name) = self.name.as_ref() {
check_name(name, "name")?;
#[cfg(feature = "strict")]
{
if let Some(name) = self.name.as_ref() {
check_name(name, "name")?;
}
}
if let Some(dname) = self.derived_from.as_ref() {
check_derived_name(dname, "derivedFrom")?;
if let Some(_dname) = self.derived_from.as_ref() {
#[cfg(feature = "strict")]
check_derived_name(_dname, "derivedFrom")?;
Ok(self)
} else if self.values.is_empty() {
Err(EnumeratedValuesError::Empty.into())
Expand Down
8 changes: 6 additions & 2 deletions src/svd/fieldinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ impl FieldInfoBuilder {

impl FieldInfo {
fn validate(self) -> Result<Self> {
#[cfg(feature = "strict")]
check_dimable_name(&self.name, "name")?;
if let Some(name) = self.derived_from.as_ref() {
check_derived_name(name, "derivedFrom")?;
#[cfg(feature = "strict")]
{
if let Some(name) = self.derived_from.as_ref() {
check_derived_name(name, "derivedFrom")?;
}
}

if self.bit_range.width == 0 {
Expand Down
6 changes: 4 additions & 2 deletions src/svd/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ impl PeripheralBuilder {
impl Peripheral {
fn validate(self) -> Result<Self> {
// TODO
#[cfg(feature = "strict")]
check_dimable_name(&self.name, "name")?;
if let Some(name) = self.derived_from.as_ref() {
check_dimable_name(name, "derivedFrom")?;
if let Some(_name) = self.derived_from.as_ref() {
#[cfg(feature = "strict")]
check_dimable_name(_name, "derivedFrom")?;
} else if let Some(registers) = self.registers.as_ref() {
if registers.is_empty() {
#[cfg(feature = "strict")]
Expand Down
19 changes: 12 additions & 7 deletions src/svd/registerinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,20 @@ impl RegisterInfoBuilder {

impl RegisterInfo {
fn validate(self) -> Result<Self> {
#[cfg(feature = "strict")]
check_dimable_name(&self.name, "name")?;
if let Some(name) = self.alternate_group.as_ref() {
check_name(name, "alternateGroup")?;
}
if let Some(name) = self.alternate_register.as_ref() {
check_dimable_name(name, "alternateRegister")?;
#[cfg(feature = "strict")]
{
if let Some(name) = self.alternate_group.as_ref() {
check_name(name, "alternateGroup")?;
}
if let Some(name) = self.alternate_register.as_ref() {
check_dimable_name(name, "alternateRegister")?;
}
}
if let Some(name) = self.derived_from.as_ref() {
check_derived_name(name, "derivedFrom")?;
if let Some(_name) = self.derived_from.as_ref() {
#[cfg(feature = "strict")]
check_derived_name(_name, "derivedFrom")?;
} else if let Some(fields) = self.fields.as_ref() {
if fields.is_empty() {
#[cfg(feature = "strict")]
Expand Down