summaryrefslogtreecommitdiff
path: root/src/string_utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/string_utils.rs')
-rw-r--r--src/string_utils.rs67
1 files changed, 66 insertions, 1 deletions
diff --git a/src/string_utils.rs b/src/string_utils.rs
index b23c349..2061110 100644
--- a/src/string_utils.rs
+++ b/src/string_utils.rs
@@ -1,6 +1,71 @@
+use crate::*;
+
+
+#[derive(Clone)]
+pub struct Name {
+ raw: String,
+}
+
+impl Name {
+ /// Preserve markdown syntax, return raw string.
+ pub fn raw(&self) -> String {
+ self.raw.clone()
+ }
+ /// Parse markdown syntax, return styled line.
+ pub fn styled(&self) -> Line {
+ Line::from_str(&self.raw)
+ }
+ /// Strip out markdown syntax, return plain text.
+ pub fn plain(&self) -> String {
+ self.styled().to_string()
+ }
+ /// Strip out markdown syntax, return url-safe text.
+ pub fn slug(&self) -> String {
+ to_slug(&self.plain())
+ }
+}
+
+impl std::fmt::Display for Name {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ self.raw.fmt(f)
+ }
+}
+
+impl std::fmt::Debug for Name {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ self.raw.fmt(f)
+ }
+}
+
+impl PartialEq for Name {
+ fn eq(&self, other: &string_utils::Name) -> bool {
+ self.slug() == other.slug()
+ }
+}
+
+impl Eq for Name {}
+impl std::hash::Hash for Name {
+ fn hash<H>(&self, hasher: &mut H) where H: std::hash::Hasher {
+ self.slug().hash(hasher)
+ }
+}
+
+impl From<String> for Name {
+ fn from(raw: String) -> Self {
+ Self { raw }
+ }
+}
+
+impl From<&str> for Name {
+ fn from(raw: &str) -> Self {
+ Self { raw: raw.to_string() }
+ }
+}
+
+
// Turn a string into a tidy URL slug.
-pub fn make_url_safe(text: &str) -> String {
+pub fn to_slug(text: &str) -> String {
text.to_ascii_lowercase().chars().filter_map(|c|
if c.is_alphanumeric() || "-_~.+/#".contains(c) { Some(c) }
else if c == ' ' { Some('-') }