#![allow(clippy::should_implement_trait)]
use crate::exceptions::{Error, Result};
use crate::xml::constants::{PKG_REL_NS, REL_NS};
use crate::xml::functions::Element;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RelationshipType {
Hyperlink,
Drawing,
Image,
}
impl RelationshipType {
pub fn as_str(self) -> &'static str {
match self {
RelationshipType::Hyperlink => "hyperlink",
RelationshipType::Drawing => "drawing",
RelationshipType::Image => "image",
}
}
pub fn uri(self) -> String {
format!("{REL_NS}/{}", self.as_str())
}
pub fn from_str(value: &str) -> Result<Self> {
Ok(match value {
"hyperlink" => RelationshipType::Hyperlink,
"drawing" => RelationshipType::Drawing,
"image" => RelationshipType::Image,
other => return Err(Error::Value(format!("Invalid relationship type {other}"))),
})
}
pub fn from_uri(uri: &str) -> Option<Self> {
let short = uri.strip_prefix(REL_NS)?.strip_prefix('/')?;
RelationshipType::from_str(short).ok()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Relationship {
pub relationship_type: RelationshipType,
pub target: Option<String>,
pub target_mode: Option<String>,
pub id: Option<String>,
}
impl Relationship {
pub fn new(relationship_type: RelationshipType, target: Option<&str>) -> Self {
Relationship {
relationship_type,
target: target.map(|t| t.to_string()),
target_mode: None,
id: None,
}
}
pub fn type_uri(&self) -> String {
self.relationship_type.uri()
}
pub fn to_document(&self) -> Element {
let mut root = Element::new(format!("{{{PKG_REL_NS}}}Relationships"));
let mut body = Element::new(format!("{{{PKG_REL_NS}}}Relationship"));
if let Some(id) = &self.id {
body.set("Id", id.clone());
}
body.set("Type", self.type_uri());
if let Some(target) = &self.target {
body.set("Target", target.clone());
}
if let Some(mode) = &self.target_mode {
body.set("TargetMode", mode.clone());
}
root.append(body);
root
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn valid_types_round_trip() {
for t in [
RelationshipType::Hyperlink,
RelationshipType::Drawing,
RelationshipType::Image,
] {
assert_eq!(RelationshipType::from_str(t.as_str()).unwrap(), t);
assert_eq!(RelationshipType::from_uri(&t.uri()), Some(t));
}
assert!(RelationshipType::from_str("bogus").is_err());
assert_eq!(RelationshipType::from_uri("http://example.com/x"), None);
}
#[test]
fn uri_shape() {
assert_eq!(
RelationshipType::Hyperlink.uri(),
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
);
}
#[test]
fn document_includes_attributes() {
let mut rel = Relationship::new(RelationshipType::Hyperlink, Some("http://x"));
rel.id = Some("rId3".to_string());
rel.target_mode = Some("External".to_string());
let doc = rel.to_document();
let xml = doc.to_pretty_string();
assert!(xml.contains("Id=\"rId3\""));
assert!(xml.contains("Target=\"http://x\""));
assert!(xml.contains("TargetMode=\"External\""));
assert!(xml.contains("relationships/hyperlink"));
}
}