aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-16 01:38:20 +0000
committerFuwn <[email protected]>2023-04-16 01:38:20 +0000
commitb52ad3f74765d46649c9a820a79b5e940c2f4aa9 (patch)
tree01dc990fb4bc11245b2debfef64262e5d9a9da2c
parentdeps(anyhow): bump from 1.0.57 to 1.0.70 (diff)
downloadgerm-b52ad3f74765d46649c9a820a79b5e940c2f4aa9.tar.xz
germ-b52ad3f74765d46649c9a820a79b5e940c2f4aa9.zip
feat(ast): clean up from_*s for ast
-rw-r--r--crates/germ/src/ast/container.rs30
-rw-r--r--crates/germ/src/convert.rs2
2 files changed, 29 insertions, 3 deletions
diff --git a/crates/germ/src/ast/container.rs b/crates/germ/src/ast/container.rs
index 6cbc677..acb8894 100644
--- a/crates/germ/src/ast/container.rs
+++ b/crates/germ/src/ast/container.rs
@@ -31,7 +31,7 @@ pub struct Ast {
}
impl Ast {
- /// Build an AST tree from Gemtext.
+ /// Build an AST tree from Gemtext
///
/// # Example
///
@@ -39,10 +39,36 @@ impl Ast {
/// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#);
/// ```
#[must_use]
- pub fn from_string(source: &str) -> Self {
+ pub fn from_owned(value: &(impl AsRef<str> + ?Sized)) -> Self {
+ Self::from_value(value.as_ref())
+ }
+
+ /// Build an AST tree from Gemtext
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// let _ = germ::ast::Ast::from_string(r#"=> gemini://gem.rest/ GemRest"#);
+ /// ```
+ #[must_use]
+ #[allow(clippy::needless_pass_by_value)]
+ pub fn from_string(value: (impl Into<String> + ?Sized)) -> Self {
+ Self::from_value(&value.into())
+ }
+
+ /// Build an AST tree from a value
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// let _ = germ::ast::Ast::from_value(r#"=> gemini://gem.rest/ GemRest"#);
+ /// ```
+ #[must_use]
+ pub fn from_value(value: &(impl ToString + ?Sized)) -> Self {
let mut ast = vec![];
let mut in_preformatted = false;
let mut in_list = false;
+ let source = value.to_string();
let mut lines = source.lines();
// Iterate over all lines in the Gemtext `source`
diff --git a/crates/germ/src/convert.rs b/crates/germ/src/convert.rs
index 4c5f0c8..bfab16d 100644
--- a/crates/germ/src/convert.rs
+++ b/crates/germ/src/convert.rs
@@ -69,5 +69,5 @@ pub fn from_ast(source: &Ast, target: &Target) -> String {
/// ```
#[must_use]
pub fn from_string(source: &str, target: &Target) -> String {
- from_ast(&Ast::from_string(source), target)
+ from_ast(&Ast::from_owned(&source), target)
}