codama_nodes/
traits.rs

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
use codama_errors::CodamaResult;
use std::fmt::Debug;

pub trait NodeTrait:
    HasKind + Debug + PartialEq + Clone + serde::Serialize + for<'de> serde::Deserialize<'de>
{
    const KIND: &'static str;

    fn to_json(&self) -> CodamaResult<String> {
        serde_json::to_string(&self).map_err(Into::into)
    }

    fn to_json_pretty(&self) -> CodamaResult<String> {
        serde_json::to_string_pretty(&self).map_err(Into::into)
    }

    fn from_json(json: &str) -> CodamaResult<Self> {
        serde_json::from_str(json).map_err(Into::into)
    }
}

pub trait NodeUnionTrait:
    HasKind + Debug + PartialEq + Clone + serde::Serialize + for<'de> serde::Deserialize<'de>
{
}

pub trait HasKind {
    fn kind(&self) -> &'static str;
}