Skip to main content

update_package/
update_mode.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Typesafe wrappers around parsing the update-mode file.
6
7use serde::{Deserialize, Serialize};
8use std::str::FromStr;
9use thiserror::Error;
10
11/// An error encountered while parsing an update mode string.
12#[derive(Clone, Debug, PartialEq, Eq, Error)]
13#[allow(missing_docs)]
14pub enum ParseUpdateModeError {
15    #[error("update mode not supported: '{0}'")]
16    UpdateModeNotSupported(String),
17}
18
19/// Enum to describe the supported update modes.
20#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
21#[serde(rename_all = "kebab-case")]
22pub enum UpdateMode {
23    /// Follow the normal system update flow.
24    #[default]
25    Normal,
26    /// Instead of the normal flow, write a recovery image and reboot into it.
27    ForceRecovery,
28}
29
30impl FromStr for UpdateMode {
31    type Err = ParseUpdateModeError;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s {
35            "normal" => Ok(UpdateMode::Normal),
36            "force-recovery" => Ok(UpdateMode::ForceRecovery),
37            other => Err(ParseUpdateModeError::UpdateModeNotSupported(other.to_string())),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_from_str() {
48        assert_eq!("normal".parse::<UpdateMode>().unwrap(), UpdateMode::Normal);
49        assert_eq!("force-recovery".parse::<UpdateMode>().unwrap(), UpdateMode::ForceRecovery);
50        assert_eq!(
51            "potato".parse::<UpdateMode>().unwrap_err(),
52            ParseUpdateModeError::UpdateModeNotSupported("potato".to_string())
53        );
54    }
55}