commit bfa96cd49f6546917eaf34cfbfc12a1cc58fb148 Author: Jonas Platte Date: Fri May 1 17:15:07 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..96ef6c0b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..13866cae --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "ruma-common" +version = "0.1.0" +authors = ["Jonas Platte "] +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"] } diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..f0d48ccb --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 00000000..ad707407 --- /dev/null +++ b/README.md @@ -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). diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..6849d043 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +//! Common types for other ruma crates. + +#![warn(missing_docs)] + +pub mod push; diff --git a/src/push.rs b/src/push.rs new file mode 100644 index 00000000..94877976 --- /dev/null +++ b/src/push.rs @@ -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, + }, +} diff --git a/src/push/tweak_serde.rs b/src/push/tweak_serde.rs new file mode 100644 index 00000000..ce7268c0 --- /dev/null +++ b/src/push/tweak_serde.rs @@ -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, + }, +} + +#[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 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 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 }, + } + } +}