oak_rust/ast/mod.rs
1#![doc = include_str!("readme.md")]
2use crate::lexer::RustTokenType;
3use core::range::Range;
4
5use std::sync::Arc;
6
7/// Strongly-typed AST root node representing the entire Rust source file.
8///
9/// This is the top-level structure that contains all items (functions, statements, etc.)
10/// parsed from a Rust source file.
11#[derive(Clone, Debug, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct RustRoot {
14 /// Collection of top-level items in the Rust file
15 pub items: Vec<Item>,
16}
17
18/// Represents a generic parameter list and where clauses in Rust.
19#[derive(Clone, Debug, PartialEq, Eq, Hash)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21pub struct Generics {
22 /// List of generic parameters (lifetimes, types, constants)
23 pub params: Vec<GenericParam>,
24 /// Optional where clause
25 pub where_clause: Option<WhereClause>,
26 /// Source code span
27 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
28 pub span: Range<usize>,
29}
30
31/// Represents a single generic parameter.
32#[derive(Clone, Debug, PartialEq, Eq, Hash)]
33#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
34pub enum GenericParam {
35 /// A lifetime parameter (e.g., `'a`)
36 Lifetime(Lifetime),
37 /// A type parameter (e.g., `T: Display`)
38 Type {
39 /// The type parameter name
40 name: Identifier,
41 /// Optional bounds (e.g., `: Display + Clone`)
42 bounds: Vec<TypeParamBound>,
43 /// Optional default type (e.g., `= i32`)
44 default: Option<Type>,
45 },
46 /// A const parameter (e.g., `const N: usize`)
47 Const {
48 /// The const parameter name
49 name: Identifier,
50 /// The type of the constant
51 ty: Type,
52 /// Optional default value
53 default: Option<Expr>,
54 },
55}
56
57/// Represents a lifetime in Rust (e.g., `'a`, `'static`).
58#[derive(Clone, Debug, PartialEq, Eq, Hash)]
59#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
60pub struct Lifetime {
61 /// The name of the lifetime (including the leading quote)
62 pub name: String,
63 /// Source code span
64 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
65 pub span: Range<usize>,
66}
67
68/// Represents a bound on a type parameter.
69#[derive(Clone, Debug, PartialEq, Eq, Hash)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub enum TypeParamBound {
72 /// A trait bound (e.g., `Display`)
73 Trait(Type),
74 /// A lifetime bound (e.g., `'a`)
75 Lifetime(Lifetime),
76}
77
78/// Represents a where clause in Rust.
79#[derive(Clone, Debug, PartialEq, Eq, Hash)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct WhereClause {
82 /// List of where predicates
83 pub predicates: Vec<WherePredicate>,
84 /// Source code span
85 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
86 pub span: Range<usize>,
87}
88
89/// Represents a single predicate in a where clause.
90#[derive(Clone, Debug, PartialEq, Eq, Hash)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum WherePredicate {
93 /// A type predicate (e.g., `T: Display`)
94 Type {
95 /// The type being bounded
96 bounded_ty: Type,
97 /// The bounds applied to the type
98 bounds: Vec<TypeParamBound>,
99 },
100 /// A lifetime predicate (e.g., `'a: 'b`)
101 Lifetime {
102 /// The lifetime being bounded
103 lifetime: Lifetime,
104 /// The bounds applied to the lifetime
105 bounds: Vec<Lifetime>,
106 },
107}
108
109/// Represents an identifier in Rust source code.
110///
111/// Identifiers are names used for variables, functions, types, and other named entities.
112/// Each identifier carries its textual representation and source location information.
113///
114/// # Examples
115///
116/// ```rust,ignore
117/// /// use oak_rust::ast::Identifier;
118/// let ident = Identifier { name: "main".to_string(), span: 0..4 }
119/// assert_eq!(ident.name, "main");
120/// ```
121#[derive(Clone, Debug, PartialEq, Eq, Hash)]
122#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
123pub struct Identifier {
124 /// The textual name of the identifier
125 pub name: String,
126 /// Source code span where this identifier appears
127 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
128 pub span: Range<usize>,
129}
130
131/// Top-level items that can appear in a Rust source file.
132///
133/// These represent the main constructs that can exist at the module level,
134/// such as function definitions, structs, enums, modules, and other declarations.
135#[derive(Clone, Debug, PartialEq, Eq, Hash)]
136#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
137pub enum Item {
138 /// A function definition
139 Function(Arc<Function>),
140 /// A struct definition
141 Struct(Arc<Struct>),
142 /// An enum definition
143 Enum(Arc<Enum>),
144 /// A module definition
145 Module(Arc<Module>),
146 /// A use statement
147 Use(Arc<UseItem>),
148 /// A trait definition
149 Trait(Arc<Trait>),
150 /// An impl block
151 Impl(Arc<Impl>),
152 /// A type alias
153 TypeAlias(Arc<TypeAlias>),
154 /// A constant definition
155 Const(Arc<Const>),
156 /// A static definition
157 Static(Arc<Static>),
158 /// An extern block
159 ExternBlock(Arc<ExternBlock>),
160}
161
162/// Represents a function definition in Rust source code.
163///
164/// Functions are fundamental building blocks in Rust that encapsulate reusable logic.
165/// They can have parameters, return types, and contain executable code blocks.
166#[derive(Clone, Debug, PartialEq, Eq, Hash)]
167#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
168pub struct Function {
169 /// The name identifier of the function
170 pub name: Identifier,
171 /// List of function parameters
172 pub params: Vec<Param>,
173 /// Optional return type
174 pub return_type: Option<Type>,
175 /// The function body containing executable statements
176 pub body: Block,
177 /// Generic parameters and where clauses
178 pub generics: Option<Generics>,
179 /// Whether the function is async
180 pub is_async: bool,
181 /// Whether the function is unsafe
182 pub is_unsafe: bool,
183 /// Whether the function is extern
184 pub is_extern: bool,
185 /// Source code span where this function appears
186 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
187 pub span: Range<usize>,
188}
189
190/// Represents a struct definition in Rust source code.
191#[derive(Clone, Debug, PartialEq, Eq, Hash)]
192#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
193pub struct Struct {
194 /// The name identifier of the struct
195 pub name: Identifier,
196 /// Generic parameters and where clauses
197 pub generics: Option<Generics>,
198 /// List of struct fields
199 pub fields: Vec<Field>,
200 /// Source code span where this struct appears
201 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
202 pub span: Range<usize>,
203}
204
205/// Represents a field in a struct definition.
206#[derive(Clone, Debug, PartialEq, Eq, Hash)]
207#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
208pub struct Field {
209 /// The field name identifier
210 pub name: Identifier,
211 /// The field type
212 pub ty: Type,
213 /// Source code span where this field appears
214 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
215 pub span: Range<usize>,
216}
217
218/// Represents an enum definition in Rust source code.
219#[derive(Clone, Debug, PartialEq, Eq, Hash)]
220#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
221pub struct Enum {
222 /// The name identifier of the enum
223 pub name: Identifier,
224 /// Generic parameters and where clauses
225 pub generics: Option<Generics>,
226 /// List of enum variants
227 pub variants: Vec<Variant>,
228 /// Source code span where this enum appears
229 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
230 pub span: Range<usize>,
231}
232
233/// Represents a variant in an enum definition.
234#[derive(Clone, Debug, PartialEq, Eq, Hash)]
235#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
236pub struct Variant {
237 /// The variant name identifier
238 pub name: Identifier,
239 /// Optional fields for tuple or struct variants
240 pub fields: Option<Vec<Field>>,
241 /// Source code span where this variant appears
242 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
243 pub span: Range<usize>,
244}
245
246/// Represents a module definition in Rust source code.
247#[derive(Clone, Debug, PartialEq, Eq, Hash)]
248#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
249pub struct Module {
250 /// The name identifier of the module
251 pub name: Identifier,
252 /// List of items within the module
253 pub items: Vec<Item>,
254 /// Source code span where this module appears
255 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
256 pub span: Range<usize>,
257}
258
259/// Represents a use statement in Rust source code.
260#[derive(Clone, Debug, PartialEq, Eq, Hash)]
261#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
262pub struct UseItem {
263 /// The path being imported
264 pub path: String,
265 /// Source code span where this use statement appears
266 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
267 pub span: Range<usize>,
268}
269
270/// Represents a trait definition in Rust source code.
271#[derive(Clone, Debug, PartialEq, Eq, Hash)]
272#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
273pub struct Trait {
274 /// The name identifier of the trait
275 pub name: Identifier,
276 /// Generic parameters and where clauses
277 pub generics: Option<Generics>,
278 /// List of trait items (methods, associated types, etc.)
279 pub items: Vec<TraitItem>,
280 /// Source code span where this trait appears
281 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
282 pub span: Range<usize>,
283}
284
285/// Represents items within a trait definition.
286#[derive(Clone, Debug, PartialEq, Eq, Hash)]
287#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
288pub enum TraitItem {
289 /// A method signature
290 Method(Function),
291 /// An associated type
292 Type(TypeAlias),
293}
294
295/// Represents an impl block in Rust source code.
296#[derive(Clone, Debug, PartialEq, Eq, Hash)]
297#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
298pub struct Impl {
299 /// Generic parameters and where clauses
300 pub generics: Option<Generics>,
301 /// The type being implemented for
302 pub ty: Type,
303 /// Optional trait being implemented
304 pub trait_: Option<Type>,
305 /// List of implementation items
306 pub items: Vec<ImplItem>,
307 /// Source code span where this impl block appears
308 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
309 pub span: Range<usize>,
310}
311
312/// Represents items within an impl block.
313#[derive(Clone, Debug, PartialEq, Eq, Hash)]
314#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
315pub enum ImplItem {
316 /// A method implementation
317 Method(Function),
318 /// An associated type implementation
319 Type(TypeAlias),
320 /// An associated constant
321 Const(Const),
322}
323
324/// Represents a type alias in Rust source code.
325#[derive(Clone, Debug, PartialEq, Eq, Hash)]
326#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
327pub struct TypeAlias {
328 /// The alias name identifier
329 pub name: Identifier,
330 /// Generic parameters and where clauses
331 pub generics: Option<Generics>,
332 /// The target type
333 pub ty: Type,
334 /// Source code span where this type alias appears
335 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
336 pub span: Range<usize>,
337}
338
339/// Represents a constant definition in Rust source code.
340#[derive(Clone, Debug, PartialEq, Eq, Hash)]
341#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
342pub struct Const {
343 /// The constant name identifier
344 pub name: Identifier,
345 /// The constant type
346 pub ty: Type,
347 /// The constant value expression
348 pub expr: Expr,
349 /// Source code span where this constant appears
350 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
351 pub span: Range<usize>,
352}
353
354/// Represents a static definition in Rust source code.
355#[derive(Clone, Debug, PartialEq, Eq, Hash)]
356#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
357pub struct Static {
358 /// The static name identifier
359 pub name: Identifier,
360 /// The static type
361 pub ty: Type,
362 /// The static value expression
363 pub expr: Expr,
364 /// Whether the static is mutable
365 pub mutable: bool,
366 /// Source code span where this static appears
367 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
368 pub span: Range<usize>,
369}
370
371/// Represents a type in Rust source code.
372#[derive(Clone, Debug, PartialEq, Eq, Hash)]
373#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
374pub enum Type {
375 /// A path type (e.g., std::vec::Vec)
376 Path(String),
377 /// A reference type (e.g., &str, &mut T)
378 Reference {
379 /// Whether the reference is mutable
380 mutable: bool,
381 /// The referenced type
382 ty: Box<Type>,
383 },
384 /// A tuple type (e.g., (i32, String))
385 Tuple(Vec<Type>),
386 /// An array type (e.g., [i32; 10])
387 Array {
388 /// The element type
389 ty: Box<Type>,
390 /// The array size expression
391 size: Expr,
392 },
393 /// A slice type (e.g., [i32])
394 Slice(Box<Type>),
395 /// A function pointer type (e.g., fn(i32) -> String)
396 Fn {
397 /// Parameter types
398 params: Vec<Type>,
399 /// Return type
400 ret: Option<Box<Type>>,
401 },
402 /// An inferred type (_)
403 Infer,
404}
405
406/// Represents a function parameter with its type annotation.
407///
408/// Parameters define the inputs that a function can accept, with their respective types.
409#[derive(Clone, Debug, PartialEq, Eq, Hash)]
410#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
411pub struct Param {
412 /// The parameter name identifier
413 pub name: Identifier,
414 /// The parameter type
415 pub ty: Type,
416 /// Whether the parameter is mutable
417 pub is_mut: bool,
418 /// Source code span where this parameter appears
419 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
420 pub span: Range<usize>,
421}
422
423/// Represents a block of statements enclosed in braces.
424///
425/// Blocks are used to group statements together and define scope boundaries.
426#[derive(Clone, Debug, PartialEq, Eq, Hash)]
427#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
428pub struct Block {
429 /// List of statements within the block
430 pub statements: Vec<Statement>,
431 /// Block start position
432 pub block_start: usize,
433 /// Block end position
434 pub block_end: usize,
435 /// Nested block level
436 pub nested: usize,
437 /// Source code span where this block appears
438 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
439 pub span: Range<usize>,
440}
441
442/// Represents different types of statements in Rust source code.
443///
444/// Statements are executable units that raise actions or declare bindings.
445#[derive(Clone, Debug, PartialEq, Eq, Hash)]
446#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
447pub enum Statement {
448 /// A let binding statement
449 Let {
450 /// The variable name being bound
451 name: Identifier,
452 /// Optional type annotation
453 ty: Option<Type>,
454 /// The expression being assigned to the variable
455 expr: Option<Expr>,
456 /// Whether the binding is mutable
457 mutable: bool,
458 /// Source code span where this let statement appears
459 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
460 span: Range<usize>,
461 },
462 /// An expression statement (expression followed by optional semicolon)
463 ExprStmt {
464 /// The expression being evaluated
465 expr: Expr,
466 /// Whether the statement ends with a semicolon
467 semi: bool,
468 /// Source code span where this expression statement appears
469 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
470 span: Range<usize>,
471 },
472 /// A return statement
473 Return {
474 /// Optional return expression
475 expr: Option<Expr>,
476 /// Source code span where this return statement appears
477 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
478 span: Range<usize>,
479 },
480 /// A break statement
481 Break {
482 /// Optional break expression
483 expr: Option<Expr>,
484 /// Source code span where this break statement appears
485 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
486 span: Range<usize>,
487 },
488 /// A continue statement
489 Continue {
490 /// Source code span where this continue statement appears
491 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
492 span: Range<usize>,
493 },
494 /// An item statement (item declaration within a block)
495 Item(Item),
496}
497
498/// Represents different types of expressions in Rust source code.
499///
500/// Expressions are constructs that evaluate to values and can be used in various contexts.
501#[derive(Clone, Debug, PartialEq, Eq, Hash)]
502#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
503pub enum Expr {
504 /// An identifier expression
505 Ident(Identifier),
506 /// A literal expression
507 Literal {
508 /// The literal value as a string
509 value: String,
510 /// Source code span where this literal appears
511 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
512 span: Range<usize>,
513 },
514 /// A boolean literal expression
515 Bool {
516 /// The boolean value
517 value: bool,
518 /// Source code span where this boolean literal appears
519 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
520 span: Range<usize>,
521 },
522 /// A unary expression (e.g., !x, -x, *x, &x)
523 Unary {
524 /// The unary operator
525 op: RustTokenType,
526 /// The operand expression
527 expr: Box<Expr>,
528 /// Source code span where this unary expression appears
529 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
530 span: Range<usize>,
531 },
532 /// A binary expression (e.g., x + y, x == y)
533 Binary {
534 /// The left operand
535 left: Box<Expr>,
536 /// The binary operator
537 op: RustTokenType,
538 /// The right operand
539 right: Box<Expr>,
540 /// Source code span where this binary expression appears
541 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
542 span: Range<usize>,
543 },
544 /// A function call expression
545 Call {
546 /// The function being called
547 callee: Box<Expr>,
548 /// The arguments passed to the function
549 args: Vec<Expr>,
550 /// Source code span where this call expression appears
551 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
552 span: Range<usize>,
553 },
554 /// A field access expression (e.g., obj.field)
555 Field {
556 /// The object being accessed
557 receiver: Box<Expr>,
558 /// The field being accessed
559 field: Identifier,
560 /// Source code span where this field expression appears
561 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
562 span: Range<usize>,
563 },
564 /// An index expression (e.g., arr[0])
565 Index {
566 /// The object being indexed
567 receiver: Box<Expr>,
568 /// The index expression
569 index: Box<Expr>,
570 /// Source code span where this index expression appears
571 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
572 span: Range<usize>,
573 },
574 /// A parenthesized expression
575 Paren {
576 /// The inner expression
577 expr: Box<Expr>,
578 /// Source code span where this parenthesized expression appears
579 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
580 span: Range<usize>,
581 },
582 /// A block expression
583 Block(Block),
584 /// An if expression
585 If {
586 /// The condition expression
587 condition: Box<Expr>,
588 /// The then block
589 then_block: Block,
590 /// Optional else block
591 else_block: Option<Block>,
592 /// Source code span where this if expression appears
593 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
594 span: Range<usize>,
595 },
596 /// A while loop expression
597 While {
598 /// The condition expression
599 condition: Box<Expr>,
600 /// The loop body
601 body: Block,
602 /// Source code span where this while expression appears
603 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
604 span: Range<usize>,
605 },
606 /// A for loop expression
607 For {
608 /// The loop variable
609 var: Identifier,
610 /// The iterable expression
611 iter: Box<Expr>,
612 /// The loop body
613 body: Block,
614 /// Source code span where this for expression appears
615 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
616 span: Range<usize>,
617 },
618 /// A loop expression
619 Loop {
620 /// The loop body
621 body: Block,
622 /// Source code span where this loop expression appears
623 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
624 span: Range<usize>,
625 },
626 /// A match expression
627 Match {
628 /// The expression being matched
629 expr: Box<Expr>,
630 /// The match arms
631 arms: Vec<MatchArm>,
632 /// Source code span where this match expression appears
633 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
634 span: Range<usize>,
635 },
636 /// A tuple expression
637 Tuple {
638 /// The tuple elements
639 elements: Vec<Expr>,
640 /// Source code span where this tuple expression appears
641 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
642 span: Range<usize>,
643 },
644 /// An array expression
645 Array {
646 /// The array elements
647 elements: Vec<Expr>,
648 /// Source code span where this array expression appears
649 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
650 span: Range<usize>,
651 },
652 /// A struct expression
653 Struct {
654 /// The struct path
655 path: String,
656 /// The struct fields
657 fields: Vec<FieldInit>,
658 /// Source code span where this struct expression appears
659 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
660 span: Range<usize>,
661 },
662}
663
664/// Represents a match arm in a match expression.
665#[derive(Clone, Debug, PartialEq, Eq, Hash)]
666#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
667pub struct MatchArm {
668 /// The pattern to match
669 pub pattern: Pattern,
670 /// Optional guard expression
671 pub guard: Option<Expr>,
672 /// The arm body expression
673 pub body: Expr,
674 /// Source code span where this match arm appears
675 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
676 pub span: Range<usize>,
677}
678
679/// Represents a pattern in pattern matching.
680#[derive(Clone, Debug, PartialEq, Eq, Hash)]
681#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
682pub enum Pattern {
683 /// A wildcard pattern (_)
684 Wildcard,
685 /// An identifier pattern
686 Ident(Identifier),
687 /// A literal pattern
688 Literal(String),
689 /// A tuple pattern
690 Tuple(Vec<Pattern>),
691 /// A struct pattern
692 Struct {
693 /// The struct path
694 path: String,
695 /// The field patterns
696 fields: Vec<FieldPattern>,
697 },
698}
699
700/// Represents a field pattern in a struct pattern.
701#[derive(Clone, Debug, PartialEq, Eq, Hash)]
702#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
703pub struct FieldPattern {
704 /// The field name
705 pub name: Identifier,
706 /// The field pattern
707 pub pattern: Pattern,
708 /// Source code span where this field pattern appears
709 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
710 pub span: Range<usize>,
711}
712
713/// Represents a field initialization in a struct expression.
714#[derive(Clone, Debug, PartialEq, Eq, Hash)]
715#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
716pub struct FieldInit {
717 /// The field name
718 pub name: Identifier,
719 /// The field value expression
720 pub expr: Expr,
721 /// Source code span where this field initialization appears
722 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
723 pub span: Range<usize>,
724}
725
726/// Represents an extern block in Rust source code.
727#[derive(Clone, Debug, PartialEq, Eq, Hash)]
728#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
729pub struct ExternBlock {
730 /// The ABI string (e.g., "C", "system")
731 pub abi: Option<String>,
732 /// List of items within the extern block
733 pub items: Vec<Item>,
734 /// Source code span where this extern block appears
735 #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
736 pub span: Range<usize>,
737}