Skip to content
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

add optional range restrictions for numeric CustomFeatureFormat instances #185

Open
robfitzgerald opened this issue Apr 10, 2024 · 0 comments
Labels
rust Applies to the rust code

Comments

@robfitzgerald
Copy link
Collaborator

robfitzgerald commented Apr 10, 2024

if a user can overwrite the initial state of charge, they could do so with the wrong unit (ratio vs percent). to avoid this, we could add optional numeric range fields for these types, and use them during encode/decode operations as constraints which can cause StateError responses. for example:

pub enum CustomFeatureFormat {
    FloatingPoint { initial: OrderedFloat<f64>, min: Option<OrderedFloat<f64>>, max: Option<OrderedFloat<f64>> },
    ...
}

impl CustomFeatureFormat {
    pub fn encode_f64(&self, value: &f64) -> Result<StateVar, StateError> {
        match self {
            CustomFeatureFormat::FloatingPoint { initial: _, min, max } => {
                let min_val = min.unwrap_or(OrderedFloat(f64::MIN));
                if value < min_val {
                    Err(...)  // also test vs max
                } else {
                    Ok(StateVar(*value))
                }
            },
            _ => Err(StateError::EncodeError(
                UnitCodecType::FloatingPoint.to_string(),
                self.name(),
            )),
        }
    }
    ...
}

testing this

@robfitzgerald robfitzgerald added the rust Applies to the rust code label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust Applies to the rust code
Projects
None yet
Development

No branches or pull requests

1 participant