Skip to content

Commit 018fde6

Browse files
committed
Introduce atrium_api::types::Unknown for unknown fields
Closes sugyan#204.
1 parent 75673d0 commit 018fde6

File tree

13 files changed

+53
-18
lines changed

13 files changed

+53
-18
lines changed

atrium-api/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- `atrium_api::types::Unknown`
11+
12+
### Changed
13+
- `unknown` field types that don't have a well-known format now have the type
14+
`atrium_api::types::Unknown` instead of `atrium_api::records::Record`.
15+
916
## [0.23.2](https://github.com/sugyan/atrium/compare/atrium-api-v0.23.1...atrium-api-v0.23.2) - 2024-07-03
1017

1118
### Added

atrium-api/src/app/bsky/embed/record.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/admin/defs.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/identity/get_recommended_did_credentials.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/identity/sign_plc_operation.rs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/identity/submit_plc_operation.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/repo/apply_writes.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/repo/get_record.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/repo/list_records.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/com/atproto/server/create_account.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/tools/ozone/moderation/defs.rs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

atrium-api/src/types.rs

+26
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub enum Union<T> {
128128
Unknown(UnknownData),
129129
}
130130

131+
/// Data with an unknown schema in an open [`Union`].
132+
///
131133
/// The data of variants represented by a map and include a `$type` field indicating the variant type.
132134
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
133135
pub struct UnknownData {
@@ -137,6 +139,30 @@ pub struct UnknownData {
137139
pub data: Ipld,
138140
}
139141

142+
/// Arbitrary data with no specific validation and no type-specific fields.
143+
///
144+
/// Corresponds to [the `unknown` field type].
145+
///
146+
/// [the `unknown` field type]: https://atproto.com/specs/lexicon#unknown
147+
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq, Eq)]
148+
#[serde(try_from = "Ipld")]
149+
pub struct Unknown {
150+
pub data: Ipld,
151+
}
152+
153+
impl TryFrom<Ipld> for Unknown {
154+
type Error = &'static str;
155+
156+
fn try_from(value: Ipld) -> Result<Self, Self::Error> {
157+
// Enforce the ATProto data model.
158+
// https://atproto.com/specs/data-model
159+
match value {
160+
Ipld::Float(_) => Err("Floats are not allowed in ATProto"),
161+
data => Ok(Unknown { data }),
162+
}
163+
}
164+
}
165+
140166
#[cfg(test)]
141167
mod tests {
142168
use super::*;

lexicon/atrium-codegen/src/token_stream.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,10 @@ fn unknown_type(unknown: &LexUnknown, name: Option<&str>) -> Result<(TokenStream
566566
let description = description(&unknown.description);
567567
if name == Some("didDoc") {
568568
Ok((description, quote!(crate::did_doc::DidDocument)))
569-
} else {
569+
} else if name == Some("record") {
570570
Ok((description, quote!(crate::records::Record)))
571+
} else {
572+
Ok((description, quote!(crate::types::Unknown)))
571573
}
572574
}
573575

0 commit comments

Comments
 (0)