Enum ruststep::ast::Parameter [−][src]
pub enum Parameter {
Typed {
keyword: String,
parameter: Box<Parameter>,
},
Integer(i64),
Real(f64),
String(String),
Enumeration(String),
List(Vec<Parameter>),
Ref(Name),
NotProvided,
Omitted,
}
Expand description
Primitive value type in STEP data
Inline struct or list can be nested, i.e. Parameter
can be a tree.
use nom::Finish;
use ruststep::{parser::exchange, ast::{Parameter, Record}};
let (residual, p) = exchange::parameter("B((1.0, A((2.0, 3.0))))")
.finish()
.unwrap();
assert_eq!(residual, "");
// A((2.0, 3.0))
let a = Parameter::Typed {
keyword: "A".to_string(),
parameter: Box::new(vec![Parameter::real(2.0), Parameter::real(3.0)].into()),
};
// B((1.0, a))
let b = Parameter::Typed {
keyword: "B".to_string(),
parameter: Box::new(vec![Parameter::real(1.0), a].into()),
};
assert_eq!(p, b);
FromIterator
Create a list as Parameter::List
from Iterator<Item=Parameter>
or Iterator<Item=&Parameter>
.
use ruststep::ast::Parameter;
let p: Parameter = [Parameter::real(1.0), Parameter::real(2.0)]
.iter()
.collect();
assert!(matches!(p, Parameter::List(_)));
Deserialize
Parameter | serde data model |
---|---|
Integer | i64 |
Real | f64 |
String | string |
List | seq |
NotProvided | option (always none) |
Omitted | option (always none) |
Enumeration | unit_variant (through serde::de::value::StringDeserializer) |
Typed | map (through de::RecordDeserializer) |
Ref | newtype_variant |
Variants
Corresponding to TYPED_PARAMETER
in WSN:
TYPED_PARAMETER = KEYWORD "(" PARAMETER ")" .
and parser::exchange::typed_parameter.
It takes only one PARAMETER
different from Record,
which takes many PARAMETER
s.
SIMPLE_RECORD = KEYWORD "(" [ PARAMETER_LIST ] ")" .
FromStr
use std::str::FromStr;
use ruststep::ast::Parameter;
let p = Parameter::from_str("FILE_NAME('ruststep')").unwrap();
assert!(matches!(p, Parameter::Typed { .. }));
Deserialize
use std::{str::FromStr, collections::HashMap};
use ruststep::ast::*;
use serde::Deserialize;
// Regarded as a map `{ "A": [1, 2] }` in serde data model
let p = Parameter::from_str("A((1, 2))").unwrap();
// Map can be deserialize as a hashmap
assert_eq!(
HashMap::<String, Vec<i32>>::deserialize(&p).unwrap(),
maplit::hashmap! {
"A".to_string() => vec![1, 2]
}
);
// Map in serde can be interpreted as Rust field
#[derive(Debug, Clone, PartialEq, Deserialize)]
struct X {
#[serde(rename = "A")]
a: Vec<i32>,
}
assert_eq!(X::deserialize(&p).unwrap(), X { a: vec![1, 2] });
Different from Record, deserializing into a struct is not supported:
use std::{str::FromStr, collections::HashMap};
use ruststep::ast::*;
use serde::Deserialize;
let p = Parameter::from_str("A(1)").unwrap();
#[derive(Debug, Clone, PartialEq, Deserialize)]
struct A {
x: i32,
}
assert!(A::deserialize(&p).is_err());
Integer(i64)
Signed integer
FromStr
use std::str::FromStr;
use ruststep::ast::Parameter;
let p = Parameter::from_str("10").unwrap();
assert_eq!(p, Parameter::Integer(10));
let p = Parameter::from_str("-10").unwrap();
assert_eq!(p, Parameter::Integer(-10));
Deserialize
use ruststep::ast::*;
use serde::Deserialize;
let p = Parameter::Integer(2);
let a = i64::deserialize(&p).unwrap();
assert_eq!(a, 2);
// can be deserialized as unsigned
let a = u64::deserialize(&p).unwrap();
assert_eq!(a, 2);
// cannot be deserialized negative integer into unsigned
let p = Parameter::Integer(-2);
let a = i64::deserialize(&p).unwrap();
assert_eq!(a, -2);
assert!(u64::deserialize(&p).is_err());
Tuple Fields of Integer
0: i64
Real(f64)
Real number
FromStr
use std::str::FromStr;
use ruststep::ast::Parameter;
let p = Parameter::from_str("1.0").unwrap();
assert_eq!(p, Parameter::Real(1.0));
Tuple Fields of Real
0: f64
String(String)
string literal
FromStr
use std::str::FromStr;
use ruststep::ast::Parameter;
let p = Parameter::from_str("'EXAMPLE STRING'").unwrap();
assert_eq!(p, Parameter::String("EXAMPLE STRING".to_string()));
Tuple Fields of String
0: String
Enumeration(String)
Enumeration defined in EXPRESS schema, like .TRUE.
FromStr
let p = Parameter::from_str(".TRUE.").unwrap();
assert_eq!(p, Parameter::Enumeration("TRUE".to_string()));
Deserialize
use ruststep::ast::*;
use serde::Deserialize;
use std::str::FromStr;
let p = Parameter::from_str(".A.").unwrap();
#[derive(Debug, PartialEq, Deserialize)]
enum E {
A,
B
}
assert_eq!(E::deserialize(&p).unwrap(), E::A);
Tuple Fields of Enumeration
0: String
List of parameters. This can be non-uniform.
FromStr
use std::str::FromStr;
use ruststep::ast::Parameter;
let p = Parameter::from_str("(1.0, 2, 'STRING')").unwrap();
assert_eq!(p, Parameter::List(vec![
Parameter::Real(1.0),
Parameter::Integer(2),
Parameter::String("STRING".to_string()),
]));
Deserialize
use std::str::FromStr;
use ruststep::ast::*;
use serde::Deserialize;
let p = Parameter::from_str("(1, 2, 3)").unwrap();
// As Vec<i32>
let a = Vec::<i32>::deserialize(&p).unwrap();
assert_eq!(a, vec![1, 2, 3]);
// As user-defined struct
#[derive(Debug, Clone, PartialEq, Deserialize)]
struct A {
x: i32,
y: i32,
z: i32,
}
let a = A::deserialize(&p).unwrap();
assert_eq!(a, A { x: 1, y: 2, z: 3 });
Ref(Name)
A reference to entity or value
Deserialize
use ruststep::ast::*;
use serde::Deserialize;
use std::str::FromStr;
let p = Parameter::from_str("#12").unwrap();
#[derive(Debug, PartialEq, Deserialize)]
enum Id {
#[serde(rename = "Entity")] // "Entity" is keyword for entity reference
E(usize),
#[serde(rename = "Value")] // "Value" is keyword for value reference
V(usize),
}
assert_eq!(Id::deserialize(&p).unwrap(), Id::E(12));
Tuple Fields of Ref
0: Name
The special token dollar sign ($
) is used to represent
an object whose value is not provided in the exchange structure.
Deserialize
use ruststep::ast::*;
use serde::Deserialize;
let p = Parameter::NotProvided;
assert_eq!(Option::<i64>::deserialize(&p).unwrap(), None);
Omitted parameter denoted by *
Deserialize
use ruststep::ast::*;
use serde::Deserialize;
let p = Parameter::Omitted;
assert_eq!(Option::<i64>::deserialize(&p).unwrap(), None);
Implementations
Trait Implementations
The error type that can be returned if some error occurs during deserialization. Read more
Require the Deserializer
to figure out how to drive the visitor based
on what data type is in the input. Read more
Hint that the Deserialize
type is expecting a bool
value.
Hint that the Deserialize
type is expecting an optional value. Read more
Hint that the Deserialize
type is expecting an i8
value.
Hint that the Deserialize
type is expecting an i16
value.
Hint that the Deserialize
type is expecting an i32
value.
Hint that the Deserialize
type is expecting an i64
value.
Hint that the Deserialize
type is expecting an i128
value. Read more
Hint that the Deserialize
type is expecting a u8
value.
Hint that the Deserialize
type is expecting a u16
value.
Hint that the Deserialize
type is expecting a u32
value.
Hint that the Deserialize
type is expecting a u64
value.
Hint that the Deserialize
type is expecting an u128
value. Read more
Hint that the Deserialize
type is expecting a f32
value.
Hint that the Deserialize
type is expecting a f64
value.
Hint that the Deserialize
type is expecting a char
value.
Hint that the Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
. Read more
Hint that the Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read more
Hint that the Deserialize
type is expecting a byte array and does not
benefit from taking ownership of buffered data owned by the
Deserializer
. Read more
Hint that the Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read more
Hint that the Deserialize
type is expecting a unit value.
Hint that the Deserialize
type is expecting a unit struct with a
particular name. Read more
Hint that the Deserialize
type is expecting a newtype struct with a
particular name. Read more
Hint that the Deserialize
type is expecting a sequence of values.
Hint that the Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data. Read more
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
fn deserialize_struct<V>(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
Hint that the Deserialize
type is expecting a struct with a particular
name and fields. Read more
Hint that the Deserialize
type is expecting a tuple struct with a
particular name and number of fields. Read more
Hint that the Deserialize
type is expecting a map of key-value pairs.
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V
) -> Result<V::Value, Self::Error> where
V: Visitor<'de>,
Hint that the Deserialize
type is expecting an enum value with a
particular name and possible variants. Read more
Hint that the Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant. Read more
Hint that the Deserialize
type needs to deserialize a value whose type
doesn’t matter because it is ignored. Read more
Determine whether Deserialize
implementations should expect to
deserialize their human-readable form. Read more
Creates a value from an iterator. Read more
Creates a value from an iterator. Read more
Auto Trait Implementations
impl RefUnwindSafe for Parameter
impl UnwindSafe for Parameter
Blanket Implementations
Mutably borrows from an owned value. Read more