From ab2571f4481879abf81b33a17a2e400c5baec608 Mon Sep 17 00:00:00 2001 From: An Long Date: Tue, 8 Sep 2026 23:34:32 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Parse=20STAT=20lines=20in=20the?= =?UTF-8?q?=20ascii=20stats=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/protocol/ascii.rs | 13 ++++--------- tests/test_ascii.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/protocol/ascii.rs b/src/protocol/ascii.rs index 3516b3c..c205dd5 100644 --- a/src/protocol/ascii.rs +++ b/src/protocol/ascii.rs @@ -310,20 +310,15 @@ impl ProtocolTrait for AsciiProtocol { let mut stats: Stats = HashMap::new(); loop { let status = self.reader.read_line(|response| { - if response != END { + if response == END { return Ok(Loop::Break); } let s = MemcacheError::try_from(response)?; - if !s.starts_with("STAT") { + let stat: Vec<_> = s.trim_end_matches("\r\n").splitn(3, ' ').collect(); + if stat.len() < 3 || stat[0] != "STAT" { return Err(ServerError::BadResponse(Cow::Owned(s.into())))?; } - let stat: Vec<_> = s.trim_end_matches("\r\n").split(" ").collect(); - if stat.len() < 3 { - return Err(ServerError::BadResponse(Cow::Owned(s.into())).into()); - } - let key = stat[1]; - let value = s.trim_start_matches(format!("STAT {}", key).as_str()); - stats.insert(key.into(), value.into()); + stats.insert(stat[1].into(), stat[2].into()); Ok(Loop::Continue) })?; diff --git a/tests/test_ascii.rs b/tests/test_ascii.rs index ff5573f..6ea97f1 100644 --- a/tests/test_ascii.rs +++ b/tests/test_ascii.rs @@ -59,7 +59,14 @@ fn test_ascii() { assert_eq!(client.increment("ascii_counter", 100).unwrap(), 103); assert_eq!(client.decrement("ascii_counter", 3).unwrap(), 100); - client.stats().unwrap(); + let stats = client.stats().unwrap(); + assert_eq!(stats.len(), 1); + let stats = &stats[0].1; + assert_eq!(stats.get("version").unwrap(), &client.version().unwrap()[0].1); + assert!(stats.get("uptime").unwrap().parse::().is_ok()); + + let value: Option = client.get("ascii_foo").unwrap(); + assert_eq!(value, Some("bar".into())); } #[test]