sbat/component.rs
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use crate::csv::Record;
10use crate::{Generation, ParseError};
11use ascii::AsciiStr;
12
13/// SBAT component. This is the machine-readable portion of SBAT that is
14/// actually used for revocation (other fields are human-readable and
15/// not used for comparisons).
16#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
17pub struct Component<'a> {
18 /// Component name.
19 pub name: &'a AsciiStr,
20
21 /// Component generation.
22 pub generation: Generation,
23}
24
25impl<'a> Component<'a> {
26 /// Create a `Component`.
27 #[must_use]
28 pub fn new(name: &'a AsciiStr, generation: Generation) -> Self {
29 Self { name, generation }
30 }
31
32 /// Parse a `Component` from a `Record`.
33 pub(crate) fn from_record<const N: usize>(
34 record: &Record<'a, N>,
35 ) -> Result<Self, ParseError> {
36 Ok(Self {
37 name: record.get_field(0).ok_or(ParseError::TooFewFields)?,
38 generation: record
39 .get_field_as_generation(1)?
40 .ok_or(ParseError::TooFewFields)?,
41 })
42 }
43}