forked from lavina/lavina
				
			Sketch interfaces
This commit is contained in:
		
							parent
							
								
									cebe354179
								
							
						
					
					
						commit
						9f3a97a1e7
					
				|  | @ -74,8 +74,8 @@ impl Jid { | |||
| pub struct BindRequest(pub Resource); | ||||
| 
 | ||||
| impl FromXmlTag for BindRequest { | ||||
|     const NS: &'static str = XMLNS; | ||||
|     const NAME: &'static str = "bind"; | ||||
|     const NS: &'static str = XMLNS; | ||||
| } | ||||
| 
 | ||||
| impl FromXml for BindRequest { | ||||
|  |  | |||
|  | @ -3,6 +3,7 @@ | |||
| pub mod bind; | ||||
| pub mod client; | ||||
| pub mod disco; | ||||
| pub mod mam; | ||||
| pub mod muc; | ||||
| mod prelude; | ||||
| pub mod roster; | ||||
|  |  | |||
|  | @ -0,0 +1,71 @@ | |||
| use anyhow::{anyhow, Result}; | ||||
| use quick_xml::events::Event; | ||||
| use quick_xml::name::{Namespace, ResolveResult}; | ||||
| 
 | ||||
| use crate::xml::*; | ||||
| 
 | ||||
| pub const XMLNS: &'static str = "urn:xmpp:mam:2"; | ||||
| 
 | ||||
| #[derive(PartialEq, Eq, Debug, Clone)] | ||||
| pub struct MessageArchiveRequest; | ||||
| 
 | ||||
| impl FromXmlTag for MessageArchiveRequest { | ||||
|     const NAME: &'static str = "query"; | ||||
|     const NS: &'static str = XMLNS; | ||||
| } | ||||
| 
 | ||||
| impl FromXml for MessageArchiveRequest { | ||||
|     type P = impl Parser<Output = Result<Self>>; | ||||
| 
 | ||||
|     fn parse() -> Self::P { | ||||
|         |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result<Self> { | ||||
|             let Event::Start(bytes) = event else { | ||||
|                 return Err(anyhow!("Unexpected XML event: {event:?}")); | ||||
|             }; | ||||
|             if bytes.name().0 != MessageArchiveRequest::NAME.as_bytes() { | ||||
|                 return Err(anyhow!("Unexpected XML tag: {:?}", bytes.name())); | ||||
|             } | ||||
|             let ResolveResult::Bound(Namespace(ns)) = namespace else { | ||||
|                 return Err(anyhow!("No namespace provided")); | ||||
|             }; | ||||
|             if ns != XMLNS.as_bytes() { | ||||
|                 return Err(anyhow!("Incorrect namespace")); | ||||
|             } | ||||
|             loop { | ||||
|                 let (namespace, event) = yield; | ||||
|                 match event { | ||||
|                     Event::End(bytes) if bytes.name().0 == MessageArchiveRequest::NAME.as_bytes() => { | ||||
|                         break; | ||||
|                     } | ||||
|                     _ => return Err(anyhow!("Unexpected XML event: {event:?}")), | ||||
|                 } | ||||
|             } | ||||
|             Ok(MessageArchiveRequest) | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl MessageArchiveRequest {} | ||||
| 
 | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|     use crate::client::{Iq, IqType}; | ||||
| 
 | ||||
|     #[test] | ||||
|     fn test_parse() { | ||||
|         let input = r#"<query xmlns="urn:xmpp:mam:2"></query>"#; | ||||
| 
 | ||||
|         let result: Iq<MessageArchiveRequest> = parse(input).unwrap(); | ||||
|         assert_eq!( | ||||
|             result, | ||||
|             Iq { | ||||
|                 from: None, | ||||
|                 id: "mam_1".to_string(), | ||||
|                 to: None, | ||||
|                 r#type: IqType::Get, | ||||
|                 body: MessageArchiveRequest, | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue