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
//! オブジェクト関連のエンティティ定義。
use std::str::FromStr;
use Error;

// FIXME: 構造体にする
/// オブジェクトのID。
pub type ObjectId = String;

/// メタデータオブジェクトのバージョン.
#[derive(
    Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct ObjectVersion(pub u64);

impl FromStr for ObjectVersion {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        track!(s.parse::<u64>().map(ObjectVersion).map_err(Error::from))
    }
}

/// メタデータオブジェクトの接頭辞
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ObjectPrefix(pub String);

/// メタデータオブジェクトの要約.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(missing_docs)]
pub struct ObjectSummary {
    pub id: String,
    pub version: ObjectVersion,
}

/// オブジェクトのメタデータ.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Metadata {
    /// バージョン番号.
    pub version: ObjectVersion,

    /// ユーザ定義の任意のバイト列.
    pub data: Vec<u8>,
}

/// 接頭辞指定でのオブジェクト削除時の削除結果要約
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct DeleteObjectsByPrefixSummary {
    /// 削除されたオブジェクトの総数
    /// JSON で u64 を扱えるように string に serialize する。
    #[serde(with = "prefix_summary_total")]
    pub total: u64,
}

/// フラグメント要約.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct FragmentsSummary {
    /// 破損しているかどうか.
    pub is_corrupted: bool,
    /// デバイス上に存在すべきフラグメント群の内、存在したフラグメント数.
    pub found_total: u8,
    /// デバイス上に存在すべきフラグメント群の内、見つからなかったフラグメント数.
    pub lost_total: u8,
}

mod prefix_summary_total {
    use serde::{de, Deserialize, Deserializer, Serializer};
    use std::fmt::Display;
    use std::str::FromStr;

    #[allow(clippy::trivially_copy_pass_by_ref)]
    pub fn serialize<S>(x: &u64, s: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        s.serialize_str(&x.to_string())
    }

    pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
    where
        T: FromStr,
        T::Err: Display,
        D: Deserializer<'de>,
    {
        String::deserialize(deserializer)?
            .parse()
            .map_err(de::Error::custom)
    }
}