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

Refactor implementation introducing the concept of modules and artifacts #19

Merged
merged 9 commits into from
Mar 12, 2021
39 changes: 0 additions & 39 deletions modelator/src/artifact.rs

This file was deleted.

19 changes: 19 additions & 0 deletions modelator/src/artifact/json_trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde_json::Value as JsonValue;
pub struct JsonTrace {
states: Vec<JsonValue>,
}

impl IntoIterator for JsonTrace {
type Item = JsonValue;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.states.into_iter()
}
}

impl From<Vec<JsonValue>> for JsonTrace {
fn from(states: Vec<JsonValue>) -> Self {
Self { states }
}
}
19 changes: 19 additions & 0 deletions modelator/src/artifact/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub(crate) mod json_trace;
pub(crate) mod tla_cfg_file;
pub(crate) mod tla_file;
pub(crate) mod tla_trace;

// Re-exports.
pub use json_trace::JsonTrace;
pub use tla_cfg_file::TlaConfigFile;
pub use tla_file::TlaFile;
pub use tla_trace::TlaTrace;

use serde::de::DeserializeOwned;
use serde::Serialize;

/// Artifacts should be able to serialized (into a string/file) and deserialized
/// (from string/file).
pub trait Artifact: Serialize + DeserializeOwned {}

// TODO: assert that all artifacts implement the above trait.
47 changes: 0 additions & 47 deletions modelator/src/artifact/model.rs

This file was deleted.

32 changes: 32 additions & 0 deletions modelator/src/artifact/tla_cfg_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::path::{Path, PathBuf};

pub struct TlaConfigFile {
path: PathBuf,
}

impl TlaConfigFile {
pub(crate) fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
path: path.as_ref().to_path_buf(),
}
}

pub(crate) fn path(&self) -> &PathBuf {
&self.path
}
}

impl<P> From<P> for TlaConfigFile
where
P: AsRef<Path>,
{
fn from(path: P) -> Self {
Self::new(path.as_ref().to_path_buf())
}
}

impl std::fmt::Display for TlaConfigFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.path)
}
}
47 changes: 47 additions & 0 deletions modelator/src/artifact/tla_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::path::{Path, PathBuf};

pub struct TlaFile {
path: PathBuf,
}

impl TlaFile {
pub(crate) fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
path: path.as_ref().to_path_buf(),
}
}

pub(crate) fn path(&self) -> &PathBuf {
&self.path
}

/// Infer TLA module name. We assume that the TLA module name matches the
/// name of the file.
pub(crate) fn tla_module_name(&self) -> Option<String> {
if self.path.is_file() {
self.path.file_name().map(|file_name| {
file_name
.to_string_lossy()
.trim_end_matches(".tla")
.to_owned()
})
} else {
None
}
}
}

impl<P> From<P> for TlaFile
where
P: AsRef<Path>,
{
fn from(path: P) -> Self {
Self::new(path.as_ref().to_path_buf())
}
}

impl std::fmt::Display for TlaFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.path)
}
}
38 changes: 38 additions & 0 deletions modelator/src/artifact/tla_trace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub(crate) type TlaState = String;

#[derive(Debug)]
pub struct TlaTrace {
states: Vec<TlaState>,
}

impl TlaTrace {
pub(crate) fn new() -> Self {
Self { states: Vec::new() }
}

pub(crate) fn add(&mut self, state: TlaState) {
self.states.push(state);
}

pub(crate) fn is_empty(&self) -> bool {
self.states.is_empty()
}
}

impl IntoIterator for TlaTrace {
type Item = TlaState;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.states.into_iter()
}
}

impl std::fmt::Display for TlaTrace {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (index, state) in self.states.iter().enumerate() {
write!(f, "State{} ==\n{}", index + 1, state)?;
}
Ok(())
}
}
66 changes: 0 additions & 66 deletions modelator/src/artifact/trace.rs

This file was deleted.

8 changes: 0 additions & 8 deletions modelator/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::Artifact;
use std::fmt::Debug;
use thiserror::Error;

Expand Down Expand Up @@ -33,13 +32,6 @@ pub enum Error {

#[error("Nom error: {0}")]
Nom(String),

#[error("Error while running method {module}.{method}")]
ModuleRun {
module: String,
method: String,
errors: Vec<Box<dyn Artifact>>,
},
}

#[derive(Error, Debug)]
Expand Down