use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hyperlink {
uri: Arc<str>,
parameters: Arc<[(String, String)]>,
}
impl Hyperlink {
pub fn new(uri: impl AsRef<str>) -> Self {
Self {
uri: uri.as_ref().into(),
parameters: Arc::from([]),
}
}
pub fn parameter(mut self, name: impl AsRef<str>, value: impl AsRef<str>) -> Self {
let mut parameters = self.parameters.to_vec();
parameters.push((name.as_ref().to_owned(), value.as_ref().to_owned()));
self.parameters = parameters.into();
self
}
pub fn uri(&self) -> &str {
&self.uri
}
pub fn parameters(&self) -> &[(String, String)] {
&self.parameters
}
}
impl From<&str> for Hyperlink {
fn from(uri: &str) -> Self {
Self::new(uri)
}
}
impl From<String> for Hyperlink {
fn from(uri: String) -> Self {
Self::new(uri)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construction_preserves_logical_hyperlink_data() {
let hyperlink = Hyperlink::new("https://example.com/a\u{1b}\\b\u{7}\u{9c}")
.parameter("i:d=;\u{9d}", "v:a=l;ue\n\u{9b}");
assert_eq!(hyperlink.uri(), "https://example.com/a\u{1b}\\b\u{7}\u{9c}");
assert_eq!(
hyperlink.parameters(),
&[("i:d=;\u{9d}".to_owned(), "v:a=l;ue\n\u{9b}".to_owned())]
);
}
}