Skip to content

Commit abb86a7

Browse files
Merge pull request #18 from cristian-recoseanu/get-sequence-value-and-length
Implement get sequence item and length methods (1p3 and 1p7)
2 parents 9272193 + ac1e4de commit abb86a7

File tree

3 files changed

+456
-8
lines changed

3 files changed

+456
-8
lines changed

src/nc_block.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,127 @@ impl NcMember for NcBlock {
9999
) -> (Option<String>, Option<Value>, NcMethodStatus) {
100100
if oid == self.base.oid {
101101
match (method_id.level, method_id.index) {
102+
(1, 3) => {
103+
match args.as_object() {
104+
Some(args_obj) => {
105+
match args_obj.get("id") {
106+
Some(id_val) => {
107+
if let Some(id_obj) = id_val.as_object() {
108+
let level = id_obj
109+
.get("level")
110+
.and_then(|v| v.as_u64())
111+
.map(|v| v as u32);
112+
let index = id_obj
113+
.get("index")
114+
.and_then(|v| v.as_u64())
115+
.map(|v| v as u32);
116+
117+
if let (Some(2), Some(2)) = (level, index) {
118+
// Handle members property (2p2)
119+
let index = match args_obj
120+
.get("index")
121+
.and_then(|v| v.as_u64())
122+
{
123+
Some(idx) => idx as usize,
124+
None => {
125+
return (
126+
Some("Invalid index argument".to_string()),
127+
None,
128+
NcMethodStatus::ParameterError,
129+
);
130+
}
131+
};
132+
133+
let members = self.generate_members_descriptors();
134+
135+
// Check if index is within bounds
136+
if index >= members.len() {
137+
return (
138+
Some(format!(
139+
"Index {} out of bounds for sequence",
140+
index
141+
)),
142+
None,
143+
NcMethodStatus::IndexOutOfBounds,
144+
);
145+
}
146+
147+
if let Some(member) = members.get(index) {
148+
return (
149+
None,
150+
Some(json!(member)),
151+
NcMethodStatus::Ok,
152+
);
153+
}
154+
}
155+
}
156+
}
157+
None => {
158+
return (
159+
Some("Missing 'id' argument".to_string()),
160+
None,
161+
NcMethodStatus::ParameterError,
162+
);
163+
}
164+
}
165+
}
166+
None => {
167+
return (
168+
Some("Invalid arguments".to_string()),
169+
None,
170+
NcMethodStatus::ParameterError,
171+
);
172+
}
173+
}
174+
// If not the members property, delegate to base class
175+
self.base.invoke_method(oid, method_id, args)
176+
}
177+
(1, 7) => {
178+
match args.as_object() {
179+
Some(args_obj) => {
180+
match args_obj.get("id") {
181+
Some(id_val) => {
182+
if let Some(id_obj) = id_val.as_object() {
183+
let level = id_obj
184+
.get("level")
185+
.and_then(|v| v.as_u64())
186+
.map(|v| v as u32);
187+
let index = id_obj
188+
.get("index")
189+
.and_then(|v| v.as_u64())
190+
.map(|v| v as u32);
191+
192+
if let (Some(2), Some(2)) = (level, index) {
193+
// Handle members property (2p2)
194+
let members = self.generate_members_descriptors();
195+
return (
196+
None,
197+
Some(json!(members.len())),
198+
NcMethodStatus::Ok,
199+
);
200+
}
201+
}
202+
}
203+
None => {
204+
return (
205+
Some("Invalid id argument".to_string()),
206+
None,
207+
NcMethodStatus::ParameterError,
208+
);
209+
}
210+
}
211+
}
212+
None => {
213+
return (
214+
Some("Invalid arguments".to_string()),
215+
None,
216+
NcMethodStatus::ParameterError,
217+
);
218+
}
219+
}
220+
// If not the members property, delegate to base class
221+
self.base.invoke_method(oid, method_id, args)
222+
}
102223
(2, 1) => (
103224
None,
104225
Some(json!(self.get_member_descriptors(args))),

src/nc_class_manager.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,151 @@ impl NcMember for NcClassManager {
8989
) -> (Option<String>, Option<Value>, NcMethodStatus) {
9090
if oid == self.base.get_oid() {
9191
match (method_id.level, method_id.index) {
92+
(1, 3) => {
93+
match args.as_object() {
94+
Some(args_obj) => {
95+
let index = match args_obj.get("index").and_then(|v| v.as_u64()) {
96+
Some(idx) => idx as usize,
97+
None => {
98+
return (
99+
Some("Invalid index argument".to_string()),
100+
None,
101+
NcMethodStatus::ParameterError,
102+
);
103+
}
104+
};
105+
106+
match args_obj.get("id").and_then(|v| v.as_object()) {
107+
Some(id_obj) => {
108+
let level = id_obj
109+
.get("level")
110+
.and_then(|v| v.as_u64())
111+
.map(|v| v as u32);
112+
let index_field = id_obj
113+
.get("index")
114+
.and_then(|v| v.as_u64())
115+
.map(|v| v as u32);
116+
117+
match (level, index_field) {
118+
(Some(3), Some(1)) => {
119+
// Handle controlClasses (3p1)
120+
let sequence: Vec<NcClassDescriptor> = self
121+
.control_classes_register
122+
.values()
123+
.cloned()
124+
.collect();
125+
126+
// Check if index is within bounds
127+
if index >= sequence.len() {
128+
return (
129+
Some(format!(
130+
"Index {} out of bounds for controlClasses sequence",
131+
index
132+
)),
133+
None,
134+
NcMethodStatus::IndexOutOfBounds,
135+
);
136+
}
137+
138+
(
139+
None,
140+
Some(json!(sequence[index].clone())),
141+
NcMethodStatus::Ok,
142+
)
143+
}
144+
(Some(3), Some(2)) => {
145+
// Handle datatypes (3p2)
146+
let sequence: Vec<NcAnyDatatypeDescriptor> = self
147+
.data_types_register
148+
.values()
149+
.cloned()
150+
.collect();
151+
152+
// Check if index is within bounds
153+
if index >= sequence.len() {
154+
return (
155+
Some(format!(
156+
"Index {} out of bounds for datatypes sequence",
157+
index
158+
)),
159+
None,
160+
NcMethodStatus::IndexOutOfBounds,
161+
);
162+
}
163+
164+
(
165+
None,
166+
Some(json!(sequence[index].clone())),
167+
NcMethodStatus::Ok,
168+
)
169+
}
170+
_ => (
171+
Some("Invalid id argument".to_string()),
172+
None,
173+
NcMethodStatus::ParameterError,
174+
),
175+
}
176+
}
177+
None => (
178+
Some("Invalid id argument".to_string()),
179+
None,
180+
NcMethodStatus::ParameterError,
181+
),
182+
}
183+
}
184+
None => (
185+
Some("Invalid arguments".to_string()),
186+
None,
187+
NcMethodStatus::ParameterError,
188+
),
189+
}
190+
}
191+
(1, 7) => {
192+
match args.as_object() {
193+
Some(args_obj) => {
194+
match args_obj.get("id").and_then(|v| v.as_object()) {
195+
Some(id_obj) => {
196+
let level = id_obj
197+
.get("level")
198+
.and_then(|v| v.as_u64())
199+
.map(|v| v as u32);
200+
let index = id_obj
201+
.get("index")
202+
.and_then(|v| v.as_u64())
203+
.map(|v| v as u32);
204+
205+
match (level, index) {
206+
(Some(3), Some(1)) => {
207+
// Return length of controlClasses (3p1)
208+
let length = self.control_classes_register.len() as u64;
209+
(None, Some(json!(length)), NcMethodStatus::Ok)
210+
}
211+
(Some(3), Some(2)) => {
212+
// Return length of datatypes (3p2)
213+
let length = self.data_types_register.len() as u64;
214+
(None, Some(json!(length)), NcMethodStatus::Ok)
215+
}
216+
_ => (
217+
Some("Invalid id argument".to_string()),
218+
None,
219+
NcMethodStatus::ParameterError,
220+
),
221+
}
222+
}
223+
None => (
224+
Some("Invalid id argument".to_string()),
225+
None,
226+
NcMethodStatus::ParameterError,
227+
),
228+
}
229+
}
230+
None => (
231+
Some("Invalid arguments".to_string()),
232+
None,
233+
NcMethodStatus::ParameterError,
234+
),
235+
}
236+
}
92237
(3, 1) => {
93238
let class_id = args.get("classId").and_then(|v| v.as_array()).map(|arr| {
94239
arr.iter()

0 commit comments

Comments
 (0)