Skip to main content

PrimitiveValue

Enum PrimitiveValue 

Source
pub enum PrimitiveValue {
Show 13 variants None, Char(u8), Int8(i8), Int16(i16), Int32(i32), Int64(i64), UInt8(u8), UInt16(u16), UInt32(u32), UInt64(u64), Float(f32), Double(f64), ByteArray(Vec<u8>),
}

Variants§

§

None

§

Char(u8)

§

Int8(i8)

§

Int16(i16)

§

Int32(i32)

§

Int64(i64)

§

UInt8(u8)

§

UInt16(u16)

§

UInt32(u32)

§

UInt64(u64)

§

Float(f32)

§

Double(f64)

§

ByteArray(Vec<u8>)

Implementations§

Source§

impl PrimitiveValue

Source

pub fn new(primitive_type: PrimitiveType, value: &[u8]) -> Self

Examples found in repository?
examples/dec.rs (line 84)
61    fn on_encoding(
62        &mut self,
63        field_token: &Token,
64        buffer: &[u8],
65        type_token: &Token,
66        acting_version: i32,
67    ) -> Result<(), TokenListenerError> {
68        // println!("    {name}: {{{}}} {p:?} = {buffer:02X?}", p.to_string());
69        self.print_scope();
70        if self.composite_level > 0 {
71            print!("{}", type_token.name);
72        } else {
73            print!("{}", field_token.name);
74        }
75
76        let np = self.const_or_not_present_value(type_token, field_token.version, acting_version);
77        if np.is_none() {
78            println!(
79                "={} : {buffer:02X?}",
80                buffer
81                    .chunks(type_token.encoding.primitive_type.size())
82                    .map(|b| format!(
83                        "{}",
84                        PrimitiveValue::new(type_token.encoding.primitive_type, b)
85                    ))
86                    .collect::<Vec<_>>()
87                    .join(", ")
88            );
89        } else {
90            print!("={np}");
91            for _ in 1..type_token.array_length() {
92                print!(", {np}");
93            }
94            println!(" [const]")
95        }
96
97        Ok(())
98    }
99
100    fn on_enum(
101        &mut self,
102        field_token: &Token,
103        enum_token: &Token,
104        buffer: &[u8],
105        tokens: &[Token],
106        acting_version: i32,
107    ) -> Result<(), TokenListenerError> {
108        // println!("begin enum {field_token:?}");
109        // println!("      enum {enum_token:?}");
110        // println!(
111        //     "~~> ENUM {:?}",
112        //     tokens
113        //         .iter()
114        //         .map(|x| (&x.signal, &x.name))
115        //         .collect::<Vec<_>>()
116        // );
117        let value = PrimitiveValue::new(enum_token.encoding.primitive_type, buffer);
118        let mut name: Cow<str> = "???".into();
119        if matches!(field_token.encoding.presence, Presence::Constant) {
120            name = field_token.encoding.const_value.to_string()[enum_token.name.len() + 1..]
121                .to_string()
122                .into();
123        } else {
124            for t in tokens {
125                // println!("{value:?} <> {:?} | {t:?}", t.encoding.const_value);
126                if value == t.encoding.const_value {
127                    name = (&t.name).into();
128                    break;
129                }
130            }
131        }
132        self.print_scope();
133        if self.composite_level > 0 {
134            print!("{}=", enum_token.name);
135        } else {
136            print!("{}=", field_token.name);
137        }
138        println!("{name} : {value:?}");
139
140        Ok(())
141    }
142
143    fn on_bit_set(
144        &mut self,
145        field_token: &Token,
146        set_token: &Token,
147        buffer: &[u8],
148        tokens: &[Token],
149        acting_version: i32,
150    ) -> Result<(), TokenListenerError> {
151        self.print_scope();
152        if self.composite_level > 0 {
153            print!("{}:", set_token.name);
154        } else {
155            print!("{}:", field_token.name);
156        }
157        let value: u64 = PrimitiveValue::new(set_token.encoding.primitive_type, buffer)
158            .try_into()
159            .unwrap();
160        for t in tokens {
161            let pos: u64 = (&t.encoding.const_value).try_into().unwrap();
162            let is_set = (1 << pos & value) != 0;
163            print!(" {}={is_set}", t.name);
164        }
165        println!();
166        //todo!()
167        Ok(())
168    }
Source

