Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 5.59 KB | None | 0 0
  1. #![feature(box_syntax, box_patterns)]
  2. extern crate futures;
  3.  
  4. use std::collections::BTreeMap;
  5. use std::rc::Rc;
  6. use std::marker::PhantomData;
  7.  
  8.  
  9. use futures::future::{err, ok};
  10. use futures::Future;
  11.  
  12. #[derive(Clone, Debug, PartialEq, Eq)]
  13. pub enum ValueType {
  14.     Character,
  15.     Integer,
  16.     Decimal,
  17.     BitField(usize /* length */),
  18.     List(Rc<ValueType>, Option<usize> /* max length */),
  19.     Collection(Rc<[ValueType]>),
  20. }
  21.  
  22. #[derive(Clone, Debug)]
  23. pub enum Value {
  24.     Character(char),
  25.     Integer(i32),
  26.     Decimal(f32),
  27.     BitField(usize, Rc<[bool]>),
  28.     List(Rc<ValueType>, Option<usize>, Rc<[Value]>),
  29.     Collection(Rc<[ValueType]>, Rc<[Value]>),
  30. }
  31.  
  32. impl From<Value> for ValueType {
  33.     fn from(value: Value) -> Self {
  34.         match value {
  35.             Value::Character(_) => ValueType::Character,
  36.             Value::Integer(_) => ValueType::Integer,
  37.             Value::Decimal(_) => ValueType::Decimal,
  38.             Value::BitField(len, _) => ValueType::BitField(len),
  39.             Value::List(value_type, max_length, _) => ValueType::List(value_type, max_length),
  40.             Value::Collection(value_types, _) => ValueType::Collection(value_types),
  41.             _ => unimplemented!(),
  42.         }
  43.     }
  44. }
  45.  
  46. impl Value {
  47.     pub fn get_type(&self) -> ValueType {
  48.         match self {
  49.             Value::Character(_) => ValueType::Character,
  50.             Value::Integer(_) => ValueType::Integer,
  51.             Value::Decimal(_) => ValueType::Decimal,
  52.             Value::BitField(len, _) => ValueType::BitField(*len),
  53.             Value::List(value_type, max_length, _) => {
  54.                 ValueType::List(value_type.clone(), *max_length)
  55.             }
  56.             Value::Collection(value_types, _) => ValueType::Collection(value_types.clone()),
  57.             _ => unimplemented!(),
  58.         }
  59.     }
  60. }
  61. impl ValueType {
  62.     pub fn validate(&self, val: &Value) -> bool {
  63.         *self == val.get_type()
  64.     }
  65.     pub fn default(&self) -> Value {
  66.         match self {
  67.             ValueType::Character => Value::Character('\x00'),
  68.             ValueType::Integer => Value::Integer(0),
  69.             ValueType::Decimal => Value::Decimal(0.0f32),
  70.             ValueType::BitField(len) => {
  71.                 Value::BitField(*len, Rc::from(Vec::with_capacity(*len).as_slice()))
  72.             }
  73.             ValueType::List(value_type, len) => match *len {
  74.                 Some(len) => {
  75.                     let mut list = Vec::with_capacity(len);
  76.                     for i in 0..len {
  77.                         list.push((**value_type).default());
  78.                     }
  79.                     Value::List(value_type.clone(), Some(len), Rc::from(list.as_slice()))
  80.                 }
  81.                 None => Value::List(value_type.clone(), None, Rc::from(vec![].as_slice())),
  82.             },
  83.             ValueType::Collection(types) => {
  84.                 let mut collection = Vec::new();
  85.                 for t in types.as_ref() {
  86.                     collection.push(t.default());
  87.                 }
  88.                 Value::Collection(Rc::from(types.as_ref()), Rc::from(collection.as_slice()))
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. pub struct ValueDescriptor {
  95.     value_type: ValueType,
  96.     default: Option<Value>,
  97. }
  98.  
  99. impl ValueDescriptor {
  100.     pub fn new(value_type: ValueType, default: Option<Value>) -> Result<ValueDescriptor, String> {
  101.         match default {
  102.             Some(val) => {
  103.                 if !value_type.validate(&val) {
  104.                     return Err(format!(
  105.                         "Incompatible types: '{:?}' and '{:?}'",
  106.                         value_type,
  107.                         val.get_type()
  108.                     ));
  109.                 }
  110.                 return Ok(ValueDescriptor {
  111.                     value_type,
  112.                     default: Some(val),
  113.                 });
  114.             }
  115.             None => Ok(ValueDescriptor {
  116.                 value_type,
  117.                 default: None,
  118.             }),
  119.         }
  120.     }
  121.     pub fn default(&self) -> Value {
  122.         match &self.default {
  123.             Some(val) => val.clone(),
  124.             None => self.value_type.default(),
  125.         }
  126.     }
  127. }
  128.  
  129. type ValueConstraint = dyn Fn(Value, Value) -> bool;
  130.  
  131. pub struct DeviceType<'a> {
  132.    parameters: BTreeMap<&'a str, ValueDescriptor>,
  133.     constants: BTreeMap<&'a str, ValueDescriptor>,
  134.    results: BTreeMap<&'a str, ValueDescriptor>,
  135. }
  136.  
  137. pub struct Input<'a> {
  138.    value_type: ValueType,
  139.    value: &'a dyn Future<Output = Result<Value, ()>>,
  140. }
  141.  
  142. pub struct Output<'a> {
  143.    value_type: ValueType,
  144.    value: Box<dyn Future<Output = Result<Value, ()>>>,
  145.    phantom: PhantomData<&'a Value>
  146. }
  147.  
  148. pub struct Constant {
  149.     value_type: ValueType,
  150.     value: Box<dyn Future<Output = Result<Value, ()>>>,
  151. }
  152.  
  153. impl<'a> Output<'a> {
  154.     pub fn attach(&self, input: &mut Input<'a>) {
  155.        input.value = self.value.as_ref();
  156.    }
  157. }
  158.  
  159. impl Constant {
  160.    pub fn attach<'a>(&self, input: &mut Input<'a>) -> &Self {
  161.        input.value = self.value.as_ref();
  162.        self
  163.    }
  164. }
  165.  
  166. pub struct Device<'a> {
  167.     inputs: BTreeMap<&'a str, Input<'a>>,
  168.     constants: BTreeMap<&'a str, Constant>,
  169.    outputs: BTreeMap<&'a str, Output<'a>>,
  170. }
  171.  
  172. impl<'a> Device<'a> {
  173.    pub fn get_outputs(&self) -> Vec<(&str, ValueType)> {
  174.        self.outputs
  175.            .iter()
  176.            .map(|(k, v)| (*k, v.value_type))
  177.            .collect()
  178.    }
  179.    pub fn get_constants(&self) -> Vec<(&str, ValueType, Value)> {
  180.        self.constants
  181.            .iter()
  182.            .map(|(k, v)| (*k, v.value_type, v.value))
  183.            .collect()
  184.    }
  185. }
  186.  
  187. fn main() {
  188.    println!("Hello, world!");
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement