use std::fmt;
use crate::tor::control_client::{commands::TorCommand, error::TorClientError, response::ResponseLine};
pub struct DelOnion<'a> {
service_id: &'a str,
}
impl<'a> DelOnion<'a> {
pub fn new(service_id: &'a str) -> Self {
Self { service_id }
}
}
impl TorCommand for DelOnion<'_> {
type Error = TorClientError;
type Output = ();
fn to_command_string(&self) -> Result<String, Self::Error> {
Ok(format!("DEL_ONION {}", self.service_id))
}
fn parse_responses(&self, mut responses: Vec<ResponseLine>) -> Result<Self::Output, Self::Error> {
let last_response = responses.pop().ok_or(TorClientError::UnexpectedEof)?;
if let Some(err) = last_response.err() {
return Err(TorClientError::TorCommandFailed(err.to_owned()));
}
Ok(())
}
}
impl fmt::Display for DelOnion<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DEL_ONION (ServiceId = {})", self.service_id)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn to_command_string() {
let command = DelOnion::new("some-random-key");
assert_eq!(command.to_command_string().unwrap(), "DEL_ONION some-random-key");
}
}