pub fn is_none(&self) -> bool

Examples found in repository?
examples/dec.rs (line 77)
61    fn on_encoding(
62        &mut self,
63        field_token: &Token,
64        buffer: &[u8],
65        type_token: &Token,
66        acting_version: i32,
67    ) -> Result<(), TokenListenerError> {
68        // println!("    {name}: {{{}}} {p:?} = {buffer:02X?}", p.to_string());
69        self.print_scope();
70        if self.composite_level > 0 {
71            print!("{}", type_token.name);
72        } else {
73            print!("{}", field_token.name);
74        }
75
76        let np = self.const_or_not_present_value(type_token, field_token.version, acting_version);
77        if np.is_none() {
78            println!(
79                "={} : {buffer:02X?}",
80                buffer
81                    .chunks(type_token.encoding.primitive_type.size())
82                    .map(|b| format!(
83                        "{}",
84                        PrimitiveValue::new(type_token.encoding.primitive_type, b)
85                    ))
86                    .collect::<Vec<_>>()
87                    .join(", ")
88            );
89        } else {
90            print!("={np}");
91            for _ in 1..type_token.array_length() {
92                print!(", {np}");
93            }
94            println!(" [const]")
95        }
96
97        Ok(())
98    }
99
100    fn on_enum(
101        &mut self,
102        field_token: &Token,
103        enum_token: &Token,
104        buffer: &[u8],
105        tokens: &[Token],
106        acting_version: i32,
107    ) -> Result<(), TokenListenerError> {
108        // println!("begin enum {field_token:?}");
109        // println!("      enum {enum_token:?}");
110        // println!(
111        //     "~~> ENUM {:?}",
112        //     tokens
113        //         .iter()
114        //         .map(|x| (&x.signal, &x.name))
115        //         .collect::<Vec<_>>()
116        // );
117        let value = PrimitiveValue::new(enum_token.encoding.primitive_type, buffer);
118        let mut name: Cow<str> = "???".into();
119        if matches!(field_token.encoding.presence, Presence::Constant) {
120            name = field_token.encoding.const_value.to_string()[enum_token.name.len() + 1..]
121                .to_string()
122                .into();
123        } else {
124            for t in tokens {
125                // println!("{value:?} <> {:?} | {t:?}", t.encoding.const_value);
126                if value == t.encoding.const_value {
127                    name = (&t.name).into();
128                    break;
129                }
130            }
131        }
132        self.print_scope();
133        if self.composite_level > 0 {
134            print!("{}=", enum_token.name);
135        } else {
136            print!("{}=", field_token.name);
137        }
138        println!("{name} : {value:?}");
139
140        Ok(())
141    }
142
143    fn on_bit_set(
144        &mut self,
145        field_token: &Token,
146        set_token: &Token,
147        buffer: &[u8],
148        tokens: &[Token],
149        acting_version: i32,
150    ) -> Result<(), TokenListenerError> {
151        self.print_scope();
152        if self.composite_level > 0 {
153            print!("{}:", set_token.name);
154        } else {
155            print!("{}:", field_token.name);
156        }
157        let value: u64 = PrimitiveValue::new(set_token.encoding.primitive_type, buffer)
158            .try_into()
159            .unwrap();
160        for t in tokens {
161            let pos: u64 = (&t.encoding.const_value).try_into().unwrap();
162            let is_set = (1 << pos & value) != 0;
163            print!(" {}={is_set}", t.name);
164        }
165        println!();
166        //todo!()
167        Ok(())
168    }
169
170    fn on_begin_composite(
171        &mut self,
172        field_token: &Token,
173        composite_token: &Token,
174        tokens: &[Token],
175    ) -> Result<(), TokenListenerError> {
176        let name = if self.composite_level > 0 {
177            &composite_token.name
178        } else {
179            &field_token.name
180        };
181        self.named_scope.push_back(name.to_owned());
182        self.composite_level += 1;
183        // self.named_scope.push_back(format!("{}", field_token.name)); //TODO
184        Ok(())
185    }
186
187    fn on_end_composite(
188        &mut self,
189        field_token: &Token,
190        tokens: &[Token],
191    ) -> Result<(), TokenListenerError> {
192        self.composite_level -= 1;
193        let c = self.named_scope.pop_back();
194        println!("-<- Composite {c:?}");
195        debug_assert!(c.is_some());
196        Ok(())
197    }
198
199    fn on_group_header(
200        &mut self,
201        token: &Token,
202        num_in_group: usize,
203    ) -> Result<(), TokenListenerError> {
204        self.print_scope();
205        println!("{} Group Header : numInGroup={num_in_group}", token.name);
206        Ok(())
207    }
208
209    fn on_begin_group(
210        &mut self,
211        token: &Token,
212        group_index: usize,
213        num_in_group: usize,
214    ) -> Result<(), TokenListenerError> {
215        self.named_scope.push_back(format!("{}", token.name));
216        Ok(())
217    }
218
219    fn on_end_group(
220        &mut self,
221        token: &Token,
222        group_index: i32,
223        num_in_group: i32,
224    ) -> Result<(), TokenListenerError> {
225        self.named_scope.pop_back();
226        Ok(())
227    }
228
229    fn on_var_data(
230        &mut self,
231        field_token: &Token,
232        buffer: &[u8],
233        type_token: &Token,
234    ) -> Result<(), TokenListenerError> {
235        println!("VARDATA! {type_token:?}");
236        self.print_scope();
237        println!("{}='{:?}'", field_token.name, buffer);
238        // todo!()
239        Ok(())
240    }
241}
242
243impl MyListener {
244    fn print_scope(&self) {
245        for s in &self.named_scope {
246            print!("{s}.");
247        }
248    }
249
250    fn const_or_not_present_value<'t>(
251        &self,
252        type_token: &'t Token,
253        field_version: i32,
254        acting_version: i32,
255    ) -> &'t PrimitiveValue {
256        match &type_token.encoding.presence {
257            Presence::Constant => &type_token.encoding.const_value,
258            Presence::Optional if acting_version < field_version => {
259                type_token.encoding.applicable_null_value()
260            }
261            _ => &PrimitiveValue::None,
262        }
263    }
264
265    fn encoding_as_string(
266        &self,
267        type_token: &Token,
268        buffer: &[u8],
269        field_version: i32,
270        acting_version: i32,
271    ) {
272        let v = self.const_or_not_present_value(type_token, field_version, acting_version);
273        if v.is_none() {}
274    }
Source

