Skip to content

Commit

Permalink
Replace magic numbers with the explanatory enums
Browse files Browse the repository at this point in the history
  • Loading branch information
isbm committed Oct 30, 2024
1 parent 5b03255 commit 691f4e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 12 additions & 0 deletions libsysinspect/src/intp/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ use serde_json::Value as JsonValue;
use serde_yaml::Value as YamlValue;
use std::hash::Hash;

pub enum StaticNamespace {
SECTION = 0,
ENTITY = 1,
REGION = 2,
STATE = 3,
LABEL = 4,
}

pub enum ClaimNamespace {
LABEL = 0,
}

pub trait ExtValue: Clone {
type Key: Clone + Eq + Hash;

Expand Down
20 changes: 14 additions & 6 deletions libsysinspect/src/intp/inspector.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use super::{
actions::Action,
checkbook::CheckbookSection,
conf::Config,
constraints::Constraint,
entities::Entity,
functions::{ClaimNamespace, ModArgFunction, StaticNamespace},
relations::Relation,
};
use crate::{
intp::functions,
Expand Down Expand Up @@ -183,7 +190,7 @@ impl SysInspector {
match func.fid() {
"claim" | "static" => {
if func.namespace().is_empty() {
return Err(SysinspectError::ModelDSLError(format!("Function has missing namespace")));
return Err(SysinspectError::ModelDSLError(format!("A {} function is missing namespace", func.fid())));
}
}
_ => {
Expand All @@ -201,7 +208,7 @@ impl SysInspector {
if let Some(claims) = entity.claims() {
if let Some(claims) = claims.get(state) {
for claim in claims {
if let Some(v) = claim.get(func.ns().get(0).unwrap()) {
if let Some(v) = claim.get(func.ns().get(ClaimNamespace::LABEL as usize).unwrap()) {
if let serde_yaml::Value::Mapping(v) = v {
if let Some(v) = v.get(func.ns().get(1).unwrap()) {
return Ok(Some(v).cloned());
Expand All @@ -221,11 +228,12 @@ impl SysInspector {
}
}
} else if func.fid().eq("static") {
match func.ns().get(0).unwrap_or(&"".to_string()).as_str() {
match func.ns().get(StaticNamespace::SECTION as usize).unwrap_or(&"".to_string()).as_str() {
"entities" => {
if let Some(e) = self.entities.get(func.ns().get(1).unwrap_or(&"".to_string())) {
if let Some(e) = self.entities.get(func.ns().get(StaticNamespace::ENTITY as usize).unwrap_or(&"".to_string()))
{
// Get function state
let state = func.ns().get(3).cloned();
let state = func.ns().get(StaticNamespace::STATE as usize).cloned();
if state.is_none() {
return Err(SysinspectError::ModelDSLError(
"Static function doesn't reach state of a claim".to_string(),
Expand All @@ -234,7 +242,7 @@ impl SysInspector {
let state = state.unwrap();

// Get label
let label = func.ns().get(4).cloned();
let label = func.ns().get(StaticNamespace::LABEL as usize).cloned();
if label.is_none() {
return Err(SysinspectError::ModelDSLError(
"Static function doesn't reach label of a claim".to_string(),
Expand Down

0 comments on commit 691f4e9

Please sign in to comment.