useblazingly_json::Value;pub(crate)fnpage_offset(args:&Value)->Result<usize, String>{letSome(cursor)= args.get("cursor").and_then(Value::as_str)else{returnOk(0);};letSome(offset)= cursor.strip_prefix("v1:")else{returnErr("cursor format is invalid; expected v1:<offset>".to_owned());};
offset
.parse::<usize>().map_err(|_|"cursor offset is invalid".to_owned())}#[cfg(test)]modtests{usesuper::page_offset;useblazingly_json::json;#[test]fncursor_offset_parses_or_names_the_defect(){assert_eq!(page_offset(&json!({})).unwrap(),0);assert_eq!(page_offset(&json!({"cursor":"v1:12"})).unwrap(),12);assert!(page_offset(&json!({"cursor":"12"})).unwrap_err().contains("v1:"));assert!(page_offset(&json!({"cursor":"v1:x"})).unwrap_err().contains("offset"));}}