Skip to main content

majordomus_cli/capability/builtin/
views.rs

1//! The views more than one module answers with: an object of the layer as a client reads
2//! it, its summary for a listing, and the empty input.
3
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8use crate::capability::benchmark::{BenchmarkCases, CaseContext, NamedCase};
9use crate::model::{Object, Provenance as ObjectProvenance};
10
11/// One declarative object of the repository's layer, as a client reads it.
12#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
13pub struct ObjectView {
14    /// `majordomus://<kind>/<identity>`.
15    pub uri: String,
16    /// The capability id, `<kind>.<identity>`.
17    pub id: String,
18    /// The kind the object was read as (`rule`, `prompt`, `document`, ...).
19    pub kind: String,
20    /// The identity within the kind (`majordomus.scope-integrity@1`, `continue`, a path).
21    pub identity: String,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    /// The title the kind's title rule found, when it found one.
24    pub title: Option<String>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    /// The one-line description the kind's description field held, when it held one.
27    pub description: Option<String>,
28    /// The parsed front matter or YAML, keys in the file's order.
29    pub metadata: Value,
30    /// Where the object came from: path, directory, source class, section, size, member.
31    pub provenance: ObjectProvenance,
32    /// IANA media type of `content` (`text/markdown`, `application/yaml`, `application/json`, `text/plain`).
33    pub media_type: String,
34    /// The file as read.
35    pub content: String,
36}
37
38impl ObjectView {
39    pub(crate) fn of(o: &Object) -> Self {
40        ObjectView {
41            uri: o.uri.clone(),
42            id: format!("{}.{}", o.kind, o.identity),
43            kind: o.kind.clone(),
44            identity: o.identity.clone(),
45            title: o.title.clone(),
46            description: o.description.clone(),
47            metadata: o.metadata.clone(),
48            provenance: o.provenance.clone(),
49            media_type: o.media_type.to_string(),
50            content: o.content.clone(),
51        }
52    }
53}
54
55/// One object, summarised for a listing.
56#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
57pub struct ObjectSummary {
58    /// `majordomus://<kind>/<identity>`.
59    pub uri: String,
60    /// The capability id, `<kind>.<identity>`.
61    pub id: String,
62    /// The kind the object was read as.
63    pub kind: String,
64    /// The identity within the kind.
65    pub identity: String,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    /// The title, when the kind's title rule found one.
68    pub title: Option<String>,
69    #[serde(skip_serializing_if = "Option::is_none")]
70    /// The one-line description, when the kind holds one.
71    pub description: Option<String>,
72    /// Repository-relative source path.
73    pub path: String,
74}
75
76impl ObjectSummary {
77    pub(crate) fn of(o: &Object) -> Self {
78        ObjectSummary {
79            uri: o.uri.clone(),
80            id: format!("{}.{}", o.kind, o.identity),
81            kind: o.kind.clone(),
82            identity: o.identity.clone(),
83            title: o.title.clone(),
84            description: o.description.clone(),
85            path: o.provenance.path.clone(),
86        }
87    }
88}
89
90/// No input.
91#[derive(Debug, Default, Serialize, Deserialize, JsonSchema)]
92#[serde(deny_unknown_fields)]
93pub struct Empty {}
94
95impl BenchmarkCases for Empty {
96    fn benchmark_cases(_: &CaseContext<'_>) -> Vec<NamedCase<Self>> {
97        vec![NamedCase::new("default", Empty {})]
98    }
99}