mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-24 01:46:31 +00:00
Keep StyledString markup-agnostic, push trait to methods
This commit is contained in:
parent
7ecf5f540b
commit
59d67e891c
@ -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>;
|
||||
}
|
||||
|
||||
type StyledHandle = OwningHandle<StringRef, Vec<Span<'static>>>;
|
||||
|
||||
/// A String that parses a markup language.
|
||||
pub struct StyledString<M> {
|
||||
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 })
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new styled string, parsing the given content.
|
||||
pub fn new<S>(content: S) -> Result<Self, M::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 content = Self::make_handle(content)?;
|
||||
Ok(StyledString {
|
||||
content,
|
||||
_phantom: PhantomData,
|
||||
let input = input.into();
|
||||
OwningHandle::try_new(StringRef::new(input), |input| {
|
||||
Self::parse(unsafe { &*input })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
content: StyledHandle,
|
||||
}
|
||||
|
||||
impl StyledString {
|
||||
|
||||
/// Creates a new styled string, parsing the given content.
|
||||
pub fn new<S, M>(content: S) -> Result<Self, M::Error>
|
||||
where
|
||||
S: Into<String>,
|
||||
M: Markup,
|
||||
{
|
||||
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(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user