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
use uri::Uri;
use specs::xml_specs::XmlDataTypes;

/// Contains specifications for validating turtle syntax.
pub struct TurtleSpecs { }


impl TurtleSpecs {
  /// Checks if the provided literal is a plain literal that corresponds to the provided data type.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::specs::turtle_specs::TurtleSpecs;
  /// use rdf::specs::xml_specs::XmlDataTypes;
  ///
  /// assert!(TurtleSpecs::is_plain_literal(&"3.0".to_string(), &Some(XmlDataTypes::Decimal.to_uri())));
  /// assert!(TurtleSpecs::is_plain_literal(&"true".to_string(), &Some(XmlDataTypes::Boolean.to_uri())));
  /// assert!(TurtleSpecs::is_plain_literal(&"3e10".to_string(), &Some(XmlDataTypes::Decimal.to_uri())));
  /// assert_eq!(TurtleSpecs::is_plain_literal(&"a".to_string(), &Some(XmlDataTypes::Decimal.to_uri())), false);
  /// ```
  pub fn is_plain_literal(literal: &String, data_type: &Option<Uri>) -> bool {
    if TurtleSpecs::is_double_literal(literal) && *data_type == Some(XmlDataTypes::Decimal.to_uri()) {
      return true;
    }

    if TurtleSpecs::is_boolean_literal(literal) && *data_type == Some(XmlDataTypes::Boolean.to_uri()) {
      return true;
    }

    if TurtleSpecs::is_integer_literal(literal) && *data_type == Some(XmlDataTypes::Integer.to_uri()) {
      return true;
    }

    false
  }

  /// Checks if the provided literal is decimal.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::specs::turtle_specs::TurtleSpecs;
  ///
  /// assert!(TurtleSpecs::is_double_literal(&"3.0".to_string()));
  /// assert!(TurtleSpecs::is_double_literal(&"3e10".to_string()));
  /// assert_eq!(TurtleSpecs::is_double_literal(&"a".to_string()), false);
  /// ```
  pub fn is_double_literal(literal: &String) -> bool {
    match literal.parse::<f64>() {
      Ok(_) => true,
      Err(_) => false
    }
  }

  /// Checks if the provided literal is an integer.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::specs::turtle_specs::TurtleSpecs;
  ///
  /// assert!(TurtleSpecs::is_integer_literal(&"3".to_string()));
  /// assert_eq!(TurtleSpecs::is_integer_literal(&"3.0".to_string()), false);
  /// ```
  pub fn is_integer_literal(literal: &String) -> bool {
    match literal.parse::<i64>() {
      Ok(_) => true,
      Err(_) => false
    }
  }


  /// Checks if the provided literal is a boolean.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::specs::turtle_specs::TurtleSpecs;
  ///
  /// assert!(TurtleSpecs::is_boolean_literal(&"true".to_string()));
  /// assert!(TurtleSpecs::is_boolean_literal(&"false".to_string()));
  /// assert_eq!(TurtleSpecs::is_boolean_literal(&"1".to_string()), false);
  /// ```
  pub fn is_boolean_literal(literal: &String) -> bool {
    match literal.parse::<bool>() {
      Ok(_) => true,
      Err(_) => false
    }
  }
}