Skip to content

Commit

Permalink
fix rust errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Dec 5, 2023
1 parent 6808a3b commit 1e6623a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 101 deletions.
44 changes: 22 additions & 22 deletions cmd/crates/soroban-spec-json/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct StructField {
impl From<&ScSpecUdtStructFieldV0> for StructField {
fn from(f: &ScSpecUdtStructFieldV0) -> Self {
StructField {
doc: f.doc.to_string_lossy(),
name: f.name.to_string_lossy(),
doc: f.doc.to_utf8_string_lossy(),
name: f.name.to_utf8_string_lossy(),
value: (&f.type_).into(),
}
}
Expand All @@ -33,8 +33,8 @@ pub struct FunctionInput {
impl From<&ScSpecFunctionInputV0> for FunctionInput {
fn from(f: &ScSpecFunctionInputV0) -> Self {
FunctionInput {
doc: f.doc.to_string_lossy(),
name: f.name.to_string_lossy(),
doc: f.doc.to_utf8_string_lossy(),
name: f.name.to_utf8_string_lossy(),
value: (&f.type_).into(),
}
}
Expand All @@ -52,11 +52,11 @@ impl From<&ScSpecUdtUnionCaseV0> for UnionCase {
fn from(c: &ScSpecUdtUnionCaseV0) -> Self {
let (doc, name, values) = match c {
ScSpecUdtUnionCaseV0::VoidV0(v) => {
(v.doc.to_string_lossy(), v.name.to_string_lossy(), vec![])
(v.doc.to_utf8_string_lossy(), v.name.to_utf8_string_lossy(), vec![])
}
ScSpecUdtUnionCaseV0::TupleV0(t) => (
t.doc.to_string_lossy(),
t.name.to_string_lossy(),
t.doc.to_utf8_string_lossy(),
t.name.to_utf8_string_lossy(),
t.type_.iter().map(Type::from).collect(),
),
};
Expand All @@ -75,8 +75,8 @@ pub struct EnumCase {
impl From<&ScSpecUdtEnumCaseV0> for EnumCase {
fn from(c: &ScSpecUdtEnumCaseV0) -> Self {
EnumCase {
doc: c.doc.to_string_lossy(),
name: c.name.to_string_lossy(),
doc: c.doc.to_utf8_string_lossy(),
name: c.name.to_utf8_string_lossy(),
value: c.value,
}
}
Expand All @@ -93,8 +93,8 @@ pub struct ErrorEnumCase {
impl From<&ScSpecUdtErrorEnumCaseV0> for EnumCase {
fn from(c: &ScSpecUdtErrorEnumCaseV0) -> Self {
EnumCase {
doc: c.doc.to_string_lossy(),
name: c.name.to_string_lossy(),
doc: c.doc.to_utf8_string_lossy(),
name: c.name.to_utf8_string_lossy(),
value: c.value,
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ impl From<&ScSpecTypeDef> for Type {
element: Box::new(Type::from(vec.element_type.as_ref())),
},
ScSpecTypeDef::Udt(udt) => Type::Custom {
name: udt.name.to_string_lossy(),
name: udt.name.to_utf8_string_lossy(),
},
ScSpecTypeDef::BytesN(b) => Type::BytesN { n: b.n },
ScSpecTypeDef::Val => Type::Val,
Expand Down Expand Up @@ -213,29 +213,29 @@ impl From<&ScSpecEntry> for Entry {
fn from(spec: &ScSpecEntry) -> Self {
match spec {
ScSpecEntry::FunctionV0(f) => Entry::Function {
doc: f.doc.to_string_lossy(),
name: f.name.to_string_lossy(),
doc: f.doc.to_utf8_string_lossy(),
name: f.name.to_utf8_string_lossy(),
inputs: f.inputs.iter().map(FunctionInput::from).collect(),
outputs: f.outputs.iter().map(Type::from).collect(),
},
ScSpecEntry::UdtStructV0(s) => Entry::Struct {
doc: s.doc.to_string_lossy(),
name: s.name.to_string_lossy(),
doc: s.doc.to_utf8_string_lossy(),
name: s.name.to_utf8_string_lossy(),
fields: s.fields.iter().map(StructField::from).collect(),
},
ScSpecEntry::UdtUnionV0(u) => Entry::Union {
doc: u.doc.to_string_lossy(),
name: u.name.to_string_lossy(),
doc: u.doc.to_utf8_string_lossy(),
name: u.name.to_utf8_string_lossy(),
cases: u.cases.iter().map(UnionCase::from).collect(),
},
ScSpecEntry::UdtEnumV0(e) => Entry::Enum {
doc: e.doc.to_string_lossy(),
name: e.name.to_string_lossy(),
doc: e.doc.to_utf8_string_lossy(),
name: e.name.to_utf8_string_lossy(),
cases: e.cases.iter().map(EnumCase::from).collect(),
},
ScSpecEntry::UdtErrorEnumV0(e) => Entry::Enum {
doc: e.doc.to_string_lossy(),
name: e.name.to_string_lossy(),
doc: e.doc.to_utf8_string_lossy(),
name: e.name.to_utf8_string_lossy(),
cases: e.cases.iter().map(EnumCase::from).collect(),
},
}
Expand Down
60 changes: 30 additions & 30 deletions cmd/crates/soroban-spec-tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ impl Spec {
),
ScType::Option(type_) => return self.doc(name, &type_.value_type),
ScType::Udt(ScSpecTypeUdt { name }) => {
let spec_type = self.find(&name.to_string_lossy())?;
let spec_type = self.find(&name.to_utf8_string_lossy())?;
match spec_type {
ScSpecEntry::FunctionV0(ScSpecFunctionV0 { doc, .. })
| ScSpecEntry::UdtStructV0(ScSpecUdtStructV0 { doc, .. })
| ScSpecEntry::UdtUnionV0(ScSpecUdtUnionV0 { doc, .. })
| ScSpecEntry::UdtEnumV0(ScSpecUdtEnumV0 { doc, .. })
| ScSpecEntry::UdtErrorEnumV0(ScSpecUdtErrorEnumV0 { doc, .. }) => doc,
}
.to_string_lossy()
.to_utf8_string_lossy()
}
};
if let Some(mut ex) = self.example(type_) {
Expand Down Expand Up @@ -164,11 +164,11 @@ impl Spec {
.and_then(|specs| {
specs.iter().find(|e| {
let entry_name = match e {
ScSpecEntry::FunctionV0(x) => x.name.to_string_lossy(),
ScSpecEntry::UdtStructV0(x) => x.name.to_string_lossy(),
ScSpecEntry::UdtUnionV0(x) => x.name.to_string_lossy(),
ScSpecEntry::UdtEnumV0(x) => x.name.to_string_lossy(),
ScSpecEntry::UdtErrorEnumV0(x) => x.name.to_string_lossy(),
ScSpecEntry::FunctionV0(x) => x.name.to_utf8_string_lossy(),
ScSpecEntry::UdtStructV0(x) => x.name.to_utf8_string_lossy(),
ScSpecEntry::UdtUnionV0(x) => x.name.to_utf8_string_lossy(),
ScSpecEntry::UdtEnumV0(x) => x.name.to_utf8_string_lossy(),
ScSpecEntry::UdtErrorEnumV0(x) => x.name.to_utf8_string_lossy(),
};
name == entry_name
})
Expand Down Expand Up @@ -248,7 +248,7 @@ impl Spec {
| ScType::Address => Ok(Value::String(s.to_owned())),
ScType::Udt(ScSpecTypeUdt { name })
if matches!(
self.find(&name.to_string_lossy())?,
self.find(&name.to_utf8_string_lossy())?,
ScSpecEntry::UdtUnionV0(_) | ScSpecEntry::UdtStructV0(_)
) =>
{
Expand Down Expand Up @@ -321,13 +321,13 @@ impl Spec {
}

fn parse_udt(&self, name: &StringM<60>, value: &Value) -> Result<ScVal, Error> {
let name = &name.to_string_lossy();
let name = &name.to_utf8_string_lossy();
match (self.find(name)?, value) {
(ScSpecEntry::UdtStructV0(strukt), Value::Object(map)) => {
if strukt
.fields
.iter()
.any(|f| f.name.to_string_lossy() == "0")
.any(|f| f.name.to_utf8_string_lossy() == "0")
{
self.parse_tuple_strukt(
strukt,
Expand Down Expand Up @@ -379,7 +379,7 @@ impl Spec {
.to_vec()
.iter()
.map(|f| {
let name = &f.name.to_string_lossy();
let name = &f.name.to_utf8_string_lossy();
let v = map
.get(name)
.ok_or_else(|| Error::MissingKey(name.clone()))?;
Expand Down Expand Up @@ -422,9 +422,9 @@ impl Spec {
ScSpecUdtUnionCaseV0::VoidV0(v) => &v.name,
ScSpecUdtUnionCaseV0::TupleV0(v) => &v.name,
};
enum_case == &name.to_string_lossy()
enum_case == &name.to_utf8_string_lossy()
})
.ok_or_else(|| Error::EnumCase(enum_case.to_string(), union.name.to_string_lossy()))?;
.ok_or_else(|| Error::EnumCase(enum_case.to_string(), union.name.to_utf8_string_lossy()))?;

let mut res = vec![ScVal::Symbol(ScSymbol(
enum_case.try_into().map_err(Error::Xdr)?,
Expand Down Expand Up @@ -583,7 +583,7 @@ impl Spec {
///
/// May panic
pub fn udt_to_json(&self, name: &StringM<60>, sc_obj: &ScVal) -> Result<Value, Error> {
let name = &name.to_string_lossy();
let name = &name.to_utf8_string_lossy();
let udt = self.find(name)?;
Ok(match (sc_obj, udt) {
(ScVal::Map(Some(map)), ScSpecEntry::UdtStructV0(strukt)) => serde_json::Value::Object(
Expand All @@ -593,7 +593,7 @@ impl Spec {
.zip(map.iter())
.map(|(field, entry)| {
let val = self.xdr_to_json(&entry.val, &field.type_)?;
Ok((field.name.to_string_lossy(), val))
Ok((field.name.to_utf8_string_lossy(), val))
})
.collect::<Result<serde_json::Map<String, _>, Error>>()?,
),
Expand All @@ -611,7 +611,7 @@ impl Spec {
let (first, rest) = match v.split_at(1) {
([first], []) => (first, None),
([first], rest) => (first, Some(rest)),
_ => return Err(Error::IllFormedEnum(union.name.to_string_lossy())),
_ => return Err(Error::IllFormedEnum(union.name.to_utf8_string_lossy())),
};

let ScVal::Symbol(case_name) = first else {
Expand All @@ -627,14 +627,14 @@ impl Spec {
};
name.as_vec() == case_name.as_vec()
})
.ok_or_else(|| Error::FailedToFindEnumCase(case_name.to_string_lossy()))?;
.ok_or_else(|| Error::FailedToFindEnumCase(case_name.to_utf8_string_lossy()))?;

let case_name = case_name.to_string_lossy();
let case_name = case_name.to_utf8_string_lossy();
match case {
ScSpecUdtUnionCaseV0::TupleV0(v) => {
let rest = rest.ok_or_else(|| {
Error::EnumMissingSecondValue(
union.name.to_string_lossy(),
union.name.to_utf8_string_lossy(),
case_name.clone(),
)
})?;
Expand Down Expand Up @@ -1130,11 +1130,11 @@ impl Spec {
}
ScType::BytesN(t) => Some(format!("{}_hex_bytes", t.n)),
ScType::Udt(ScSpecTypeUdt { name }) => {
match self.find(&name.to_string_lossy()).ok()? {
match self.find(&name.to_utf8_string_lossy()).ok()? {
ScSpecEntry::UdtStructV0(ScSpecUdtStructV0 { fields, .. })
if fields
.get(0)
.map(|f| f.name.to_string_lossy() == "0")
.map(|f| f.name.to_utf8_string_lossy() == "0")
.unwrap_or_default() =>
{
let fields = fields
Expand All @@ -1159,7 +1159,7 @@ impl Spec {
let inner = strukt
.fields
.iter()
.map(|f| (f.name.to_string_lossy(), &f.type_))
.map(|f| (f.name.to_utf8_string_lossy(), &f.type_))
.map(|(name, type_)| {
let type_ = self.arg_value_name(type_, depth + 1)?;
Some(format!("{name}: {type_}"))
Expand All @@ -1176,15 +1176,15 @@ impl Spec {
.map(|f| {
Some(match f {
ScSpecUdtUnionCaseV0::VoidV0(ScSpecUdtUnionCaseVoidV0 { name, .. }) => {
name.to_string_lossy()
name.to_utf8_string_lossy()
}
ScSpecUdtUnionCaseV0::TupleV0(ScSpecUdtUnionCaseTupleV0 {
name,
type_,
..
}) => format!(
"{}({})",
name.to_string_lossy(),
name.to_utf8_string_lossy(),
type_
.iter()
.map(|type_| self.arg_value_name(type_, depth + 1))
Expand Down Expand Up @@ -1283,7 +1283,7 @@ impl Spec {
Some(format!("\"{res}\""))
}
ScType::Udt(ScSpecTypeUdt { name }) => {
self.example_udts(name.to_string_lossy().as_ref())
self.example_udts(name.to_utf8_string_lossy().as_ref())
}
// No specific value name for these yet.
ScType::Val => None,
Expand All @@ -1294,7 +1294,7 @@ impl Spec {
match self.find(name).ok() {
Some(ScSpecEntry::UdtStructV0(strukt)) => {
// Check if a tuple strukt
if !strukt.fields.is_empty() && strukt.fields[0].name.to_string_lossy() == "0" {
if !strukt.fields.is_empty() && strukt.fields[0].name.to_utf8_string_lossy() == "0" {
let value_types = strukt
.fields
.iter()
Expand All @@ -1307,7 +1307,7 @@ impl Spec {
let inner = strukt
.fields
.iter()
.map(|f| (f.name.to_string_lossy(), &f.type_))
.map(|f| (f.name.to_utf8_string_lossy(), &f.type_))
.map(|(name, type_)| {
let type_ = self.example(type_)?;
let name = format!(r#""{name}""#);
Expand All @@ -1331,21 +1331,21 @@ impl Spec {
.iter()
.map(|case| match case {
ScSpecUdtUnionCaseV0::VoidV0(ScSpecUdtUnionCaseVoidV0 { name, .. }) => {
Some(format!("\"{}\"", name.to_string_lossy()))
Some(format!("\"{}\"", name.to_utf8_string_lossy()))
}
ScSpecUdtUnionCaseV0::TupleV0(ScSpecUdtUnionCaseTupleV0 {
name, type_, ..
}) => {
if type_.len() == 1 {
let single = self.example(&type_[0])?;
Some(format!("{{\"{}\":{single}}}", name.to_string_lossy()))
Some(format!("{{\"{}\":{single}}}", name.to_utf8_string_lossy()))
} else {
let names = type_
.iter()
.map(|t| self.example(t))
.collect::<Option<Vec<_>>>()?
.join(", ");
Some(format!("{{\"{}\":[{names}]}}", name.to_string_lossy()))
Some(format!("{{\"{}\":[{names}]}}", name.to_utf8_string_lossy()))
}
}
})
Expand Down
18 changes: 9 additions & 9 deletions cmd/crates/soroban-spec-tools/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ impl Display for ContractSpec {
}

fn write_func(f: &mut std::fmt::Formatter<'_>, func: &ScSpecFunctionV0) -> std::fmt::Result {
writeln!(f, " • Function: {}", func.name.to_string_lossy())?;
writeln!(f, " • Function: {}", func.name.to_utf8_string_lossy())?;
if func.doc.len() > 0 {
writeln!(
f,
" Docs: {}",
&indent(&func.doc.to_string_lossy(), 11).trim()
&indent(&func.doc.to_utf8_string_lossy(), 11).trim()
)?;
}
writeln!(
Expand All @@ -174,7 +174,7 @@ fn write_union(f: &mut std::fmt::Formatter<'_>, udt: &ScSpecUdtUnionV0) -> std::
writeln!(
f,
" Docs: {}",
indent(&udt.doc.to_string_lossy(), 10).trim()
indent(&udt.doc.to_utf8_string_lossy(), 10).trim()
)?;
}
writeln!(f, " Cases:")?;
Expand All @@ -191,15 +191,15 @@ fn write_struct(f: &mut std::fmt::Formatter<'_>, udt: &ScSpecUdtStructV0) -> std
writeln!(
f,
" Docs: {}",
indent(&udt.doc.to_string_lossy(), 10).trim()
indent(&udt.doc.to_utf8_string_lossy(), 10).trim()
)?;
}
writeln!(f, " Fields:")?;
for field in udt.fields.iter() {
writeln!(
f,
" • {}: {}",
field.name.to_string_lossy(),
field.name.to_utf8_string_lossy(),
indent(&format!("{:#?}", field.type_), 8).trim()
)?;
if field.doc.len() > 0 {
Expand All @@ -216,7 +216,7 @@ fn write_enum(f: &mut std::fmt::Formatter<'_>, udt: &ScSpecUdtEnumV0) -> std::fm
writeln!(
f,
" Docs: {}",
indent(&udt.doc.to_string_lossy(), 10).trim()
indent(&udt.doc.to_utf8_string_lossy(), 10).trim()
)?;
}
writeln!(f, " Cases:")?;
Expand All @@ -233,7 +233,7 @@ fn write_error(f: &mut std::fmt::Formatter<'_>, udt: &ScSpecUdtErrorEnumV0) -> s
writeln!(
f,
" Docs: {}",
indent(&udt.doc.to_string_lossy(), 10).trim()
indent(&udt.doc.to_utf8_string_lossy(), 10).trim()
)?;
}
writeln!(f, " Cases:")?;
Expand All @@ -254,9 +254,9 @@ fn indent(s: &str, n: usize) -> String {

fn format_name(lib: &StringM<80>, name: &StringM<60>) -> String {
if lib.len() > 0 {
format!("{}::{}", lib.to_string_lossy(), name.to_string_lossy())
format!("{}::{}", lib.to_utf8_string_lossy(), name.to_utf8_string_lossy())
} else {
name.to_string_lossy()
name.to_utf8_string_lossy()
}
}

Expand Down
Loading

0 comments on commit 1e6623a

Please sign in to comment.