netgauze_parse_utils/traits.rs
1// Copyright (C) 2026-present The NetGauze Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::reader::SliceReader;
17
18/// Parse `Self` from the cursor with no additional context.
19pub trait ParseFrom<'a>: Sized {
20 type Error;
21 fn parse(cur: &mut SliceReader<'a>) -> Result<Self, Self::Error>;
22}
23
24/// Parse `Self` from the cursor with one immutable context argument.
25pub trait ParseFromWithOneInput<'a, I1>: Sized {
26 type Error;
27 fn parse(cur: &mut SliceReader<'a>, i1: I1) -> Result<Self, Self::Error>;
28}
29
30/// Parse `Self` from the cursor with two immutable context arguments.
31pub trait ParseFromWithTwoInputs<'a, I1, I2>: Sized {
32 type Error;
33 fn parse(cur: &mut SliceReader<'a>, i1: I1, i2: I2) -> Result<Self, Self::Error>;
34}
35
36/// Parse `Self` from the cursor with three immutable context arguments.
37pub trait ParseFromWithThreeInputs<'a, I1, I2, I3>: Sized {
38 type Error;
39 fn parse(cur: &mut SliceReader<'a>, i1: I1, i2: I2, i3: I3) -> Result<Self, Self::Error>;
40}
41
42/// Parse `Self` from the cursor with a **mutable** context.
43/// Used for stateful protocols (e.g. BGP, where capabilities update the
44/// context).
45pub trait ParseFromWithMut<'a, Ctx>: Sized {
46 type Error;
47 fn parse(cur: &mut SliceReader<'a>, ctx: &mut Ctx) -> Result<Self, Self::Error>;
48}