Initial commit

This commit is contained in:
Jonas Platte 2020-05-01 17:15:07 +02:00
commit bfa96cd49f
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
7 changed files with 137 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
Cargo.lock

16
Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "ruma-common"
version = "0.1.0"
authors = ["Jonas Platte <jplatte@posteo.de>"]
description = "Common types for other ruma crates."
homepage = "https://github.com/ruma/ruma-common"
keywords = ["matrix", "chat", "messaging", "ruma"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/ruma/ruma-client-api"
edition = "2018"
[dependencies]
ruma-serde = "0.1.2"
serde = { version = "1.0.106", features = ["derive"] }
serde_json = { version = "1.0.52", features = ["raw_value"] }

19
LICENSE Normal file
View File

@ -0,0 +1,19 @@
Copyright (c) 2020 Jonas Platte
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# ruma-common
Common types for other ruma crates.
This crate is meant to be a dependency for other ruma crates only, consumers of
those crates should never have to use this directly (its types will be
re-exported from the other crates).

5
src/lib.rs Normal file
View File

@ -0,0 +1,5 @@
//! Common types for other ruma crates.
#![warn(missing_docs)]
pub mod push;

37
src/push.rs Normal file
View File

@ -0,0 +1,37 @@
//! Common types for the [push notifications module][push]
//!
//! [push]: https://matrix.org/docs/spec/client_server/r0.6.0#id89
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
mod tweak_serde;
/// The `set_tweak` action.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(from = "tweak_serde::Tweak", into = "tweak_serde::Tweak")]
pub enum Tweak {
/// A string representing the sound to be played when this notification arrives.
///
/// A value of "default" means to play a default sound. A device may choose to alert the user by
/// some other means if appropriate, eg. vibration.
Sound(String),
/// A boolean representing whether or not this message should be highlighted in the UI.
///
/// This will normally take the form of presenting the message in a different color and/or
/// style. The UI might also be adjusted to draw particular attention to the room in which the
/// event occurred. If a `highlight` tweak is given with no value, its value is defined to be
/// `true`. If no highlight tweak is given at all then the value of `highlight` is defined to be
/// `false`.
Highlight(#[serde(default = "ruma_serde::default_true")] bool),
/// A custom tweak
Custom {
/// The name of the custom tweak (`set_tweak` field)
name: String,
/// The value of the custom tweak
value: Box<RawJsonValue>,
},
}

51
src/push/tweak_serde.rs Normal file
View File

@ -0,0 +1,51 @@
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
/// Values for the `set_tweak` action.
#[derive(Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Tweak {
Sound(SoundTweak),
Highlight(HighlightTweak),
Custom {
#[serde(rename = "set_tweak")]
name: String,
value: Box<RawJsonValue>,
},
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "set_tweak", rename = "sound")]
pub struct SoundTweak {
value: String,
}
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "set_tweak", rename = "highlight")]
pub struct HighlightTweak {
value: bool,
}
impl From<super::Tweak> for Tweak {
fn from(tweak: super::Tweak) -> Self {
use super::Tweak::*;
match tweak {
Sound(value) => Self::Sound(SoundTweak { value }),
Highlight(value) => Self::Highlight(HighlightTweak { value }),
Custom { name, value } => Self::Custom { name, value },
}
}
}
impl From<Tweak> for super::Tweak {
fn from(tweak: Tweak) -> Self {
use Tweak::*;
match tweak {
Sound(SoundTweak { value }) => Self::Sound(value),
Highlight(HighlightTweak { value }) => Self::Highlight(value),
Custom { name, value } => Self::Custom { name, value },
}
}
}