1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use fuchsia_inspect as inspect;
6use std::borrow::Cow;
7use std::marker::PhantomData;
89pub trait GraphObject {
10type Id;
11fn write_to_node(node: &inspect::Node, id: &Self::Id);
12}
1314#[derive(Debug)]
15pub struct VertexMarker<T>(PhantomData<T>);
16#[derive(Debug)]
17pub struct EdgeMarker;
1819impl GraphObject for EdgeMarker {
20type Id = u64;
2122fn write_to_node(node: &inspect::Node, id: &Self::Id) {
23 node.record_uint("edge_id", *id);
24 }
25}
2627impl<T: VertexId> GraphObject for VertexMarker<T> {
28type Id = T;
2930fn write_to_node(node: &inspect::Node, id: &Self::Id) {
31 node.record_string("vertex_id", id.get_id().as_ref());
32 }
33}
3435/// The ID of a vertex.
36pub trait VertexId {
37/// Fetches the ID of a vertex, which must have a string representation.
38fn get_id(&self) -> Cow<'_, str>;
39}
4041impl<T: std::fmt::Display> VertexId for T {
42fn get_id(&self) -> Cow<'_, str> {
43 Cow::Owned(format!("{self}"))
44 }
45}
4647impl VertexId for str {
48fn get_id(&self) -> Cow<'_, str> {
49 Cow::Borrowed(self)
50 }
51}