use serde::{Deserialize, Serialize};
use super::{Link, LinkTarget};
use crate::model::keys::Label;
pub fn split_store(text: &str) -> (Option<&str>, &str) {
match text.split_once(':') {
Some((store, rest)) => (Some(store), rest),
None => (None, text),
}
}
impl<'a> Label<'a> {
pub fn parse(rest: &'a str) -> Self {
let (head, fragment) = match rest.split_once('#') {
Some((head, fragment)) => (head, Some(fragment)),
None => (rest, None),
};
let (store, id) = split_store(head);
Label {
store,
id: (!id.is_empty()).then_some(id),
fragment,
}
}
pub fn of(target: &'a LinkTarget) -> Option<Self> {
match target {
LinkTarget::Host(rest) => Some(Label::parse(rest)),
_ => None,
}
}
}
pub fn label_parts(label: &str) -> Option<Label<'_>> {
label.strip_prefix("sokf:").map(Label::parse)
}
pub fn sokf_label(link: &Link) -> Option<String> {
match &link.target {
LinkTarget::Host(rest) => Some(format!("sokf:{rest}")),
_ => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum IncludeTarget {
Document {
store: Option<String>,
id: String,
fragment: Option<String>,
},
File {
path: String,
region: Option<String>,
},
}
impl IncludeTarget {
pub fn as_written(&self) -> String {
match self {
IncludeTarget::Document {
store,
id,
fragment,
} => format!(
"{}{id}{}",
store
.as_deref()
.map(|s| format!("{s}:"))
.unwrap_or_default(),
fragment
.as_deref()
.map(|f| format!("#{f}"))
.unwrap_or_default()
),
IncludeTarget::File { path, region } => match region {
Some(r) => format!("{path}#{r}"),
None => path.clone(),
},
}
}
pub fn parse(text: &str) -> Self {
let (head, tail) = match text.split_once('#') {
Some((h, t)) => (h.to_string(), Some(t.to_string())),
None => (text.to_string(), None),
};
if text.starts_with('/') {
IncludeTarget::File {
path: head,
region: tail,
}
} else {
let (store, id) = split_store(&head);
IncludeTarget::Document {
store: store.map(str::to_string),
id: id.to_string(),
fragment: tail,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn labels_split_into_their_parts() {
let l = |s| Label::parse(s);
assert_eq!(
l("config"),
Label {
store: None,
id: Some("config"),
fragment: None
}
);
assert_eq!(
l("req#CLI-1_AC-2"),
Label {
store: None,
id: Some("req"),
fragment: Some("CLI-1_AC-2")
}
);
assert_eq!(
l("#K"),
Label {
store: None,
id: None,
fragment: Some("K")
}
);
assert_eq!(
l("acme/common:#K"),
Label {
store: Some("acme/common"),
id: None,
fragment: Some("K")
}
);
assert_eq!(label_parts("sokf:a#b"), Some(l("a#b")));
assert_eq!(label_parts("a#b"), None);
assert_eq!(split_store("glossary"), (None, "glossary"));
assert_eq!(
split_store("sokf-spec:glossary"),
(Some("sokf-spec"), "glossary")
);
assert_eq!(split_store("acme/common:"), (Some("acme/common"), ""));
}
#[test]
fn a_label_is_rebuilt_as_written() {
let link = |rest: &str| Link {
target: LinkTarget::Host(rest.into()),
line: 1,
is_image: false,
end_line: 1,
inline: None,
};
assert_eq!(sokf_label(&link("a")).unwrap(), "sokf:a");
assert_eq!(sokf_label(&link("a#b")).unwrap(), "sokf:a#b");
assert_eq!(sokf_label(&link("#K")).unwrap(), "sokf:#K");
let url = Link {
target: LinkTarget::Url("https://x".into()),
..link("a")
};
assert!(sokf_label(&url).is_none());
}
#[test]
fn include_targets() {
assert_eq!(IncludeTarget::parse("/a.rs#cli").as_written(), "/a.rs#cli");
assert_eq!(IncludeTarget::parse("/a.rs").as_written(), "/a.rs");
assert_eq!(
IncludeTarget::parse("doc-id"),
IncludeTarget::Document {
store: None,
id: "doc-id".into(),
fragment: None
}
);
assert_eq!(
IncludeTarget::parse("rates#q3"),
IncludeTarget::Document {
store: None,
id: "rates".into(),
fragment: Some("q3".into())
}
);
assert_eq!(
IncludeTarget::parse("flowers#"),
IncludeTarget::Document {
store: None,
id: "flowers".into(),
fragment: Some(String::new())
}
);
assert_eq!(IncludeTarget::parse("rates#q3").as_written(), "rates#q3");
assert_eq!(IncludeTarget::parse("flowers#").as_written(), "flowers#");
}
}