snarkvm_console_network_environment/traits/parse.rs
1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use nom::{
17 Err as NomErr,
18 IResult,
19 error::{VerboseError, convert_error},
20};
21
22/// The `nom`-compatible parser return type.
23pub type ParserResult<'a, O> = IResult<&'a str, O, VerboseError<&'a str>>;
24
25/// Converts a `ParserResult` into a human-readable message.
26pub fn convert_result<'a, O>(result: ParserResult<'a, O>, input: &'a str) -> String {
27 match result {
28 Ok(_) => "Parsing was successful.".to_string(),
29 Err(error) => match error {
30 NomErr::Incomplete(_) => "Parsing failed to consume the entire input.".to_string(),
31 NomErr::Error(err) | NomErr::Failure(err) => convert_error(input, err),
32 },
33 }
34}
35
36/// Operations to parse a string literal into an object.
37pub trait Parser: core::fmt::Display + core::str::FromStr {
38 /// Parses a string literal into an object.
39 fn parse(string: &str) -> ParserResult<Self>
40 where
41 Self: Sized;
42}