Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
on: # yamllint disable-line rule:truthy
pull_request:
workflow_dispatch:
push:
branches:
- master
Expand Down
23 changes: 11 additions & 12 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,23 +1442,22 @@ fn analyze_named_module(
ModuleItem::Module(module) if module.name == name => Some(module),
_ => None,
});
let witness_module = iter
.next()
.ok_or(Error::ModuleRequired(name.shallow_clone()))
.with_span(from)?;
let witness_module = iter.next();
if iter.next().is_some() {
return Err(Error::ModuleRedefined(name)).with_span(from);
}
let mut map = HashMap::new();
for assignment in witness_module.assignments() {
if map.contains_key(assignment.name()) {
return Err(Error::WitnessReassigned(assignment.name().shallow_clone()))
.with_span(assignment);
if let Some(witness_module) = witness_module {
for assignment in witness_module.assignments() {
if map.contains_key(assignment.name()) {
return Err(Error::WitnessReassigned(assignment.name().shallow_clone()))
.with_span(assignment);
}
map.insert(
assignment.name().shallow_clone(),
assignment.value().clone(),
);
}
map.insert(
assignment.name().shallow_clone(),
assignment.value().clone(),
);
}
Ok(map)
}
Expand Down
25 changes: 25 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ pub enum Error {
ModuleRedefined(ModuleName),
ArgumentMissing(WitnessName),
ArgumentTypeMismatch(WitnessName, ResolvedType, ResolvedType),
InvalidJsonFormat(String),
UndefinedWitness(WitnessName),
UndefinedParameter(WitnessName),
WitnessMultipleAssignments(WitnessName),
ArgumentMultipleAssignments(WitnessName),
}

#[rustfmt::skip]
Expand Down Expand Up @@ -481,6 +486,26 @@ impl fmt::Display for Error {
f,
"Parameter `{name}` was declared with type `{declared}` but its assigned argument is of type `{assigned}`"
),
Error::InvalidJsonFormat(description) => write!(
f,
"Invalid JSON format for witness/argument deserialization: {description}"
),
Error::UndefinedWitness(name) => write!(
f,
"Witness `{name}` is not defined in WitnessTypes context"
),
Error::UndefinedParameter(name) => write!(
f,
"Parameter `{name}` is not defined in Parameters context"
),
Error::WitnessMultipleAssignments(name) => write!(
f,
"Witness `{name}` is assigned multiple times in JSON"
),
Error::ArgumentMultipleAssignments(name) => write!(
f,
"Argument `{name}` is assigned multiple times in JSON"
),
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,18 @@ impl CompiledProgram {
.and_then(|template| template.instantiate(arguments, include_debug_symbols))
}

/// Access the debug symbols for the Simplicity target code.
/// Access the debug symbols for the Simplicity target code.
pub fn debug_symbols(&self) -> &DebugSymbols {
&self.debug_symbols
}

/// Access the witness types declared in the SimplicityHL program.
///
/// [NEW METHOD - INSERT HERE]
pub fn witness_types(&self) -> &WitnessTypes {
&self.witness_types
}

/// Access the Simplicity target code, without witness data.
pub fn commit(&self) -> Arc<CommitNode<Elements>> {
named::forget_names(&self.simplicity)
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.map(|wit_file| -> Result<simplicityhl::WitnessValues, String> {
let wit_path = std::path::Path::new(wit_file);
let wit_text = std::fs::read_to_string(wit_path).map_err(|e| e.to_string())?;
let witness = serde_json::from_str::<simplicityhl::WitnessValues>(&wit_text).unwrap();
// Use new context-aware deserialization method
// Type information is provided by the compiled program (witness_types)
// Users only need to specify values in simplified JSON format
let witness = simplicityhl::WitnessValues::from_json_with_types(
&wit_text,
&compiled.witness_types(),
)
.map_err(|e| e.to_string())?;
Ok(witness)
})
.transpose()?;
Expand Down
Loading