Skip to content

Commit

Permalink
Define grep_file_contents interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
panhania committed Sep 25, 2024
1 parent 9286e01 commit 9b336ad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
31 changes: 28 additions & 3 deletions crates/rrg/src/action/grep_file_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
// Use of this source code is governed by an MIT-style license that can be found
// in the LICENSE file or at https://opensource.org/licenses/MIT.

use std::path::PathBuf;

/// Arguments of the `grep_file_contents` action.
pub struct Args {
/// Path to the file to grep the contents of.
path: PathBuf,
/// Regular expression to search for in the file contents.
regex: regex::Regex,
}

/// Result of the `grep_file_contents` action.
pub struct Item {
/// Byte offset within the file from which the content matched.
offset: u64,
/// Content that matched the specified regular expression.
content: String,
}

/// Handles invocations of the `grep_file_contents` action.
Expand All @@ -23,8 +33,19 @@ impl crate::request::Args for Args {

type Proto = rrg_proto::grep_file_contents::Args;

fn from_proto(proto: Self::Proto) -> Result<Args, crate::request::ParseArgsError> {
todo!()
fn from_proto(mut proto: Self::Proto) -> Result<Args, crate::request::ParseArgsError> {
use crate::request::ParseArgsError;

let path = PathBuf::try_from(proto.take_path())
.map_err(|error| ParseArgsError::invalid_field("path", error))?;

let regex = regex::Regex::new(proto.regex())
.map_err(|error| ParseArgsError::invalid_field("regex", error))?;

Ok(Args {
path,
regex,
})
}
}

Expand All @@ -33,6 +54,10 @@ impl crate::response::Item for Item {
type Proto = rrg_proto::grep_file_contents::Result;

fn into_proto(self) -> Self::Proto {
todo!()
let mut proto = Self::Proto::default();
proto.set_offset(self.offset);
proto.set_content(self.content);

proto
}
}
23 changes: 23 additions & 0 deletions proto/rrg/action/grep_file_contents.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@ package rrg.action.get_file_contents;
import "rrg/fs.proto";

message Args {
// Absolute path to the file to grep the contents of.
//
// The file content must be valid UTF-8.
rrg.fs.Path path = 1;

// Regular expression to search for in the file contents.
//
// The specific syntax of the regex language is left unspecified as the
// implementation detail but most common regex features can be expected to
// be supported.
string regex = 2;

// TODO(@panhania): Add support for files that not necessarily conform to
// Unicode.

// TODO(@panhania): Add support for different file encodings.
}

message Result {
// Byte offset within the file from which the content matched.
uint64 offset = 1;

// Content that matched the specified regular expression.
string content = 2;

// TODO(@panhania): Add support for capture groups.
}

0 comments on commit 9b336ad

Please sign in to comment.