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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2025 Rust Nostr Developers
// Distributed under the MIT software license
//! NIPC0: Code Snippets
//!
//! <https://github.com/nostr-protocol/nips/blob/master/C0.md>
use alloc::string::String;
use alloc::vec::Vec;
use crate::{EventBuilder, Kind, Tag, TagStandard};
/// Code snippet
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CodeSnippet {
/// The code snippet.
pub snippet: String,
/// Programming language name.
/// Examples: "javascript", "python", "rust"
pub language: Option<String>,
/// Name of the code snippet, commonly a filename.
/// Examples: "hello-world.js", "quick-sort.py"
pub name: Option<String>,
/// File extension (without the dot).
/// Examples: "js", "py", "rs"
pub extension: Option<String>,
/// Brief description of what the code does
pub description: Option<String>,
/// Runtime or environment specification.
/// Example: "node v18.15.0", "python 3.11"
pub runtime: Option<String>,
/// License under which the code is shared.
/// Example: "MIT", "GPL-3.0", "Apache-2.0"
pub license: Option<String>,
/// Dependencies required for the code to run.
pub dependencies: Vec<String>,
/// Reference to a repository where this code originates.
pub repo: Option<String>,
}
impl CodeSnippet {
/// Create a new code snippet
#[inline]
pub fn new<T>(snippet: T) -> Self
where
T: Into<String>,
{
Self {
snippet: snippet.into(),
..Default::default()
}
}
/// Set the programming language name (e.g. "javascript", "python", "rust").
#[inline]
pub fn language<T>(mut self, lang: T) -> Self
where
T: AsRef<str>,
{
self.language = Some(lang.as_ref().to_lowercase());
self
}
/// Set the name of the code snippet, commonly a filename.
#[inline]
pub fn name<T>(mut self, name: T) -> Self
where
T: Into<String>,
{
self.name = Some(name.into());
self
}
/// Set the file extension (without the dot).
#[inline]
pub fn extension<T>(mut self, extension: T) -> Self
where
T: Into<String>,
{
self.extension = Some(extension.into());
self
}
/// Set a brief description of what the code does
#[inline]
pub fn description<T>(mut self, description: T) -> Self
where
T: Into<String>,
{
self.description = Some(description.into());
self
}
/// Set the runtime or environment specification (e.g. "node v18.15.0", "python 3.11").
#[inline]
pub fn runtime<T>(mut self, runtime: T) -> Self
where
T: Into<String>,
{
self.runtime = Some(runtime.into());
self
}
/// Set the license under which the code is shared (e.g. "MIT", "GPL-3.0", "Apache-2.0").
#[inline]
pub fn license<T>(mut self, license: T) -> Self
where
T: Into<String>,
{
self.license = Some(license.into());
self
}
/// Add a dependency required for the code to run.
pub fn dependencies<T>(mut self, dep: T) -> Self
where
T: Into<String>,
{
let dep = dep.into();
if !self.dependencies.contains(&dep) {
self.dependencies.push(dep);
}
self
}
/// Set the repository where this code originates.
#[inline]
pub fn repo<T>(mut self, repo: T) -> Self
where
T: Into<String>,
{
self.repo = Some(repo.into());
self
}
/// Convert the code snippet to an event builder
#[allow(clippy::wrong_self_convention)]
pub(crate) fn to_event_builder(self) -> EventBuilder {
let mut tags: Vec<Tag> = Vec::new();
let mut add_if_some = |tag: Option<TagStandard>| {
if let Some(tag) = tag {
tags.push(Tag::from_standardized_without_cell(tag));
}
};
// `l` tag used for label in all event kinds except Code Snippets (1337)
// is used as the programming language
add_if_some(self.language.map(|l| TagStandard::Label {
value: l,
namespace: None,
}));
add_if_some(self.name.map(TagStandard::Name));
add_if_some(self.extension.map(TagStandard::Extension));
add_if_some(self.description.map(TagStandard::Description));
add_if_some(self.runtime.map(TagStandard::Runtime));
add_if_some(self.license.map(TagStandard::License));
add_if_some(self.repo.map(TagStandard::Repository));
for dep in self.dependencies.into_iter() {
tags.push(TagStandard::Dependency(dep).into());
}
EventBuilder::new(Kind::CodeSnippet, self.snippet).tags(tags)
}
}