From 086062e92973f120fcdeac52f8c27e9cdbaf36df Mon Sep 17 00:00:00 2001 From: Ulrich Hornung Date: Tue, 9 Jan 2024 16:57:36 +0100 Subject: [PATCH 1/2] make get_varible usable from outside the crate --- src/error.rs | 21 +++++++-------------- src/lib.rs | 12 ++++++------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/error.rs b/src/error.rs index 368b92b..43e8193 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,8 +1,7 @@ //! Module containing error details. /// An error that can occur during variable substitution. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum Error { /// The input string contains an invalid escape sequence. InvalidEscapeSequence(InvalidEscapeSequence), @@ -139,8 +138,7 @@ impl std::fmt::Display for CharOrByte { } /// The input string contains an invalid escape sequence. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct InvalidEscapeSequence { /// The byte offset within the input where the error occurs. /// @@ -170,8 +168,7 @@ impl std::fmt::Display for InvalidEscapeSequence { } /// The input string contains a variable placeholder without a variable name (`"${}"`). -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct MissingVariableName { /// The byte offset within the input where the error occurs. /// @@ -192,8 +189,7 @@ impl std::fmt::Display for MissingVariableName { } /// The input string contains an unexpected character. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct UnexpectedCharacter { /// The byte offset within the input where the error occurs. /// @@ -225,8 +221,7 @@ impl std::fmt::Display for UnexpectedCharacter { } /// A struct to describe what was expected instead of the unexpected character. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct ExpectedCharacter { /// A human readable message to describe what is expected. pub(crate) message: &'static str, @@ -241,8 +236,7 @@ impl ExpectedCharacter { } /// The input string contains an unclosed variable placeholder. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct MissingClosingBrace { /// The byte offset within the input where the error occurs. /// @@ -260,8 +254,7 @@ impl std::fmt::Display for MissingClosingBrace { } /// The input string contains a placeholder for a variable that is not in the variable map. -#[derive(Debug, Clone)] -#[cfg_attr(test, derive(Eq, PartialEq))] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct NoSuchVariable { /// The byte offset within the input where the error occurs. /// diff --git a/src/lib.rs b/src/lib.rs index 286513b..244e686 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,24 +161,24 @@ where /// A parsed variable. #[derive(Debug)] -struct Variable<'a> { +pub struct Variable<'a> { /// The name of the variable. - name: &'a str, + pub name: &'a str, /// The start position of the name in the source. - name_start: usize, + pub name_start: usize, /// The default value of the variable. - default: Option>, + pub default: Option>, /// The end position of the entire variable in the source. - end_position: usize, + pub end_position: usize, } /// Parse a variable from source at the given position. /// /// The finger must be the position of the dollar sign in the source. -fn parse_variable(source: &[u8], finger: usize) -> Result { +pub fn parse_variable(source: &[u8], finger: usize) -> Result { if finger == source.len() { return Err(error::MissingVariableName { position: finger, From 54b294365a353f8f7cac8d1a2b23e1b2f194cd0a Mon Sep 17 00:00:00 2001 From: Ulrich Hornung Date: Wed, 10 Jan 2024 23:03:56 +0100 Subject: [PATCH 2/2] undo changes on error.rs --- src/error.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index 43e8193..368b92b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,8 @@ //! Module containing error details. /// An error that can occur during variable substitution. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub enum Error { /// The input string contains an invalid escape sequence. InvalidEscapeSequence(InvalidEscapeSequence), @@ -138,7 +139,8 @@ impl std::fmt::Display for CharOrByte { } /// The input string contains an invalid escape sequence. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct InvalidEscapeSequence { /// The byte offset within the input where the error occurs. /// @@ -168,7 +170,8 @@ impl std::fmt::Display for InvalidEscapeSequence { } /// The input string contains a variable placeholder without a variable name (`"${}"`). -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct MissingVariableName { /// The byte offset within the input where the error occurs. /// @@ -189,7 +192,8 @@ impl std::fmt::Display for MissingVariableName { } /// The input string contains an unexpected character. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct UnexpectedCharacter { /// The byte offset within the input where the error occurs. /// @@ -221,7 +225,8 @@ impl std::fmt::Display for UnexpectedCharacter { } /// A struct to describe what was expected instead of the unexpected character. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct ExpectedCharacter { /// A human readable message to describe what is expected. pub(crate) message: &'static str, @@ -236,7 +241,8 @@ impl ExpectedCharacter { } /// The input string contains an unclosed variable placeholder. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct MissingClosingBrace { /// The byte offset within the input where the error occurs. /// @@ -254,7 +260,8 @@ impl std::fmt::Display for MissingClosingBrace { } /// The input string contains a placeholder for a variable that is not in the variable map. -#[derive(Debug, Clone, Eq, PartialEq)] +#[derive(Debug, Clone)] +#[cfg_attr(test, derive(Eq, PartialEq))] pub struct NoSuchVariable { /// The byte offset within the input where the error occurs. ///