Skip to content

Commit ef6cb43

Browse files
authored
rust: expose writer function and some new helpers (#80)
1 parent b4ad2ba commit ef6cb43

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

bindings/rust/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "scip"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = """

bindings/rust/src/mod.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,29 @@
33
// This will pull generated code into `scip::types`
44
#[path = "generated/mod.rs"]
55
mod scip_mod;
6-
76
pub use scip_mod::scip as types;
87

98
// Exports symbol usage under scip::symbol namespace
109
pub mod symbol;
10+
11+
/// Write a message to a particular filepath.
12+
///
13+
/// This allows users of the SCIP library to not add protobuf as
14+
/// a direct dependency of the project (which can be useful to limit
15+
/// usage of protobuf elsewhere if not desired).
16+
pub fn write_message_to_file<P>(
17+
path: P,
18+
msg: impl protobuf::Message,
19+
) -> Result<(), Box<dyn std::error::Error>>
20+
where
21+
P: AsRef<std::path::Path>,
22+
{
23+
use std::io::Write;
24+
25+
let res = msg.write_to_bytes()?;
26+
let output = std::fs::File::create(path)?;
27+
let mut writer = std::io::BufWriter::new(output);
28+
writer.write_all(&res)?;
29+
30+
Ok(())
31+
}

bindings/rust/src/symbol.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ pub fn format_symbol(symbol: Symbol) -> String {
105105
format_symbol_with(symbol, SymbolFormatOptions::default())
106106
}
107107

108+
impl Symbol {
109+
pub fn new_local(id: usize) -> Self {
110+
internal_local_symbol(id.to_string().as_str())
111+
}
112+
}
113+
108114
pub fn parse_symbol(symbol: &str) -> Result<Symbol, SymbolError> {
109115
fn dot(s: String) -> String {
110116
if s.as_str() == "." {
@@ -118,7 +124,7 @@ pub fn parse_symbol(symbol: &str) -> Result<Symbol, SymbolError> {
118124

119125
let scheme = parser.accept_space_escaped_identifier("scheme")?;
120126
if scheme == "local" {
121-
return Ok(new_local_symbol(
127+
return Ok(internal_local_symbol(
122128
symbol
123129
.chars()
124130
.skip(parser.index)
@@ -338,7 +344,7 @@ impl SymbolParser {
338344
}
339345
}
340346

341-
fn new_local_symbol(id: &str) -> Symbol {
347+
fn internal_local_symbol(id: &str) -> Symbol {
342348
let descriptor = Descriptor {
343349
name: id.to_string(),
344350
disambiguator: "".to_string(),
@@ -376,7 +382,7 @@ mod test {
376382
fn parses_local() {
377383
assert_eq!(
378384
parse_symbol("local a").expect("to parse local"),
379-
new_local_symbol("a")
385+
internal_local_symbol("a")
380386
);
381387
}
382388

0 commit comments

Comments
 (0)