1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
//! Built-in gRDF node type.

use iref::IriBuf;
use ordered_float::NotNan;
use std::fmt;

/// Standard gRDF term.
///
/// Either a blank node identifier, IRI or literal value.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Term {
	/// Blank node identifier.
	Blank(usize),

	/// IRI.
	Iri(IriBuf),

	/// Literal value.
	Value(Literal),
}

impl Term {
	/// Get the string representation of the term, if any.
	///
	/// Returns some string if the term is an IRI or a string literal.
	pub fn as_str(&self) -> Option<&str> {
		match self {
			Term::Iri(iri) => Some(iri.as_str()),
			Term::Value(lit) => lit.as_str(),
			_ => None,
		}
	}
}

impl fmt::Display for Term {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Term::Blank(id) => write!(f, "_:{}", id),
			Term::Iri(iri) => write!(f, "<{}>", iri),
			Term::Value(lit) => lit.fmt(f),
		}
	}
}

/// RDF literals.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Literal {
	/// A maybe tagged string literal.
	String(String, Option<Tag>),

	/// Integer constant.
	Int(i64),

	/// Fload constant.
	Float(NotNan<f64>),

	/// Boolean constant.
	Bool(bool),
}

impl fmt::Display for Literal {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Literal::String(str, Some(tag)) => write!(f, "\"{}\"{}", str, tag),
			Literal::String(str, None) => write!(f, "\"{}\"", str),
			Literal::Int(i) => i.fmt(f),
			Literal::Float(d) => d.fmt(f),
			Literal::Bool(true) => write!(f, "true"),
			Literal::Bool(false) => write!(f, "false"),
		}
	}
}

impl Literal {
	pub fn as_str(&self) -> Option<&str> {
		match self {
			Literal::String(str, _) => Some(str.as_str()),
			_ => None,
		}
	}
}

/// Tag of a RDF tagged string.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Tag {
	/// Language tag.
	Lang(String),

	/// IRI tag.
	Iri(IriBuf),
}

impl fmt::Display for Tag {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Tag::Lang(lang) => write!(f, "@{}", lang),
			Tag::Iri(iri) => write!(f, "^^<{}>", iri),
		}
	}
}