MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / parse_hop_range

Function parse_hop_range

nodedb/src/engine/graph/pattern/compiler/bindings.rs:175–213  ·  view source on GitHub ↗

Parse hop range: `1..3`, `2`, `..5`, `1..`.

(s: &str)

Source from the content-addressed store, hash-verified

173
174/// Parse hop range: `1..3`, `2`, `..5`, `1..`.
175pub(super) fn parse_hop_range(s: &str) -> crate::Result<(usize, usize)> {
176 if s.is_empty() {
177 return Ok((1, 1));
178 }
179
180 if let Some(dots) = s.find("..") {
181 let min_str = s[..dots].trim();
182 let max_str = s[dots + 2..].trim();
183
184 let min = if min_str.is_empty() {
185 1
186 } else {
187 min_str.parse().map_err(|_| crate::Error::BadRequest {
188 detail: format!("invalid min hops: '{min_str}'"),
189 })?
190 };
191
192 let max = if max_str.is_empty() {
193 10
194 } else {
195 max_str.parse().map_err(|_| crate::Error::BadRequest {
196 detail: format!("invalid max hops: '{max_str}'"),
197 })?
198 };
199
200 if min > max {
201 return Err(crate::Error::BadRequest {
202 detail: format!("min hops ({min}) > max hops ({max})"),
203 });
204 }
205
206 Ok((min, max))
207 } else {
208 let n: usize = s.parse().map_err(|_| crate::Error::BadRequest {
209 detail: format!("invalid hop count: '{s}'"),
210 })?;
211 Ok((n, n))
212 }
213}

Callers 1

parse_edge_innerFunction · 0.85

Calls 3

is_emptyMethod · 0.45
findMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected