Skip to content

Commit 15629e6

Browse files
sadroeckmeta-codesync[bot]
authored andcommitted
Add nodes types for the TypeSystem library in Rust
Summary: This diff introduces the building blocks (~data model) for the Rust's `TypeSystem` library, which mirrors the [C++ version](https://fburl.com/code/a8tjzcm3). This crate provides representations for all Thrift types, e.g. primitives, containers, and user-defined types, as a shared type graph with immutable nodes. The `TypeRef` enum provides a descriptor for a given node, while the specific nodes themselves provide access to their structural properties. ## Note See the C++ implementation & rest of the diff stack for more details on "TypeSystem". Reviewed By: praihan Differential Revision: D96817822 fbshipit-source-id: c871cf6ebe58b4a3b6428b97fc9c20d555942d76
1 parent 55637ab commit 15629e6

7 files changed

Lines changed: 1236 additions & 0 deletions

File tree

thrift/lib/rust/dynamic/BUCK

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@fbsource//tools/build_defs:fbsource_utils.bzl", "is_fbcode")
2+
load("@fbsource//xplat/thrift:defs.bzl", "thrift_rust_library")
3+
4+
oncall("thrift_dynamic")
5+
6+
thrift_rust_library(
7+
name = "thrift_type_system",
8+
srcs = glob(["type_system/**/*.rs"]),
9+
allow_oss_build = False,
10+
crate_root = "type_system/lib.rs",
11+
unittests = is_fbcode(),
12+
deps = [
13+
"fbsource//third-party/rust:thiserror",
14+
"//thrift/lib/thrift:record-rust",
15+
"//thrift/lib/thrift:type_id-rust",
16+
],
17+
)

thrift/lib/rust/dynamic/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# @generated by autocargo from //thrift/lib/rust/dynamic:thrift_type_system
2+
3+
[package]
4+
name = "thrift_type_system"
5+
version = "0.0.1+unstable"
6+
authors = ["Daniel Xu <dlxu@fb.com>", "Facebook"]
7+
edition = "2024"
8+
repository = "https://github.com/facebook/fbthrift"
9+
license = "Apache-2.0"
10+
11+
[lib]
12+
path = "type_system/lib.rs"
13+
14+
[dependencies]
15+
thiserror = "2.0.18"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use crate::type_ref::TypeRef;
18+
19+
/// Thrift `list<T>` type descriptor.
20+
#[derive(Clone, Debug)]
21+
pub struct ListType {
22+
pub(crate) element: TypeRef,
23+
}
24+
25+
impl ListType {
26+
pub fn element_type(&self) -> &TypeRef {
27+
&self.element
28+
}
29+
}
30+
31+
/// Thrift `set<T>` type descriptor.
32+
#[derive(Clone, Debug)]
33+
pub struct SetType {
34+
pub(crate) element: TypeRef,
35+
}
36+
37+
impl SetType {
38+
pub fn element_type(&self) -> &TypeRef {
39+
&self.element
40+
}
41+
}
42+
43+
/// Thrift `map<K, V>` type descriptor.
44+
#[derive(Clone, Debug)]
45+
pub struct MapType {
46+
pub(crate) key: TypeRef,
47+
pub(crate) value: TypeRef,
48+
}
49+
50+
impl MapType {
51+
pub fn key_type(&self) -> &TypeRef {
52+
&self.key
53+
}
54+
55+
pub fn value_type(&self) -> &TypeRef {
56+
&self.value
57+
}
58+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/// Errors produced when validating or querying type definitions.
18+
#[derive(Debug, thiserror::Error)]
19+
pub enum InvalidTypeError {
20+
#[error("Unknown URI: {0}")]
21+
UnknownUri(String),
22+
#[error("Duplicate field ID {0} in {1}")]
23+
DuplicateFieldId(i16, String),
24+
#[error("Duplicate field name '{0}' in {1}")]
25+
DuplicateFieldName(String, String),
26+
#[error("Non-optional field {0} in union {1}")]
27+
NonOptionalUnionField(i16, String),
28+
#[error("Duplicate enum value {0} in {1}")]
29+
DuplicateEnumValue(i32, String),
30+
#[error("Duplicate enum name '{0}' in {1}")]
31+
DuplicateEnumName(String, String),
32+
#[error("Accessed {expected} but TypeRef is {actual}")]
33+
WrongKind {
34+
expected: &'static str,
35+
actual: &'static str,
36+
},
37+
#[error("Duplicate URI: {0}")]
38+
DuplicateUri(String),
39+
#[error("Opaque alias target must not be user-defined: {0}")]
40+
InvalidOpaqueAlias(String),
41+
#[error("Unresolvable TypeId in {uri}: {detail}")]
42+
UnresolvableTypeId { uri: String, detail: String },
43+
#[error("Empty TypeId")]
44+
EmptyTypeId,
45+
#[error("Too many fields ({0}) in {1}, maximum is 65535")]
46+
TooManyFields(usize, String),
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//! In-memory representation of a Thrift type system.
18+
//!
19+
//! Models all Thrift type definitions — primitives, containers, and
20+
//! user-defined types — as a shared, cheaply-cloneable type graph.
21+
//! Designed for construction from (and round-tripping to)
22+
//! [`SerializableTypeSystem`].
23+
24+
pub mod containers;
25+
pub mod error;
26+
pub mod nodes;
27+
pub mod type_ref;
28+
29+
pub use containers::ListType;
30+
pub use containers::MapType;
31+
pub use containers::SetType;
32+
pub use error::InvalidTypeError;
33+
pub use nodes::AnnotationsMap;
34+
pub use nodes::EnumNode;
35+
pub use nodes::EnumValue;
36+
pub use nodes::FieldDefinition;
37+
pub use nodes::FieldIdentity;
38+
pub use nodes::OpaqueAliasNode;
39+
pub use nodes::PresenceQualifier;
40+
pub use nodes::StructNode;
41+
pub use nodes::StructuredNode;
42+
pub use nodes::UnionNode;
43+
pub use type_ref::DefinitionRef;
44+
pub use type_ref::Kind;
45+
pub use type_ref::TypeRef;

0 commit comments

Comments
 (0)