provenant/output_schema/
datasource_id.rs1use serde::{Deserialize, Serialize};
5use std::fmt;
6use std::str::FromStr;
7
8#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
9#[serde(transparent)]
10pub struct OutputDatasourceId(String);
11
12impl OutputDatasourceId {
13 pub fn as_str(&self) -> &str {
14 &self.0
15 }
16}
17
18impl fmt::Display for OutputDatasourceId {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 f.write_str(&self.0)
21 }
22}
23
24impl From<crate::models::DatasourceId> for OutputDatasourceId {
25 fn from(value: crate::models::DatasourceId) -> Self {
26 Self(value.as_str().to_owned())
27 }
28}
29
30impl From<&crate::models::DatasourceId> for OutputDatasourceId {
31 fn from(value: &crate::models::DatasourceId) -> Self {
32 Self(value.as_str().to_owned())
33 }
34}
35
36impl TryFrom<&OutputDatasourceId> for crate::models::DatasourceId {
37 type Error = String;
38 fn try_from(value: &OutputDatasourceId) -> Result<Self, Self::Error> {
39 Self::from_str(value.as_str())
40 }
41}
42
43impl AsRef<str> for OutputDatasourceId {
44 fn as_ref(&self) -> &str {
45 &self.0
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn roundtrip_via_model() {
55 let model = crate::models::DatasourceId::NpmPackageJson;
56 let output = OutputDatasourceId::from(model);
57 assert_eq!(output.as_str(), "npm_package_json");
58 let back = crate::models::DatasourceId::try_from(&output).unwrap();
59 assert_eq!(back, model);
60 }
61
62 #[test]
63 fn serializes_as_plain_string() {
64 let output = OutputDatasourceId::from(crate::models::DatasourceId::CargoToml);
65 let json = serde_json::to_string(&output).unwrap();
66 assert_eq!(json, "\"cargo_toml\"");
67 }
68
69 #[test]
70 fn deserializes_from_plain_string() {
71 let output: OutputDatasourceId = serde_json::from_str("\"npm_package_json\"").unwrap();
72 assert_eq!(output.as_str(), "npm_package_json");
73 }
74}