Skip to main content

gwr_engine/
types.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Shared types.
4
5use std::error::Error;
6use std::fmt;
7use std::rc::Rc;
8
9use crate::traits::{Event, Runnable};
10
11/// The return value from a call to [listen()](crate::traits::Event)
12pub type EventResult<T> = T;
13
14pub type Eventable<T> = Box<dyn Event<T> + 'static>;
15
16/// The type of a component that can be registered with the `Engine` so that it
17/// will automatically be spawned.
18pub type Component = Rc<dyn Runnable + 'static>;
19
20// Simulation errors
21
22/// Build a [SimError] from a message that supports `to_string`
23#[macro_export]
24macro_rules! sim_error {
25    ($($arg:tt)+) => {
26        Err($crate::types::SimError(format!($($arg)+).to_string()))
27    };
28}
29
30/// Error returned while parsing, constructing, connecting, or running a
31/// simulation.
32///
33/// Prefer catching static mistakes early. Use parse-time validation when
34/// reading files, and construction-time and connect-time validation where
35/// possible.
36///
37/// Include enough detail to identify the entity, config item, port, node, or
38/// edge that caused the failure clearly enough to fix the model topology or
39/// input.
40#[derive(Debug)]
41pub struct SimError(pub String);
42
43impl fmt::Display for SimError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        write!(f, "{}", self.0)
46    }
47}
48
49impl Error for SimError {}
50
51/// The SimResult is the return type for most simulation functions
52pub type SimResult = Result<(), SimError>;
53
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
55pub struct DeviceId(pub u64);
56
57/// Generic access types
58#[derive(Copy, Clone, Debug, Default, PartialEq)]
59pub enum AccessType {
60    #[default]
61    ReadRequest,
62    WriteRequest,
63    WriteNonPostedRequest,
64    ReadResponse,
65    WriteNonPostedResponse,
66    Control,
67}
68
69impl fmt::Display for AccessType {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        match self {
72            AccessType::ReadRequest => {
73                write!(f, "ReadRequest")
74            }
75            AccessType::WriteRequest => {
76                write!(f, "WriteRequest")
77            }
78            AccessType::WriteNonPostedRequest => {
79                write!(f, "WriteNonPostedRequest")
80            }
81            AccessType::ReadResponse => {
82                write!(f, "ReadResponse")
83            }
84            AccessType::WriteNonPostedResponse => {
85                write!(f, "WriteNonPostedResponse")
86            }
87            AccessType::Control => {
88                write!(f, "Control")
89            }
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    // Test added for code coverage
99    #[test]
100    fn access_type_display_names_match_variants() {
101        assert_eq!(AccessType::ReadRequest.to_string(), "ReadRequest");
102        assert_eq!(AccessType::WriteRequest.to_string(), "WriteRequest");
103        assert_eq!(
104            AccessType::WriteNonPostedRequest.to_string(),
105            "WriteNonPostedRequest"
106        );
107        assert_eq!(AccessType::ReadResponse.to_string(), "ReadResponse");
108        assert_eq!(
109            AccessType::WriteNonPostedResponse.to_string(),
110            "WriteNonPostedResponse"
111        );
112        assert_eq!(AccessType::Control.to_string(), "Control");
113    }
114}