Compare commits

...

2 Commits

Author SHA1 Message Date
cel 🌸 1d831b4c4e add write function and remove todo Sink impl 2024-12-04 02:36:05 +00:00
cel 🌸 692a9ebce8 create IntoContent trait and add Comment ContentBuilder type 2024-12-04 02:35:42 +00:00
2 changed files with 18 additions and 32 deletions

View File

@ -3,7 +3,7 @@
#![feature(drain_filter)] #![feature(drain_filter)]
use std::{ use std::{
collections::{HashMap, HashSet, VecDeque}, collections::{vec_deque, HashMap, HashSet, VecDeque},
convert::Infallible, convert::Infallible,
str::FromStr, str::FromStr,
}; };
@ -580,6 +580,10 @@ impl ElementBuilder {
} }
pub trait IntoContent { pub trait IntoContent {
fn into_content(&self) -> Content {
self.builder().build().unwrap()
}
fn builder(&self) -> ContentBuilder; fn builder(&self) -> ContentBuilder;
} }
@ -592,9 +596,14 @@ where
} }
} }
pub trait FromContent: Sized {
fn from_content(content: Content) -> DeserializeResult<Self>;
}
pub enum ContentBuilder { pub enum ContentBuilder {
Element(ElementBuilder), Element(ElementBuilder),
Text(String), Text(String),
Comment(String),
} }
impl ContentBuilder { impl ContentBuilder {
@ -604,6 +613,7 @@ impl ContentBuilder {
Ok(Content::Element(element_builder.build()?)) Ok(Content::Element(element_builder.build()?))
} }
ContentBuilder::Text(text) => Ok(Content::Text(text.to_string())), ContentBuilder::Text(text) => Ok(Content::Text(text.to_string())),
ContentBuilder::Comment(s) => Ok(Content::Comment(s.to_string())),
} }
} }
} }

View File

@ -1,4 +1,4 @@
use std::{collections::HashSet, str::FromStr}; use std::{collections::HashSet, pin::pin, str::FromStr};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use futures::Sink; use futures::Sink;
@ -6,7 +6,7 @@ use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::{ use crate::{
declaration::{Declaration, VersionInfo}, declaration::{Declaration, VersionInfo},
element::{escape_str, Content, Element, IntoElement, Name, NamespaceDeclaration}, element::{escape_str, Content, Element, IntoContent, IntoElement, Name, NamespaceDeclaration},
error::Error, error::Error,
xml::{self, composers::Composer, parsers_complete::Parser, ETag, XMLDecl}, xml::{self, composers::Composer, parsers_complete::Parser, ETag, XMLDecl},
Result, XMLNS_NS, XML_NS, Result, XMLNS_NS, XML_NS,
@ -79,6 +79,11 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> {
Ok(()) Ok(())
} }
pub async fn write(&mut self, into_content: &impl IntoContent) -> Result<()> {
let content = into_content.into_content();
Ok(self.write_content(&content).await?)
}
#[async_recursion] #[async_recursion]
pub async fn write_element(&mut self, element: &Element) -> Result<()> { pub async fn write_element(&mut self, element: &Element) -> Result<()> {
if element.content.is_empty() { if element.content.is_empty() {
@ -357,35 +362,6 @@ impl<W: AsyncWrite + Unpin + Send> Writer<W> {
} }
} }
impl<W: AsyncWrite, E: Into<Element>> Sink<E> for Writer<W> {
type Error = Error;
fn poll_ready(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<()>> {
todo!()
}
fn start_send(self: std::pin::Pin<&mut Self>, item: E) -> Result<()> {
todo!()
}
fn poll_flush(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<()>> {
todo!()
}
fn poll_close(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<()>> {
todo!()
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{ use crate::{