diff --git a/src/builtin/ops.rs b/src/builtin/ops.rs index 29ea447..6d268c0 100644 --- a/src/builtin/ops.rs +++ b/src/builtin/ops.rs @@ -158,7 +158,7 @@ impl FuncOp { // Create an empty entry block. let arg_types = ty.deref(ctx).get_inputs().clone(); let region = op.deref_mut(ctx).get_region(0).unwrap(); - let body = BasicBlock::new(ctx, Some("entry".into()), arg_types); + let body = BasicBlock::new(ctx, Some("entry".try_into().unwrap()), arg_types); body.insert_at_front(region, ctx); { let opref = &mut *op.deref_mut(ctx); diff --git a/src/debug_info.rs b/src/debug_info.rs index 2129299..2861eca 100644 --- a/src/debug_info.rs +++ b/src/debug_info.rs @@ -154,7 +154,11 @@ mod tests { builtin::register(&mut ctx); let i64_ty = IntegerType::get(&mut ctx, 64, Signedness::Signed); - let block = BasicBlock::new(&mut ctx, Some("entry".into()), vec![i64_ty.into()]); + let block = BasicBlock::new( + &mut ctx, + Some("entry".try_into().unwrap()), + vec![i64_ty.into()], + ); set_block_arg_name(&ctx, block, 0, "foo".to_string()); assert!(get_block_arg_name(&ctx, block, 0).unwrap() == "foo"); block.deref(&ctx).verify(&ctx)?; diff --git a/src/dialect.rs b/src/dialect.rs index 2e6f660..3466b4a 100644 --- a/src/dialect.rs +++ b/src/dialect.rs @@ -24,7 +24,7 @@ pub struct DialectName(Identifier); impl DialectName { /// Create a new DialectName pub fn new(name: &str) -> DialectName { - DialectName(name.into()) + DialectName(name.try_into().expect("Invalid Identifier for DialectName")) } } diff --git a/src/identifier.rs b/src/identifier.rs index c2b7f70..dd748a4 100644 --- a/src/identifier.rs +++ b/src/identifier.rs @@ -6,7 +6,6 @@ use combine::{token, Parser}; use thiserror::Error; use crate::{ - common_traits::Verify, context::Context, parsable::{self, Parsable, ParseResult}, printable::{self, Printable}, @@ -35,15 +34,23 @@ impl Display for Identifier { } } -impl From for Identifier { - fn from(value: String) -> Self { - Identifier(value) +impl TryFrom for Identifier { + type Error = result::Error; + + fn try_from(value: String) -> Result { + let re = regex::Regex::new(r"[a-zA-Z_][a-zA-Z0-9_]*").unwrap(); + if !(re.is_match(&value)) { + return verify_err_noloc!(MalformedIdentifierErr(value.clone())); + } + Ok(Identifier(value)) } } -impl From<&str> for Identifier { - fn from(value: &str) -> Self { - Identifier(value.to_string()) +impl TryFrom<&str> for Identifier { + type Error = result::Error; + + fn try_from(value: &str) -> Result { + TryFrom::::try_from(value.to_string()) } } @@ -65,16 +72,6 @@ impl Deref for Identifier { #[error("Malformed identifier {0}")] struct MalformedIdentifierErr(String); -impl Verify for Identifier { - fn verify(&self, _ctx: &Context) -> result::Result<()> { - let re = regex::Regex::new(r"[a-zA-Z_][a-zA-Z0-9_]*").unwrap(); - if !(re.is_match(&self.0)) { - return verify_err_noloc!(MalformedIdentifierErr(self.0.clone())); - } - Ok(()) - } -} - impl Parsable for Identifier { type Arg = (); type Parsed = Identifier; @@ -89,7 +86,10 @@ impl Parsable for Identifier { .map(|(c, rest)| c.to_string() + &rest); parser - .map(|str| str.into()) + .map(|str| { + str.try_into() + .expect("Something is wrong in our Identifier parser") + }) .parse_stream(state_stream) .into() } diff --git a/src/type.rs b/src/type.rs index 3cba74a..5f6c661 100644 --- a/src/type.rs +++ b/src/type.rs @@ -218,7 +218,7 @@ pub struct TypeName(Identifier); impl TypeName { /// Create a new TypeName. pub fn new(name: &str) -> TypeName { - TypeName(name.into()) + TypeName(name.try_into().expect("Invalid Identifier for TypeName")) } }