1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use std::io::{Read, Write};
use crate::{
common::traits::private::Sealed,
v0::{
config::Config,
metadata::{
error::{MetadataRecordReadError, MetadataRecordWriteError},
traits::MetadataRecordObj,
MetadataRecord,
},
raw::VariableLengthEnum,
tokens::{error::MetadataTokenError, MetadataToken},
traits::{ReadFrom, WriteTo},
},
};
/// Formula variable name [metadata record](https://github.com/jiricekcz/fef-specification/blob/main/metadata/keys/Variable%20Name.md).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VariableNameMetadataRecordObj {
name: String,
variable_identifier: VariableLengthEnum,
}
impl VariableNameMetadataRecordObj {
/// Creates a new variable name metadata record.
///
/// # Example
///
/// Creating a metadata record setting the name of variable number `1` to `"x"`:
/// ```rust
/// # use fef::v0::metadata::VariableNameMetadataRecordObj;
/// # use fef::v0::raw::VariableLengthEnum;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let variable_identifier = VariableLengthEnum::from(1);
/// let record = VariableNameMetadataRecordObj::new("x".to_string(), variable_identifier.clone());
///
/// assert_eq!(record.name(), "x");
/// assert_eq!(record.variable_identifier(), &variable_identifier);
/// # Ok(())
/// # }
pub fn new(name: String, variable_identifier: VariableLengthEnum) -> Self {
Self {
name,
variable_identifier,
}
}
/// Returns the name of the variable to which this metadata record refers.
pub fn name(&self) -> &str {
&self.name
}
/// Returns the identifier of the variable to which this metadata record refers.
pub fn variable_identifier(&self) -> &VariableLengthEnum {
&self.variable_identifier
}
}
impl Sealed for VariableNameMetadataRecordObj {}
impl MetadataRecordObj for VariableNameMetadataRecordObj {
fn token(&self) -> Result<MetadataToken, MetadataTokenError> {
Ok(MetadataToken::VariableName)
}
fn byte_length(&self) -> usize {
let string_length = self.name.len();
string_length
+ VariableLengthEnum::min_byte_length_of_usize(string_length)
+ self.variable_identifier.min_byte_length()
}
}
impl<R: ?Sized + Read> ReadFrom<R> for VariableNameMetadataRecordObj {
type ReadError = MetadataRecordReadError;
/// Reads a variable name metadata record from a reader.
///
/// # Example
/// ```rust
/// # use fef::v0::metadata::VariableNameMetadataRecordObj;
/// # use fef::v0::traits::ReadFrom;
/// # use fef::v0::config::DEFAULT_CONFIG;
/// # use fef::v0::raw::VariableLengthEnum;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let data: Vec<u8> = vec![
/// 0x03, // Length of the record
/// 0x01, // Variable identifier
/// 0x01, // Length of the string
/// b'x', // Name
/// ];
/// let mut reader = &mut data.as_slice();
/// let record = VariableNameMetadataRecordObj::read_from(&mut reader, &DEFAULT_CONFIG)?;
/// assert_eq!(record.name(), "x");
/// assert_eq!(record.variable_identifier(), &VariableLengthEnum::from(1));
/// # Ok(())
/// # }
fn read_from<C: ?Sized + Config>(
reader: &mut R,
configuration: &C,
) -> Result<Self, Self::ReadError> {
let full_length: usize =
VariableLengthEnum::read_from(reader, configuration)?.try_into()?;
let mut reserved_part = reader.take(full_length as u64);
let variable_identifier = VariableLengthEnum::read_from(&mut reserved_part, configuration)?;
let name = String::read_from(&mut reserved_part, configuration)?;
let mut buf = Vec::new();
reserved_part.read_to_end(&mut buf)?;
drop(buf);
Ok(Self::new(name, variable_identifier))
}
}
impl<W: ?Sized + Write> WriteTo<W> for VariableNameMetadataRecordObj {
type WriteError = MetadataRecordWriteError;
/// Writes the variable name metadata record to a writer.
///
/// # Example
///
/// Writing a metadata record setting the name of variable number `1` to `"x"`:
/// ```rust
/// # use fef::v0::metadata::VariableNameMetadataRecordObj;
/// # use fef::v0::raw::VariableLengthEnum;
/// # use fef::v0::config::DEFAULT_CONFIG;
/// # use fef::v0::traits::WriteTo;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let variable_identifier = VariableLengthEnum::from(1);
/// let record = VariableNameMetadataRecordObj::new("x".to_string(), variable_identifier.clone());
///
/// let mut writer: Vec<u8> = Vec::new();
/// record.write_to(&mut writer, &DEFAULT_CONFIG)?;
///
/// let expected_result: Vec<u8> = vec![
/// 0x03, // Length of the record
/// 0x01, // Variable identifier
/// 0x01, // Length of the string
/// b'x', // Name
/// ];
/// assert_eq!(writer, expected_result);
/// # Ok(())
/// # }
fn write_to<C: ?Sized + Config>(
&self,
writer: &mut W,
configuration: &C,
) -> Result<(), Self::WriteError> {
let byte_length_enum = VariableLengthEnum::from(self.byte_length());
byte_length_enum.write_to(writer, configuration)?;
self.variable_identifier.write_to(writer, configuration)?;
self.name.write_to(writer, configuration)?;
Ok(())
}
}
impl Into<MetadataRecord> for VariableNameMetadataRecordObj {
fn into(self) -> MetadataRecord {
MetadataRecord::VariableName(self)
}
}