spaceapi_dezentrale/
lib.rs

1//! SpaceAPI definitions and serialization.
2//!
3//! This crate contains all data-related definitions that are present in the
4//! [SpaceAPI](https://spaceapi.io/). It also handles serializing and
5//! deserializing of that data from/to JSON by implementing the Serde
6//! [`Serialize`](https://docs.serde.rs/serde/ser/trait.Serialize.html) and
7//! [`Deserialize`](https://docs.serde.rs/serde/de/trait.Deserialize.html)
8//! traits for all structs.
9//!
10//! The currently supported SpaceAPI version is 0.13. It is not yet fully
11//! implemented.
12//!
13//! If you want to implement a SpaceAPI server on top of these types, you
14//! might want to take a look at the [`spaceapi_server`
15//! crate](https://github.com/spaceapi-community/spaceapi-server-rs).
16//!
17//! This library requires Rust 1.20.0 or newer.
18//!
19//! # Examples
20//!
21//! ## Serializing
22//!
23//! You can create a new `Status` instance by using the `StatusBuilder`.
24//!
25//!     use serde_json;
26//!     use spaceapi::{State, Status, StatusBuilder, Location, Contact, IssueReportChannel};
27//!
28//!     # fn main() {
29//!     let status = StatusBuilder::new("coredump")
30//!         .logo("https://www.coredump.ch/logo.png")
31//!         .url("https://www.coredump.ch/")
32//!         .state(State{
33//!             open: Some(false),
34//!             ..State::default()
35//!         })
36//!         .location(
37//!             Location {
38//!                 address: None,
39//!                 lat: 47.22936,
40//!                 lon: 8.82949,
41//!                 ..Default::default()
42//!             })
43//!         .contact(
44//!             Contact {
45//!                 irc: Some("irc://freenode.net/#coredump".into()),
46//!                 twitter: Some("@coredump_ch".into()),
47//!                 foursquare: Some("525c20e5498e875d8231b1e5".into()),
48//!                 email: Some("danilo@coredump.ch".into()),
49//!                 ..Default::default()
50//!             })
51//!         .add_issue_report_channel(IssueReportChannel::Email)
52//!         .add_issue_report_channel(IssueReportChannel::Twitter)
53//!         .build()
54//!         .expect("Creating status failed");
55//!     let serialized = serde_json::to_string(&status).unwrap();
56//!     let deserialized: Status = serde_json::from_str(&serialized).unwrap();
57//!
58//!     # assert!(&serialized[0..1] == "{");
59//!     # assert_eq!(deserialized, status);
60//!     # }
61//!
62//! ## Deserializing
63//!
64//! You can deserialize any struct of the SpaceAPI through Serde:
65//!
66//!     use serde_json;
67//!     use spaceapi::Location;
68//!
69//!     # fn main() {
70//!     let location = "{\"lat\": 47.22936, \"lon\": 8.82949}";
71//!     let decoded: Location = serde_json::from_str(location).unwrap();
72//!     println!("{:?}", decoded);
73//!
74//!     // Output:
75//!     // Location { address: None, lat: 47.22936000000001, lon: 8.829490000000002, timezone: None }
76//!     # }
77
78pub mod sensors;
79mod status;
80pub use crate::status::*;
81
82/// Return own crate version. Used in API responses.
83pub fn get_version() -> &'static str {
84    env!("CARGO_PKG_VERSION")
85}
86
87#[cfg(test)]
88mod test {
89    use super::get_version;
90
91    #[test]
92    fn test_get_version() {
93        let version = get_version();
94        assert_eq!(3, version.split('.').count());
95    }
96}