File: str_value.rs

package info (click to toggle)
rust-zvariant 5.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,375 bytes parent folder | download
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
use zvariant::{LE, Value, serialized::Context, to_bytes};

#[macro_use]
mod common {
    include!("common.rs");
}

#[test]
fn str_value() {
    let string = String::from("hello world");
    basic_type_test!(LE, DBus, string, 16, String, 4);
    basic_type_test!(LE, DBus, string, 16, &str, 4);

    // GVariant format now
    #[cfg(feature = "gvariant")]
    basic_type_test!(LE, GVariant, string, 12, String, 1);

    let string = "hello world";
    basic_type_test!(LE, DBus, string, 16, &str, 4);
    basic_type_test!(LE, DBus, string, 16, String, 4);

    // As Value
    let v: Value<'_> = string.into();
    assert_eq!(v.value_signature(), "s");
    assert_eq!(v, Value::new("hello world"));
    value_test!(LE, DBus, v, 20);
    #[cfg(feature = "gvariant")]
    value_test!(LE, GVariant, v, 14);

    let v: String = v.try_into().unwrap();
    assert_eq!(v, "hello world");

    // Characters are treated as strings
    basic_type_test!(LE, DBus, 'c', 6, char, 4);
    #[cfg(feature = "gvariant")]
    basic_type_test!(LE, GVariant, 'c', 2, char, 1);

    // As Value
    let v: Value<'_> = "c".into();
    assert_eq!(v.value_signature(), "s");
    let ctxt = Context::new_dbus(LE, 0);
    let encoded = to_bytes(ctxt, &v).unwrap();
    assert_eq!(encoded.len(), 10);
    let (v, _) = encoded.deserialize::<Value<'_>>().unwrap();
    assert_eq!(v, Value::new("c"));
}