fapolicy_rules/parser/
from_str.rs

1/*
2 * Copyright Concurrent Technologies Corporation 2021
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7 */
8
9use crate::parser::parse::*;
10use crate::parser::{decision, object, permission, subject};
11use crate::{Decision, Object, Permission, Subject};
12use std::str::FromStr;
13
14impl FromStr for Decision {
15    type Err = String;
16
17    fn from_str(i: &str) -> Result<Self, Self::Err> {
18        match decision::parse(StrTrace::new(i)) {
19            Ok((_, s)) => Ok(s),
20            Err(_) => Err("Failed to parse Decision from string".into()),
21        }
22    }
23}
24
25impl FromStr for Permission {
26    type Err = String;
27
28    fn from_str(i: &str) -> Result<Self, Self::Err> {
29        match permission::parse(StrTrace::new(i)) {
30            Ok((_, s)) => Ok(s),
31            Err(_) => Err("Failed to parse Permission from string".into()),
32        }
33    }
34}
35
36impl FromStr for Subject {
37    type Err = String;
38
39    fn from_str(i: &str) -> Result<Self, Self::Err> {
40        match subject::parse(StrTrace::new(i)) {
41            Ok((_, s)) => Ok(s),
42            Err(_) => Err("Failed to parse Subject from string".into()),
43        }
44    }
45}
46
47impl FromStr for Object {
48    type Err = String;
49
50    fn from_str(i: &str) -> Result<Self, Self::Err> {
51        match object::parse(StrTrace::new(i)) {
52            Ok((_, s)) => Ok(s),
53            Err(_) => Err("Failed to parse Object from string".into()),
54        }
55    }
56}