pub fn primitive_type(&self) -> PrimitiveType

Source

pub fn to_string(&self) -> Cow<'_, str>

Examples found in repository?
examples/dec.rs (line 120)
100    fn on_enum(
101        &mut self,
102        field_token: &Token,
103        enum_token: &Token,
104        buffer: &[u8],
105        tokens: &[Token],
106        acting_version: i32,
107    ) -> Result<(), TokenListenerError> {
108        // println!("begin enum {field_token:?}");
109        // println!("      enum {enum_token:?}");
110        // println!(
111        //     "~~> ENUM {:?}",
112        //     tokens
113        //         .iter()
114        //         .map(|x| (&x.signal, &x.name))
115        //         .collect::<Vec<_>>()
116        // );
117        let value = PrimitiveValue::new(enum_token.encoding.primitive_type, buffer);
118        let mut name: Cow<str> = "???".into();
119        if matches!(field_token.encoding.presence, Presence::Constant) {
120            name = field_token.encoding.const_value.to_string()[enum_token.name.len() + 1..]
121                .to_string()
122                .into();
123        } else {
124            for t in tokens {
125                // println!("{value:?} <> {:?} | {t:?}", t.encoding.const_value);
126                if value == t.encoding.const_value {
127                    name = (&t.name).into();
128                    break;
129                }
130            }
131        }
132        self.print_scope();
133        if self.composite_level > 0 {
134            print!("{}=", enum_token.name);
135        } else {
136            print!("{}=", field_token.name);
137        }
138        println!("{name} : {value:?}");
139
140        Ok(())
141    }

Trait Implementations§

Source§

impl Debug for PrimitiveValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for PrimitiveValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PrimitiveValue

Source§

fn eq(&self, other: &PrimitiveValue) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&PrimitiveValue> for u64

Source§

type Error = TryFromPrimitiveValueIntError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &PrimitiveValue) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<PrimitiveValue> for u64

Source§

type Error = TryFromPrimitiveValueIntError

The type returned in the event of a conversion error.
Source§

fn try_from(value: PrimitiveValue) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl StructuralPartialEq for PrimitiveValue

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.