Keep StyledString markup-agnostic, push trait to methods
@@ -19,50 +19,54 @@ pub trait Markup {
/// Parses text and return the styled spans.
fn parse<'a>(input: &'a str) -> Result<Vec<Span<'a>>, Self::Error>;
+
+ /// Returns a string and its parsed spans.
+ ///
+ /// Generates a self-borrowing struct containing the source string, as well
+ /// as the styled spans borrowing this string.
+ fn make_handle<S>(input: S) -> Result<StyledHandle, Self::Error>
+ where
+ S: Into<String>,
+ {
+ let input = input.into();
+ OwningHandle::try_new(StringRef::new(input), |input| {
+ Self::parse(unsafe { &*input })
+ })
+ }
}
-type StyledHandle = OwningHandle<StringRef, Vec<Span<'static>>>;
+/// Holds both parsed spans, and the input string they borrow.
+///
+/// This is used to pass around a parsed string.
+pub type StyledHandle = OwningHandle<StringRef, Vec<Span<'static>>>;
/// A String that parses a markup language.
-pub struct StyledString<M> {
+pub struct StyledString {
content: StyledHandle,
- _phantom: PhantomData<M>,
}
-impl<M> StyledString<M>
-where
- M: Markup,
-{
- fn make_handle<S: Into<String>>(
- content: S
- ) -> Result<StyledHandle, M::Error> {
- let content = content.into();
- OwningHandle::try_new(StringRef::new(content), |input| {
- M::parse(unsafe { &*input })
- })
- }
+impl StyledString {
/// Creates a new styled string, parsing the given content.
- pub fn new<S>(content: S) -> Result<Self, M::Error>
+ pub fn new<S, M>(content: S) -> Result<Self, M::Error>
where
S: Into<String>,
+ M: Markup,
{
- let content = Self::make_handle(content)?;
- Ok(StyledString {
- content,
- _phantom: PhantomData,
- })
+ let content = M::make_handle(content)?;
+ Ok(StyledString { content })
}
/// Sets the content of this string.
///
/// The content will be parsed; if an error is found,
/// it will be returned here (and the content will be unchanged).
- pub fn set_content<S>(&mut self, content: S) -> Result<(), M::Error>
+ pub fn set_content<S, M>(&mut self, content: S) -> Result<(), M::Error>
where
S: Into<String>,
+ M: Markup,
{
- self.content = Self::make_handle(content)?;
+ self.content = M::make_handle(content)?;
Ok(())
}
